節點:
機器 | 編號 | 節點 |
172.16.38.130 | node-01 | master |
172.16.38.131 | node-02 | slave-01 |
172.16.38.132 | node-03 | slave-02 |
每個節點安裝mysql:
docker pull mysql:5.7.19;
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=yunkun920801 -d mysql:5.7.19;
開啓root遠程登陸並設置密碼
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d706ed1d33eb mysql:5.7.19 "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:3306->3306/tcp mysql
[root@localhost ~]# docker exec -ti d706ed1d33eb /bin/bash
root@d706ed1d33eb:/# 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.19 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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> grant all privileges on *.* to root@"%" identified by "yunkun920801" with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
至此,三個節點都安裝好了mysql並且可通過root遠程登錄
容器內安裝vim
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d706ed1d33eb mysql:5.7.19 "docker-entrypoint.s…" 8 minutes ago Up 8 minutes 0.0.0.0:3306->3306/tcp mysql
[root@localhost ~]# docker exec -ti d706ed1d33eb /bin/bash
root@d706ed1d33eb:/# apt-get update
root@d706ed1d33eb:/# apt-get install vim
master
root@d706ed1d33eb:/# vi /etc/mysql/mysql.conf.d/mysqld.cnf
# 設置 server_id,一般設置爲 IP
server_id=130
# 複製過濾:不需要備份的數據庫,不輸出
binlog-ignore-db=mysql
# 開啓二進制日誌功能,可以隨便取,最好有含義
log-bin=mysql-bin
# 爲每個 session 分配的內存,在事務過程中用來存儲二進制日誌的緩存
binlog_cache_size=1M
# 主從複製的格式mixed,statement,row,默認格式是 statement
binlog_format=mixed
# 二進制日誌自動刪除/過期的天數。默認值爲 0,表示不自動刪除。
expire_logs_days=7
# 跳過主從複製中遇到的所有錯誤或指定類型的錯誤,避免 slave 端複製中斷。 ## 如:1062 錯誤是指一些主鍵重複,1032 錯誤是因爲主從數據庫數據不一致
slave_skip_errors=1062
重啓主節點
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d706ed1d33eb mysql:5.7.19 "docker-entrypoint.s…" 29 minutes ago Up 29 minutes 0.0.0.0:3306->3306/tcp mysql
[root@localhost ~]# docker restart d706ed1d33eb
d706ed1d33eb
[root@localhost ~]#
創建授權賬戶
[root@localhost ~]# docker exec -ti d706ed1d33eb /bin/bash
root@d706ed1d33eb:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.19-log MySQL Community Server (GPL)
mysql> grant replication slave on *.* to master@'%' identified by 'yunkun920801';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 591 | | mysql | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
至此主節點已完成
slave
vi /etc/mysql/mysql.conf.d/mysqld.cnf
server_id=131
# 開啓二進制日誌,以備 Slave 作爲其它 Slave 的 Master 時使用
log-bin=mysql-slave1-bin
# 爲每個 session 分配的內存,在事務過程中用來存儲二進制日誌的緩存
binlog_cache_size = 1M
# 主從複製的格式(mixed,statement,row,默認格式是 statement)
binlog_format=mixed
# 二進制日誌自動刪除/過期的天數。默認值爲 0,表示不自動刪除。
expire_logs_days=7
# 跳過主從複製中遇到的所有錯誤或指定類型的錯誤,避免 slave 端複製中斷。 ## 如:1062 錯誤是指一些主鍵重複,1032 錯誤是因爲主從數據庫數據不一致
slave_skip_errors=1062
# relay_log 配置中繼日誌
relay_log=mysql-relay-bin
# log_slave_updates 表示 slave 將複製事件寫進自己的二進制日誌
log_slave_updates=1
# 防止改變數據(除了特殊的線程)
read_only=1
重啓後
root@5f4d8cd5ab47:/# mysql -u root -p
mysql> change master to master_host='172.16.38.130', master_user='master', master_password='yunkun920801', master_port=3306, master_log_file='mysql-bin.000001', master_log_pos=591, master_connect_retry=30;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
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.16.38.130
Master_User: master
Master_Port: 3306
Connect_Retry: 30
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 591
Relay_Log_File: mysql-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: 591
Relay_Log_Space: 527
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: 130
Master_UUID: 25b3ee05-328a-11ea-ade2-0242ac110002
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)
ERROR:
No query specified
如下圖即便配置成功
兩個slave都配置完成可以在master庫建表加數據,會發現從庫自動備份了,結束!