1. Overview
1.概述
In today’s technological era, organizations have to release apps quickly to attract and retain business. This makes the teams build and configure the deployment environment at a faster pace with a lesser cost. However, containerization technologies come to the rescue for building a lightweight infrastructure.
在今天的技术时代,企业必须快速发布应用程序以吸引和保留业务。这使得团队以更快的速度建立和配置部署环境,并减少成本。然而,容器化技术为构建轻量级基础设施提供了帮助。
This article will elucidate the method of deploying and connecting to a MySQL server in a container-based environment.
本文将阐明在基于容器的环境中部署和连接到MySQL服务器的方法。
Now, let’s get into the nitty-gritty of it.
现在,让我们来讨论一下其中的细节问题。
2. MySQL Container Deployment
2.MySQL容器的部署
Firstly, let’s look into the steps involved in the MySQL container deployment. Basically, MySQL follows the Client-Server architecture model. Here, the server is a container image with databases, whereas the client is used to access the database on the host machine.
首先,让我们看看在MySQL容器部署中涉及的步骤。基本上,MySQL遵循客户-服务器架构模型。在这里,服务器是一个带有数据库的容器镜像,而客户端是用来访问主机上的数据库的。
We’ve divided the deployment workflow into three sections.
我们把部署工作流程分为三个部分。
2.1. MySQL Server Deployment
2.1.MySQL服务器的部署
Now, let’s bring the MySQL server instance into Docker. We can simply build a container based on the MySQL image pulled from the Docker Hub. There are two points we should consider when choosing which version of an image to pull from the Docker Hub:
现在,让我们把MySQL服务器实例引入Docker。我们可以简单地根据从Docker Hub中提取的MySQL镜像构建一个容器。在选择从Docker Hub拉取哪个版本的镜像时,我们应该考虑两点。
- Official Image Stamping – These are more secure and curated images from the MySQL developer team.
- Latest Tag – Unless we have any reservations on the MySQL version, we can go with the latest version available in the repository.
Let’s pull the official MySQL image from Docker Hub using the docker pull command:
让我们使用docker pull命令从Docker Hub拉出官方MySQL镜像。
$ docker pull mysql:latest
latest: Pulling from library/mysql
f003217c5aae: Pull complete
…
… output truncated …
…
70f46ebb971a: Pull complete
db6ea71d471d: Waiting
c2920c795b25: Downloading [=================================================> ] 105.6MB/107.8MB
26c3bdf75ff5: Download complete
Usually, the images are distinct layers tightly coupled in an ordered form as described in the manifest file. Our docker pull command will get the layers of the images from the blob store and automatically create the image using the manifest file:
通常,图像是不同的层,以清单文件中描述的有序形式紧密耦合在一起。我们的docker pull命令将从blob存储中获取图像的层,并使用清单文件自动创建图像。
…
… output truncated …
…
4607fa685ac6: Pull complete
Digest: sha256:1c75ba7716c6f73fc106dacedfdcf13f934ea8c161c8b3b3e4618bcd5fbcf195
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
The bundled image gets a hash code for future reference as demonstrated above.
捆绑的图像会得到一个哈希代码,供将来参考,如上图所示。
Without further ado, let’s run the container. The docker run command typically creates the writeable container layer on top of the image layers. We’ll need to provide the container name using the -name argument and use the MySQL image with the latest tag. Further, we’ll set the MySQL server password through the environment variable MYSQL_ROOT_PASSWORD. In our case, the password is set to “baeldung”.
不多说了,让我们来运行容器。docker run命令通常在图像层之上创建可写的容器层。我们需要使用-name参数提供容器名称,并使用带有最新标签的MySQL图像。此外,我们将通过环境变量 MYSQL_ROOT_PASSWORD来设置MySQL服务器密码。在我们的例子中,密码被设置为 “baeldung”。
Finally, the -d option helps us run the container as a daemon. The output throws another hash code for future container management:
最后,-d选项帮助我们将容器作为一个守护程序运行。输出中会抛出另一个哈希代码,用于未来的容器管理。
$ docker run --name bael-mysql-demo -e MYSQL_ROOT_PASSWORD=baeldung -d mysql:latest
fedf880ce2b690f9205c7a37f32d75f669fdb1da2505e485e44cadd0b912bd35
We can see all running containers in a host through the ps command:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fedf880ce2b6 mysql:latest "docker-entrypoint.s…" 17 seconds ago Up 16 seconds 3306/tcp, 33060/tcp bael-mysql-demo
2.2. MySQL Client Installation
2.2 MySQL客户端的安装
It’s mandatory to install a client to get easy access to the MySQL server. Depending on our need, we can either install the client on the host machine or any other machine or container that has IP reachability with the server container:
安装一个客户端以方便访问MySQL服务器是必须的。根据我们的需要,我们既可以在主机上安装客户端,也可以在与服务器容器有IP联系的任何其他机器或容器上安装。
$ sudo apt install mysql-client -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
mysql-client is already the newest version (5.7.37-0ubuntu0.18.04.1).
…
… output truncated …
…
Now, let’s enable the extraction of the installation path and version of the MySQL client:
现在,让我们启用提取MySQL客户端的安装路径和版本。
$ which mysql
/usr/bin/mysql
$ mysql --version
mysql Ver 14.14 Distrib 5.7.37, for Linux (x86_64) using EditLine wrapper
2.3. Establish Communication
2.3.建立沟通
Next, let’s log in to the server using the installed client. Traditionally, we use the MySQL command with username and password for server login. However, it will not work in the case of container-based solutions:
接下来,让我们使用已安装的客户端登录到服务器上。传统上,我们使用带有用户名和密码的MySQL命令进行服务器登录。然而,在基于容器的解决方案的情况下,它将不起作用。
$ mysql -u root -p
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
We’ll end up with socket errors as illustrated above. Here, it’s important to understand that the MySQL server is a container and not simply installed on the host machine. As highlighted in the above section, containers are lightweight servers with their own compute resources, networking, and storage.
我们最终会出现如上图所示的套接字错误。在这里,重要的是要理解,MySQL服务器是一个容器,而不是简单地安装在主机上。正如上节所强调的,容器是轻量级的服务器,拥有自己的计算资源、网络和存储。
The inspect command helps to allocate an IP address to the MySQL server instance:
inspect命令有助于为MySQL服务器实例分配一个IP 地址。
$ docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' bael-mysql-demo
172.17.0.2
Let’s provide the above IP address in the client’s host option, with the default port number and protocol type as TCP:
让我们在客户端的主机选项中提供上述IP地址,默认端口号和协议类型为TCP。
$ mysql -h 172.17.0.2 -P 3306 --protocol=tcp -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
…
… output truncated …
…
mysql>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> exit
Bye
Congrats, we have logged into the MySQL server successfully!
祝贺你,我们已经成功地登录到MySQL服务器上了!
3. Conclusion
3.总结
In summary, we’ve seen in detail the steps for deploying a MySQL server container, installing the MySQL client on the host machine, and finally, establishing the connection between them using container information.
综上所述,我们已经详细看到了部署MySQL服务器容器的步骤,在主机上安装MySQL客户端,最后是使用容器信息建立它们之间的连接。