一、單實(shí)例安裝
1.1 下載鏡像
使用docker search mysql
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/mysql MySQL is a widely used, open-source relati... 8178 [OK]
docker.io docker.io/mariadb MariaDB is a community-developed fork of M... 2785 [OK]
//...
選擇第一個(gè)鏡像docker.io/mysql,可以自己指定版本號(hào)掂僵,這里使用的是5.7航厚。使用如下命令下載mysql鏡像:
# docker pull docker.io/mysql:5.7
Trying to pull repository docker.io/library/mysql ...
5.7: Pulling from docker.io/library/mysql
743f2d6c1f65: Pull complete
3f0c413ee255: Pull complete
aef1ef8f1aac: Pull complete
f9ee573e34cb: Pull complete
3f237e01f153: Pull complete
f9da32e8682a: Pull complete
4b8da52fb357: Pull complete
6f38e9cfd49b: Pull complete
9f4834b3f44f: Pull complete
af631d92fdba: Pull complete
0e771ddab25c: Pull complete
Digest: sha256:196fe3e00d68b2417a8cf13482bdab1fcc2b32cf7c7575d0906c700688b352b4
Status: Downloaded newer image for docker.io/mysql:5.7
1.2 啟動(dòng)mysql
啟動(dòng)mysql除了端口映射之外,還需要使用-e MYSQL_ROOT_PASSWORD
指定一下root密碼锰蓬,命令如下:
# docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
d16116181b61544db1681dde4f07fc79774f08144044617b20a19f5d46128071
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d16116181b61 mysql:5.7 "docker-entrypoint..." 3 seconds ago Up 3 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
使用docker ps
可以看到mysql已經(jīng)正常啟動(dòng)起來了幔睬。可以用命令行或者mysql客戶端連接上去看看芹扭。
如果mysql是安裝在遠(yuǎn)程服務(wù)器麻顶,使用本地的mysql客戶端連接不上的話,一般有兩個(gè)原因:
- 服務(wù)器端口號(hào)未開放
- mysql未開啟遠(yuǎn)程登錄
解決方法:
對(duì)于原因1舱卡,檢查下服務(wù)器的防火墻等相關(guān)配置辅肾。
對(duì)于原因2,需在服務(wù)器上登錄mysql并開啟遠(yuǎn)程登錄轮锥。
開啟遠(yuǎn)程登錄方法如下:
在服務(wù)器上通過docker命令進(jìn)入mysql實(shí)例內(nèi)部并登錄mysql
# docker exec -it d16116181b61 /bin/bash
root@d16116181b61:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26 MySQL Community Server (GPL)
// ....
mysql>
開啟mysql遠(yuǎn)程登錄
這一步只需要在mysql的user表中設(shè)置一條host='%'并且user='root'的記錄即可矫钓。
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select host,user from user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| % | root |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+---------------+
4 rows in set (0.00 sec)
檢查下mysql數(shù)據(jù)庫(kù)的user表中是否有host='%'并且user='root'的記錄,用Docker安裝的mysql默認(rèn)是有的舍杜。
如果沒有新娜,可以執(zhí)行如下的更新語句:
update set host = '%' where host = 'localhost' and user = 'root';
flush privileges;
user表中默認(rèn)都會(huì)有一條user='root'并且host='localhost'的記錄,可以直接將其host改為%既绩。
二概龄、主從安裝
2.1 主服務(wù)器安裝
2.1.1 配置主服務(wù)器
首先為主服務(wù)器創(chuàng)建一個(gè)文件夾,例如/home/mysql1饲握,用來存放主服務(wù)器的配置文件和數(shù)據(jù)文件私杜。
在mysql1下創(chuàng)建conf/my.cnf,文件內(nèi)容如下互拾。
# Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# 主數(shù)據(jù)庫(kù)端ID號(hào)
server_id = 1
# 開啟二進(jìn)制日志
log-bin = mysql-bin
# 需要復(fù)制的數(shù)據(jù)庫(kù)名歪今,如果復(fù)制多個(gè)數(shù)據(jù)庫(kù),重復(fù)設(shè)置這個(gè)選項(xiàng)即可
# binlog-do-db = db
# 復(fù)制時(shí)需要忽略的數(shù)據(jù)庫(kù)
binlog-ignore-db=mysql
# 為每個(gè)session分配的內(nèi)存颜矿,在事務(wù)過程中用來存儲(chǔ)二進(jìn)制日志的緩存
binlog_cache_size=1M
# 主從復(fù)制的格式(mixed, statement, row, 默認(rèn)格式是statement)
binlog_format=mixed
# 將從服務(wù)器從主服務(wù)器收到的更新記入到從服務(wù)器自己的二進(jìn)制日志文件中
log-slave-updates
# 控制binlog的寫入頻率。每執(zhí)行多少次事務(wù)寫入一次(這個(gè)參數(shù)性能消耗很大嫉晶,但可減小MySQL崩潰造成的損失)
sync_binlog = 1
# 這個(gè)參數(shù)一般用在主主同步中骑疆,用來錯(cuò)開自增值, 防止鍵值沖突
auto_increment_offset = 1
# 這個(gè)參數(shù)一般用在主主同步中,用來錯(cuò)開自增值, 防止鍵值沖突
auto_increment_increment = 1
# 二進(jìn)制日志自動(dòng)刪除的天數(shù)替废,默認(rèn)值為0,表示“沒有自動(dòng)刪除”箍铭,啟動(dòng)時(shí)和二進(jìn)制日志循環(huán)時(shí)可能刪除
expire_logs_days = 7
# 將函數(shù)復(fù)制到slave
log_bin_trust_function_creators = 1
前面都是基礎(chǔ)配置,從server-id開始是主服務(wù)器的配置椎镣。
2.1.2 啟動(dòng)主服務(wù)器
使用docker run
來啟動(dòng)一個(gè)mysql實(shí)例作為主服務(wù)器
# docker run --name mysql1 -v /home/mysql1/conf/my.cnf:/etc/mysql/my.cnf -v /home/mysql1/data:/var/lib/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci
8a54b5ee92d2137b96879eec77c79c20507346d3c57cc34fbddbe3f6c6bcc323
2.1.3 創(chuàng)建復(fù)制賬號(hào)
登錄mysql诈火,為從服務(wù)器創(chuàng)建一個(gè)賬號(hào),并賦予其相應(yīng)權(quán)限状答。
root@8a54b5ee92d2:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26-log 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> create user 'slave'@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave, replication client on *.* to 'slave'@'%';
Query OK, 0 rows affected (0.00 sec)
2.1.4 查看master狀態(tài)
使用show master status
查看當(dāng)前的狀態(tài)冷守,并記錄下File和Position刀崖,后面從服務(wù)器要用。
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 617 | | mysql | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
2.1.5 獲取master IP
使用docker inspect
查看主服務(wù)器IP拍摇,后面從服務(wù)器要用亮钦。
# docker inspect --format='{{.NetworkSettings.IPAddress}}' 4264f76d7003
172.17.0.2
2.2 從實(shí)例安裝
2.2.1 配置從服務(wù)器
同樣也為從服務(wù)器創(chuàng)建一個(gè)文件夾,例如/home/mysql2充活,用來存放從服務(wù)器的配置文件和數(shù)據(jù)文件蜂莉。
在mysql2下創(chuàng)建conf/my.cnf,文件內(nèi)容如下:
# Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# 從數(shù)據(jù)庫(kù)端ID號(hào)
server_id = 2
#開啟二進(jìn)制日志
log-bin = mysql-bin
# 需要復(fù)制的數(shù)據(jù)庫(kù)名混卵,如果復(fù)制多個(gè)數(shù)據(jù)庫(kù)映穗,重復(fù)設(shè)置這個(gè)選項(xiàng)即可
# binlog-do-db = db
# 復(fù)制時(shí)需要忽略的數(shù)據(jù)庫(kù)
binlog-ignore-db = mysql
# 為每個(gè)session分配的內(nèi)存
binlog_cache_size = 1M
# 主從復(fù)制的格式(mixed、statement幕随、row男公,默認(rèn)是statement)
binlog_format = mixed
# 將從服務(wù)器從主服務(wù)器收到的更新記入到從服務(wù)器自己的二進(jìn)制日志文件中
log_slave_updates = 1
# 這個(gè)參數(shù)一般用在主主同步中,用來錯(cuò)開自增值, 防止鍵值沖突
# auto_increment_offset = 1
# 這個(gè)參數(shù)一般用在主主同步中合陵,用來錯(cuò)開自增值, 防止鍵值沖突
# auto_increment_increment = 1
# 二進(jìn)制日志自動(dòng)刪除的天數(shù)枢赔,默認(rèn)值為0,表示“沒有自動(dòng)刪除”,啟動(dòng)時(shí)和二進(jìn)制日志循環(huán)時(shí)可能刪除
expire_logs_days = 7
#將函數(shù)復(fù)制到slave
log_bin_trust_function_creators = 1
# relay_log 配置中繼日志
relay_log = mysql-relay-bin
# 防止改變數(shù)據(jù)
read_only = 1
2.2.2 啟動(dòng)從服務(wù)器
使用docker run
來啟動(dòng)一個(gè)mysql實(shí)例作為從服務(wù)器拥知。
# docker run --name mysql2 -v /home/mysql2/conf/my.cnf:/etc/mysql/my.cnf -v /home/mysql2/data:/var/lib/mysql -p 3308:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci
ceb421c503a6533ead15708a92534c47f544410a32dd5d0f1d8bdf036c9e215d
2.2.3 為從服務(wù)器指定主服務(wù)器
mysql> change master to master_host='172.17.0.2', master_user='slave', master_password='123456', master_port=3306, master_log_file='mysql-bin.000002', master_log_pos=617, master_connect_retry=30;
Query OK, 0 rows affected, 2 warnings (0.03 sec)
說明:
- master_host: 主服務(wù)器ip踏拜,對(duì)應(yīng)上面2.1.5中獲取到的ip
- master_user: 復(fù)制用戶,對(duì)應(yīng)上面2.1.3中創(chuàng)建的用戶
- master_password: 復(fù)制用戶密碼低剔,對(duì)應(yīng)上面2.1.3中的創(chuàng)建的用戶密碼
- master_log_file: 日志文件名速梗,對(duì)應(yīng)上面2.1.4中的File
- master_log_pos: 日志文件位置,對(duì)應(yīng)上面2.1.4中的Position
2.2.4 查看從服務(wù)器狀態(tài)
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 172.17.0.2
Master_User: slave
Master_Port: 3306
Connect_Retry: 30
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 617
Relay_Log_File: mysql-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: No
Slave_SQL_Running: No
// 省略
1 row in set (0.00 sec)
ERROR:
No query specified
Slave_IO_Running和Slave_SQL_Running都是NO襟齿,表明slave還沒開始復(fù)制過程
2.2.5 開啟主從同步
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.17.0.2
Master_User: slave
Master_Port: 3306
Connect_Retry: 30
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 617
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
// 省略
1 row in set (0.00 sec)
到這里主從配置就已經(jīng)好了姻锁,接下來就可以進(jìn)行測(cè)試了。先在主服務(wù)器中進(jìn)行操作猜欺,然后看從服務(wù)器有沒有自動(dòng)同步主服務(wù)器的數(shù)據(jù)位隶。
注: 對(duì)于同一個(gè)mysql實(shí)例,先使用docker stop
再使用docker start
开皿,即時(shí)沒加-v參數(shù)指定數(shù)據(jù)文件涧黄,之前的數(shù)據(jù)也不會(huì)丟失。如果啟動(dòng)了一個(gè)新實(shí)例赋荆,同時(shí)還想用已有的實(shí)例的數(shù)據(jù)笋妥,則可以先使用docker cp mysql:/var/lib/mysql /home/tmp/mysql/data
將已有實(shí)例的數(shù)據(jù)拷貝出來。然后在啟動(dòng)新實(shí)例時(shí)使用-v參數(shù)指定該數(shù)據(jù)文件窄潭。