把MySQL放進(jìn)Docker,總共需要幾步?本次就通過社區(qū)版容器安裝2個(gè)mysql實(shí)例,看一下部署有多簡單。
1、 安裝docker
操作系統(tǒng)我使用的Centos7 x64系統(tǒng),而Docker 目前看僅CentOS 7 及以上版本。本次采用Docker 倉庫進(jìn)行安裝 ,具體步驟如下:
1.1設(shè)置倉庫
因本機(jī)首次安裝 Docker,所以需要先設(shè)置 Docker 倉庫,以后的安裝可以直接從倉庫安裝。
/* 安裝所需的軟件包 */
yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
使用以下命令來設(shè)置穩(wěn)定的倉庫
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
這一步有的包下載可能比較慢,要耐心等待,如果失敗再重新執(zhí)行幾遍。
如果有同學(xué)配置了多個(gè) Docker 倉庫,而且在 yum install 或 yum update 命令中未指定版本時(shí),則會(huì)安裝或更新最新版本的包,如果對(duì)穩(wěn)定性 或版本有要求,則安裝時(shí)一定要指定特定版本。
1.3 啟動(dòng)docker
經(jīng)過上述安裝后,啟動(dòng)docker服務(wù)即可
1.4 測(cè)試docker 部署是否成功
可以運(yùn)行經(jīng)典的hello-world 來測(cè)試

可見docker已部署成功并可以正常運(yùn)行。
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,不指定版本默認(rèn)拉取最新版。
[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

注: 拉取鏡像的時(shí)候容易出現(xiàn)如下超時(shí)錯(cuò)誤。
Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled (Client.Timeout exceeded while awaiting headers)
建議修改國內(nèi)鏡像地址,例如修改為網(wǎng)易或阿里云的鏡像地址(我采用的是個(gè)人阿里鏡像的方式, 下載速度很理想,基本一分鐘內(nèi)下載完畢)
修改源的方法:
vim /etc/docker/daemon.json
/* 添加如下內(nèi)容 */
{
"registry-mirrors": ["http://hub-mirror.c.163.com","https://registry.docker-cn.com"]
使用阿里云鏡像需要自己登錄到阿里云,配置后復(fù)制自己的地址再使用,需要的小伙伴可以聯(lián)系我獲取指引。
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 運(yùn)行mysql容器
[root@c7_2 local]# docker run -di --name=mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=Admin@123 mysql
9f6668b5d0292b30308cfc5c6a6b88a34c4d62d9e5c70dff9bfce9f090117968
其中主要參數(shù)說明如下:
- --name 后面配置容器名
- -p代表端口映射, 格式為 宿主機(jī)映射端口:容器運(yùn)行端口
- -e代表添加環(huán)境變量 MYSQL_ROOT_PASSWORD 是root用戶的登陸密碼
- 最后的mysql代碼容器鏡像名
啟動(dòng)成功后
2.5 進(jìn)入mysql容器
指定進(jìn)入mysql容器中
[root@c7_2 local]# docker exec -it mysql /bin/bash
root@9f6668b5d029:/#
在容器內(nèi)登錄mysql
root@9f6668b5d029:/# mysql -u root -p'Admin@123'
結(jié)果見如下截圖,可以看到部署的是最新的MySQL8.0.20版本

注: MySQL8.0 用戶的加密組件做了變更,低版本客戶端登錄會(huì)報(bào)錯(cuò)。處理的方式有多種,主要的方式有2種:
- 修改對(duì)應(yīng)用戶的密碼加密方式
- 升級(jí)客戶端或驅(qū)動(dòng)
具體方式可參考 MySQL8.0用戶登錄那些事。
2.6 再部署一個(gè)mysql5.7的容器
上面部署的是最新版mysql8.0.20,想部署5.7版本該如何部署?其實(shí)就是拉取鏡像的時(shí)候指定選擇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

運(yùn)行mysql5.7的docker
docker run -di --name=mysql5.7 -p 3307:3306 -e MYSQL_ROOT_PASSWORD=Admin@123 centos/mysql-57-centos7

不進(jìn)入容器,在本地或其他機(jī)器上登錄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 社區(qū)版的5.7.24

2.7 查看正在運(yùn)行的docker
查看一臺(tái)機(jī)器上運(yùn)行的docker信息可以通過 docker ps 命令查看

本地端口信息如下

3、結(jié)語
將MySQL放進(jìn)docker主要就這幾步。不過其中修改數(shù)據(jù)庫配置文件等相關(guān)內(nèi)容本次來不及細(xì)說,有興趣的同學(xué)可以自行測(cè)試,相對(duì)也必將簡單,可以在啟動(dòng)的時(shí)候指定。