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


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

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

文章插图
项目需求
解决方案对于非数据库运维人员,安装MySQL数据库有一定的难度,所以在这里推荐用Docker来搭建MySQL数据库服务 。这种方案门槛低,对于非数据库专业人员也能秒级搭建好一条MySQL服务 。
搭建步骤拉取MySQL数据库镜像docker pull mysql:latest直接执行这个命令,意思是拉取最新的镜像,但是实际项目可能需要制定的数据库版本,所以这里需要制定标签,拉取需要的镜像
【秒级搭建MySQL数据库服务,太香了】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.28423e39a1c8669a53942aed14002102adbb8871c47edfbaa67825691eb16d0d45[root@localhost ~]# docker run --name mysql5.7.28_3307 -e MYSQL_ROOT_PASSWORD=root -p 3307:3306 -d -it mysql:5.7.28ff668e421d59b1fef61d58b70532bf87d5915da1c47d63db690857d74d283e12[root@localhost ~]# docker psCONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMESff668e421d59mysql:5.7.28"docker-entrypoint.s鈥 29 seconds agoUp 27 seconds33060/tcp, 0.0.0.0:3307->3306/tcpmysql5.7.28_3307423e39a1c866mysql:5.7.28"docker-entrypoint.s鈥 About a minute agoUp About a minute0.0.0.0:3306->3306/tcp, 33060/tcpmysql5.7.28_3306这样2个容器就创建好了,只是用docker ps显示的结果被截断了,不够友好,可以用下面的命令,来展示你需要看到的列信息就好
[root@localhost ~]# docker ps --format "table {{.ID}}t{{.Image}}t{{.Names}}t{{.Ports}}t{{.RunningFor}}t{{.Status}}"CONTAINER IDIMAGENAMESPORTSCREATEDSTATUSff668e421d59mysql:5.7.28mysql5.7.28_330733060/tcp, 0.0.0.0:3307->3306/tcpAbout a minute agoUp About a minute423e39a1c866mysql:5.7.28mysql5.7.28_33060.0.0.0:3306->3306/tcp, 33060/tcp2 minutes agoUp 2 minutesMySQL服务测试验证端口
[root@localhost ~]# netstat -an|egrep "3306|3307"tcp600 :::3306:::*LISTENtcp600 :::3307:::*LISTEN可以看到,3306和3307端口已经开启 。
MySQL客户端连接测试
[mysql@localhost ~]$ mysql -uroot -proot -h 192.168.17.128 -P 3306mysql: [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 2Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.mysql> quitBye[mysql@localhost ~]$ [mysql@localhost ~]$ mysql -uroot -proot -h 192.168.17.128 -P 3307mysql: [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 2Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.mysql> quit可以看到,两个MySQL数据库服务已经搭建好了,整个搭建都不到1分钟 。




    推荐阅读