Docker MySQL主從服務部署
一、MySQL安裝
1、鏡像搜索下載
docker search mysql
docker pull mysql
2、部署Master
創建master的宿主機映射目錄
mkdir -p /opt/module/mysql/mysql-master/conf
mkdir -p /opt/module/mysql/mysql-master/data
運行鏡像
docker run --name mysql-master \
-e MYSQL_ROOT_PASSWORD=root \
-v /opt/module/mysql/mysql-master/conf:/etc/mysql/conf.d \
-v /opt/module/mysql/mysql-master/data:/var/lib/mysql \
-p 33061:3306 \
-d mysql:5.7
3、部署Slave
創建master的宿主機映射目錄
mkdir -p /opt/module/mysql/mysql-slave/conf
mkdir -p /opt/module/mysql/mysql-slave/data
運行鏡像
docker run --name mysql-slave \
-e MYSQL_ROOT_PASSWORD=root \
-v /opt/module/mysql/mysql-slave/conf:/etc/mysql/conf.d \
-v /opt/module/mysql/mysql-slave/data:/var/lib/mysql \
-p 33062:3306 \
-d mysql:5.7
4、配置基於bin-log的主從複製
分別在/opt/module/mysql/mysql-master/conf
和/opt/module/mysql/mysql-slave/conf
下創建master.cnf
和slave.cnf
文件。
- 修改
master.cnf
文件[mysqld] #開啓bin-log功能 log-bin=mysql-bin #集羣中mysql服務的唯一Id server-id=1
- 修改
slave.cnf
文件[mysqld] #開啓bin-log功能 log-bin=mysql-bin #集羣中mysql服務的唯一Id server-id=2
5、配置Master服務
使用命令:docker exec -it mysql-master bash
進入到Master服務中
創建用於同步的用戶:
root@22cdf8ce43b9:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.27-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 'syncBack'@'172.17.0.%' identified by 'syncBack';
Query OK, 0 rows affected (0.02 sec)
mysql> grant replication slave on *.* to 'syncBack'@'172.17.0.%' identified by 'syncBack';
Query OK, 0 rows affected, 1 warning (0.04 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.04 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 2967 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>
6、配置Slave服務
使用命令:docker exec -it mysql-slave bash
進入到Slave服務中
root@8db4ca18e835:/# 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.27-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> change master to master_host='172.17.0.4',
-> master_user='syncBack',
-> master_password='syncBack',
-> master_port=3306,
-> master_log_file='mysql-bin.000001',
-> master_log_pos=2967,
-> master_connect_retry=30;
Query OK, 0 rows affected, 2 warnings (0.31 sec)
mysql> start slave;
Query OK, 0 rows affected (0.04 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.17.0.4
Master_User: syncBack
Master_Port: 3306
Connect_Retry: 30
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 2967
Relay_Log_File: 8db4ca18e835-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 2967
Relay_Log_Space: 534
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 4b8d5613-e976-11e9-a4ca-0242ac110004
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
mysql>
至此主從配置就好了,測試在主表創建一個數據庫,從表也會出現一個數據庫。