把MySQL放進Docker,總共需要幾步?本次就通過社區版容器安裝2個mysql實例,看一下部署有多簡單。
1、 安裝docker
操作系統我使用的Centos7 x64系統,而Docker 目前看僅CentOS 7 及以上版本。本次采用Docker 倉庫進行安裝 ,具體步驟如下:
1.1 設置倉庫
因本機首次安裝 Docker,所以需要先設置 Docker 倉庫,以后的安裝可以直接從倉庫安裝。
/* 安裝所需的軟件包 */ yum install -y yum-utils \ device-mapper-persistent-data \ lvm2
使用以下命令來設置穩定的倉庫
yum-config-manager \ > --add-repo \ > https://download.docker.com/linux/centos/docker-ce.repo
1.2 安裝Docker Engine-Community
安裝最新版本的 Docker Engine-Community 和 containerd
yum install docker-ce docker-ce-cli containerd.io
這一步有的包下載可能比較慢,要耐心等待,如果失敗再重新執行幾遍。
如果有同學配置了多個 Docker 倉庫,而且在 yum install 或 yum update 命令中未指定版本時,則會安裝或更新最新版本的包,如果對穩定性 或版本有要求,則安裝時一定要指定特定版本。
1.3 啟動docker
經過上述安裝后,啟動docker服務即可
systemctl start docker
1.4 測試docker 部署是否成功
可以運行經典的hello-world 來測試
可見docker已部署成功並可以正常運行。
2、 部署MySQL
2.1 查看可用的mysql鏡像
[root@c7_2 local]# docker search mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 9453 [OK] mariadb MariaDB is a community-developed fork of MyS… 3415 [OK] mysql/mysql-server Optimized MySQL Server Docker images. Create… 691 [OK] centos/mysql-57-centos7 MySQL 5.7 SQL database server 75 mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 68 centurylink/mysql Image containing mysql. Optimized to be link… 61 [OK] deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK] bitnami/mysql Bitnami MySQL Docker Image 39 [OK] tutum/mysql Base docker image to run a MySQL database se… 35 schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 30 [OK] prom/mysqld-exporter 27 [OK] linuxserver/mysql A Mysql container, brought to you by LinuxSe… 25 centos/mysql-56-centos7 MySQL 5.6 SQL database server 19 circleci/mysql MySQL is a widely used, open-source relation… 19 databack/mysql-backup Back up mysql databases to... anywhere! 17 mysql/mysql-router MySQL Router provides transparent routing be… 15 arey/mysql-client Run a MySQL client from a docker container 14 [OK] openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6 fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 6 [OK] genschsa/mysql-employees MySQL Employee Sample Database 5 [OK] devilbox/mysql Retagged MySQL, MariaDB and PerconaDB offici… 3 ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 2 [OK] jelastic/mysql An image of the MySQL database server mainta… 1 widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl… 0 [OK] monasca/mysql-init A minimal decoupled init container for mysql 0
2.2 部署最新版本mysql
拉取最新版本mysql,不指定版本默認拉取最新版
[root@c7_2 local]# docker pull mysql Using default tag: latest latest: Pulling from library/mysql 54fec2fa59d0: Pull complete bcc6c6145912: Pull complete 951c3d959c9d: Pull complete 05de4d0e206e: Pull complete 319f0394ef42: Pull complete d9185034607b: Pull complete 013a9c64dadc: Pull complete 42f3f7d10903: Pull complete c4a3851d9207: Pull complete 82a1cc65c182: Pull complete a0a6b01efa55: Pull complete bca5ce71f9ea: Pull complete Digest: sha256:61a2a33f4b8b4bc93b7b6b9e65e64044aaec594809f818aeffbff69a893d1944 Status: Downloaded newer image for mysql:latest docker.io/library/mysql:latest
注: 拉取鏡像的時候容易出現如下超時錯誤
Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled (Client.Timeout exceeded while awaiting headers)
建議修改國內鏡像地址,例如修改為網易或阿里雲的鏡像地址(我采用的是個人阿里鏡像的方式, 下載速度很理想,基本一分鍾內下載完畢)
修改源的方法:
vim /etc/docker/daemon.json
/* 添加如下內容 */
{
"registry-mirrors": ["http://hub-mirror.c.163.com","https://registry.docker-cn.com"]
}
使用阿里雲鏡像需要自己登錄到阿里雲,配置后復制自己的地址再使用,需要的小伙伴可以聯系我獲取指引。
2.3 查看已下載的鏡像
[root@c7_2 containers]# docker image ls # 或使用 docker images 查看 REPOSITORY TAG IMAGE ID CREATED SIZE mysql latest a7a67c95e831 7 days ago 541MB hello-world latest bf756fb1ae65 4 months ago 13.3kB
2.4 運行mysql容器
[root@c7_2 local]# docker run -di --name=mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=Admin@123 mysql 9f6668b5d0292b30308cfc5c6a6b88a34c4d62d9e5c70dff9bfce9f090117968
其中主要參數說明如下:
- --name 后面配置容器名
- -p代表端口映射, 格式為 宿主機映射端口:容器運行端口
- -e代表添加環境變量 MYSQL_ROOT_PASSWORD 是root用戶的登陸密碼
- 最后的mysql代碼容器鏡像名
啟動成功后
2.5 進入mysql容器
指定進入mysql容器中
[root@c7_2 local]# docker exec -it mysql /bin/bash
root@9f6668b5d029:/#
在容器內登錄mysql
root@9f6668b5d029:/# mysql -u root -p'Admin@123'
結果見如下截圖,可以看到部署的是最新的MySQL8.0.20版本
注: MySQL8.0 用戶的加密組件做了變更,低版本客戶端登錄會報錯。處理的方式有多種,主要的方式有2種:
- 修改對應用戶的密碼加密方式
- 升級客戶端或驅動
具體方式可參考 MySQL8.0用戶登錄那些事
2.6 再部署一個mysql5.7的容器
上面部署的是最新版mysql8.0.20,想部署5.7版本該如何部署?其實就是拉取鏡像的時候指定選擇MySQL5.7版本的即可。具體步驟如下:
拉取mysql5.7版本鏡像
[root@c7_2 local]# docker pull centos/mysql-57-centos7 Using default tag: latest latest: Pulling from centos/mysql-57-centos7 d8d02d457314: Pull complete a11069b6e245: Pull complete 596303fb1aa3: Pull complete a29499e779a7: Pull complete 17d1a52c2e00: Pull complete ed24591227fe: Pull complete de0ad46e3ed9: Pull complete c62e4a984a9c: Pull complete 01d54c6bda68: Pull complete Digest: sha256:e08ee4d43b7356607685b69bde6335e27cf20c020f345b6c6c59400183882764 Status: Downloaded newer image for centos/mysql-57-centos7:latest docker.io/centos/mysql-57-centos7:latest
運行mysql5.7的docker
docker run -di --name=mysql5.7 -p 3307:3306 -e MYSQL_ROOT_PASSWORD=Admin@123 centos/mysql-57-centos7
不進入容器,在本地或其他機器上登錄mysql5.7
[root@c7_2 local]# /usr/local/mysql5.7/bin/mysql -uroot -p'Admin@123' -P3307 -h 192.168.28.129 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 11 Server version: 5.7.24 MySQL Community Server (GPL) Copyright (c) 2009-2019 Percona LLC and/or its affiliates 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> select version(); +-----------+ | version() | +-----------+ | 5.7.24 | +-----------+ 1 row in set (0.01 sec) mysql>
可以看到 該版本為MySQL 社區版的5.7.24
2.7 查看正在運行的docker
查看一台機器上運行的docker信息可以通過 docker ps 命令查看
本地端口信息如下
3、結語
將MySQL放進docker主要就這幾步。不過其中修改數據庫配置文件等相關內容本次來不及細說,有興趣的同學可以自行測試,相對也必將簡單,可以在啟動的時候指定。
另外,將MySQL放在docker后與本地直接運行之間的有什么差別,各自的優缺點是什么?歡迎大家在技術群里討論。