需求
在一些項目現(xiàn)場,很多時候,都是缺少專門的數(shù)據(jù)庫運(yùn)維人員的,但是開發(fā)人員開發(fā)項目,又需要用到MySQL數(shù)據(jù)庫服務(wù)器,而且不同的項目的數(shù)據(jù)庫又要分開
項目需求
解決方案
對于非數(shù)據(jù)庫運(yùn)維人員,安裝MySQL數(shù)據(jù)庫有一定的難度,所以在這里推薦用docker來搭建MySQL數(shù)據(jù)庫服務(wù)。這種方案門檻低,對于非數(shù)據(jù)庫專業(yè)人員也能秒級搭建好一條MySQL服務(wù)。
搭建步驟
拉取MySQL數(shù)據(jù)庫鏡像
docker pull mysql:latest
直接執(zhí)行這個命令,意思是拉取最新的鏡像,但是實際項目可能需要制定的數(shù)據(jù)庫版本,所以這里需要制定標(biāo)簽,拉取需要的鏡像
docker pull mysql:5.7.28
創(chuàng)建MySQL容器
拉取好鏡像之后,就可以創(chuàng)建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 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個容器就創(chuàng)建好了,只是用docker ps顯示的結(jié)果被截斷了,不夠友好,可以用下面的命令,來展示你需要看到的列信息就好
[root@localhost ~]# docker ps --format 'table {{.ID}}\t{{.Image}}\t{{.Names}}\t{{.Ports}}\t{{.RunningFor}}\t{{.Status}}'CONTAINER ID IMAGE NAMES PORTS CREATED STATUSff668e421d59 mysql:5.7.28 mysql5.7.28_3307 33060/tcp, 0.0.0.0:3307->3306/tcp About a minute ago Up About a minute423e39a1c866 mysql:5.7.28 mysql5.7.28_3306 0.0.0.0:3306->3306/tcp, 33060/tcp 2 minutes ago Up 2 minutes
MySQL服務(wù)測試
驗證端口
[root@localhost ~]# netstat -an|egrep '3306|3307'tcp6 0 0 :::3306 :::* LISTEN
tcp6 0 0 :::3307 :::* LISTEN
可以看到,3306和3307端口已經(jīng)開啟。
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 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 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數(shù)據(jù)庫服務(wù)已經(jīng)搭建好了,整個搭建都不到1分鐘。