需求
在一些项目现场,很多时候,都是缺少专门的数据库运维人员的,但是开发人员开发项目,又需要用到MySQL数据库服务器,而且不同的项目的数据库又要分开
秒级搭建MySQL数据库服务,太香了

解决方案
对于非数据库运维人员,安装MySQL数据库有一定的难度,所以在这里推荐用docker来搭建MySQL数据库服务。这种方案门槛低,对于非数据库专业人员也能秒级搭建好一条MySQL服务。

搭建步骤
拉取MySQL数据库镜像


docker pull mysql:latest

直接执行这个命令,意思是拉取最新的镜像,但是实际项目可能需要制定的数据库版本,所以这里需要制定标签,拉取需要的镜像

docker pull mysql:5.7.28

秒级搭建MySQL数据库服务,太香了

创建MySQL容器
拉取好镜像之后,就可以创建2个MySQL容器了,对外访问端口为3306,3307


[root@localhost ~]# docker run --name mysql5.7.28_3306 -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 -d -it mysql:5.7.28
423e39a1c8669a53942aed14002102adbb8871c47edfbaa67825691eb16d0d45
[root@localhost ~]# docker run --name mysql5.7.28_3307 -e MYSQL_ROOT_PASSWORD=root -p 3307:3306 -d -it mysql:5.7.28
ff668e421d59b1fef61d58b70532bf87d5915da1c47d63db690857d74d283e12
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                               NAMES
ff668e421d59        mysql:5.7.28        "docker-entrypoint.s鈥 29 seconds ago       Up 27 seconds       33060/tcp, 0.0.0.0:3307->3306/tcp   mysql5.7.28_3307
423e39a1c866        mysql:5.7.28        "docker-entrypoint.s鈥 About a minute ago   Up About a minute   0.0.0.0:3306->3306/tcp, 33060/tcp   mysql5.7.28_3306

这样2个容器就创建好了,只是用docker ps显示的结果被截断了,不够友好,可以用下面的命令,来展示你需要看到的列信息就好

[root@localhost ~]# docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Names}}\t{{.Ports}}\t{{.RunningFor}}\t{{.Status}}"
CONTAINER ID        IMAGE               NAMES               PORTS                               CREATED              STATUS
ff668e421d59        mysql:5.7.28        mysql5.7.28_3307    33060/tcp, 0.0.0.0:3307->3306/tcp   About a minute ago   Up About a minute
423e39a1c866        mysql:5.7.28        mysql5.7.28_3306    0.0.0.0:3306->3306/tcp, 33060/tcp   2 minutes ago        Up 2 minutes

MySQL服务测试
验证端口


[root@localhost ~]# netstat -an|egrep "3306|3307"
tcp6       0      0 :::3306                 :::*                    LISTEN
tcp6       0      0 :::3307                 :::*                    LISTEN

可以看到,3306和3307端口已经开启。

MySQL客户端连接测试

[mysql@localhost ~]$ mysql -uroot -proot -h 192.168.17.128 -P 3306
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.28 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> quit
Bye
[mysql@localhost ~]$
[mysql@localhost ~]$ mysql -uroot -proot -h 192.168.17.128 -P 3307
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.28 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> quit

可以看到,两个MySQL数据库服务已经搭建好了,整个搭建都不到1分钟。