濮阳杆衣贸易有限公司

主頁 > 知識庫 > mysql容器之間的replication配置實例詳解

mysql容器之間的replication配置實例詳解

熱門標簽:云南語音外呼系統(tǒng)平臺 沃克斯電梯外呼線路圖 常州電銷外呼系統(tǒng)一般多少錢 北京人工外呼系統(tǒng)價錢 房產(chǎn)智能外呼系統(tǒng)品牌 地圖標注被騙三百怎么辦 天智外呼系統(tǒng) 福州呼叫中心外呼系統(tǒng)哪家好 400電話鄭州申請

背景

上周公司培訓了MySQL replication, 這個周末打算用所學來實踐操作一下。

Master server:MySQL container mysql_master on NAS

  • NAS server IP: 192.168.1.108
  • mysql_master inner ip: 172.17.0.6

Slave server: MySQK container mysql_slave on Mac mini

  • Mac mini docker host IP: 192.168.1.139
  • mysql_slave inner ip: 172.17.0.2

準備MySQL container

準備mysql_master

創(chuàng)建兩個目錄用來存放MySQL文件

mkdir -p /mnt/md1/disk4/mysql
mkdir -p /mnt/md1/disk4/mysql-files

創(chuàng)建用于測試的master mysql node

[root@TNAS-2664 disk4]# docker run -d -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 --name mysql_master -v /mnt/md1/disk4/mysql:/var/lib/mysql -v /mnt/md1/disk4/mysql-files:/var/lib/mysql-files mysql
3bebf0e21df6d9034ce8275b14ebb1616e11f5e2678b1e084d03c087ed91a72a

查看當前NAS上運行的mysql的container ID

[root@TNAS-2664 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
40db0be51460 mysql "docker-entrypoint..." 44 seconds ago Up 29 seconds 33060/tcp, 0.0.0.0:3307->3306/tcp mysql_master
db5f6a287a21 mautic/mautic "/entrypoint.sh ap..." 2 weeks ago Up 11 days 0.0.0.0:8082->80/tcp mautic
dc1eac509c70 qianliu/mediawikiwithcomposer "docker-php-entryp..." 2 weeks ago Up 11 days 0.0.0.0:8086->80/tcp sarawiki
b5c0a00f5f42 mysql "docker-entrypoint..." 2 weeks ago Up 11 days 0.0.0.0:3306->3306/tcp, 33060/tcp mysql2
911c0a8987ba qianliu/mediawikiwithcomposer "docker-php-entryp..." 2 weeks ago Up 11 days 0.0.0.0:8083->80/tcp qianliuwiki

使用docker cp命令將my.cnf復制到host機器上進行更改

docker cp 40db0be51460:/etc/mysql/my.cnf .

在my.cnf 加入以下配置

server-id = 1
 gtid-mode = ON # (replicated by GTID)
 enforce_gtid_consistency =1 #(replicated by GTID) 
 log-bin = master-log
 binlog_format = mixed
 expire-logs-days = 14
 sync-binlog = 1
 log-bin-trust-function-creators= 1
 
 # MASTER DB # 
 binlog-ignore-db = mysql,information_schema,performance_schema,sys
 auto-increment-increment = 2
 auto-increment-offset = 1
 
 # SLAVE DB #
 replicate-ignore-db = mysql,information_schema,performance_schema,sys
 relay_log = relay-log
 log-slave-updates = ON

將my.cnf 用docker cp 復制到mysql_master 容器中

docker cp my.cnf 40db0be51460:/etc/mysql/

進入mysql_slave,發(fā)現(xiàn)my.cnf因為權(quán)限文件 is ignored,這將導致剛剛寫入到my.cnf的配置無法生效

[root@TNAS-2664 ~]# docker exec -it mysql_master /bin/bash
root@40db0be51460:/# mysql -uroot -p123456
mysql: [Warning] World-writable config file '/etc/mysql/my.cnf' is ignored.
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.

更改my.cnf的權(quán)限為664

root@40db0be51460:/# chmod 644 /etc/mysql/my.cnf
root@40db0be51460:/# exit

重啟mysql_slave以使my.cnf生效

[root@TNAS-2664 ~]# docker restart mysql_master

9.進入mysql_master查看master status

mysql> show master status;
+-------------------+----------+--------------+-------------------------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB  | Executed_Gtid_Set |
+-------------------+----------+--------------+-------------------------------------------------+-------------------+
| master-log.000001 | 156 | | mysql,information_schema,performance_schema,sys |  |
+-------------------+----------+--------------+-------------------------------------------------+-------------------+
1 row in set (0.00 sec)

mysql> exit

準備mysql_slave 容器

在Mac mini上創(chuàng)建兩個目錄用來存放MySQL文件

mkdir -p /Volumes/MacintoshHDD_Data/mysql_slave_db/mysql
mkdir -p /Volumes/MacintoshHDD_Data/mysql_slave_db/mysql-files

創(chuàng)建用于測試的mysql_slave container

/Volumes/MacintoshHDD_Data/mysql_slave_db  docker run -d -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 --name mysql_slave -v /Volumes/MacintoshHDD_Data/mysql_slave_db/mysql:/var/lib/mysql -v /Volumes/MacintoshHDD_Data/mysql_slave_db/mysql-files:/var/lib/mysql-files mysql

查看當前macmini上運行的mysql_slave的container ID

/Volumes/MacintoshHDD_Data/mysql_slave_db  docker ps
CONTAINER ID IMAGE  COMMAND   CREATED  STATUS  PORTS    NAMES
8623ac99e5d4 mysql  "docker-entrypoint.s…" 5 seconds ago Up 4 seconds 33060/tcp, 0.0.0.0:3307->3306/tcp mysql_slave

使用docker cp命令將my.cnf復制到host機器上進行更改

docker cp 8623ac99e5d4:/etc/mysql/my.cnf .

在my.cnf 加入以下配置

server-id   = 2
 gtid-mode   = ON
 enforce_gtid_consistency =1
 log-bin   = slave-log
 binlog_format   = mixed
 expire-logs-days  = 14
 sync-binlog   = 1
 log-bin-trust-function-creators= 1
 
 # MASTER DB # 
 binlog-ignore-db  = mysql,information_schema,performance_schema,sys
 auto-increment-increment = 2
 auto-increment-offset  = 2
 
 # SLAVE DB #
 replicate-ignore-db  = mysql,information_schema,performance_schema,sys
 relay_log   = relay-log
 log-slave-updates  = ON

將my.cnf 用docker cp 復制到mysql_master 容器中

docker cp my.cnf 8623ac99e5d4:/etc/mysql/

重啟mysql_slave以使my.cnf生效

docker restart mysql_slave

進入mysql_slave查看master status

mysql> show master status;
+------------------+----------+--------------+-------------------------------------------------+------------------------------------------+
| File  | Position | Binlog_Do_DB | Binlog_Ignore_DB    | Executed_Gtid_Set   |
+------------------+----------+--------------+-------------------------------------------------+------------------------------------------+
| slave-log.000001 | 1460 |  | mysql,information_schema,performance_schema,sys | f102ae13-5341-11eb-a578-0242ac110002:1-5 |
+------------------+----------+--------------+-------------------------------------------------+------------------------------------------+
1 row in set (0.00 sec)

準備用于復制的mysql用戶

準備mysql_master中用來復制的mysql user

mysql> CREATE USER 'slave'@'192.168.1.139' IDENTIFIED BY 'slave';
Query OK, 0 rows affected (0.59 sec)

mysql> CREATE USER 'slave'@'172.17.0.2' IDENTIFIED BY 'slave';
Query OK, 0 rows affected (0.60 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'192.168.1.139';
Query OK, 0 rows affected (0.19 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'172.17.0.2';
Query OK, 0 rows affected (0.19 sec)j

mysql> flush privileges;
Query OK, 0 rows affected (0.10 sec)

mysql> exit

準備mysql_slave中用來復制的mysql user

mysql> CREATE USER 'slave'@'192.168.1.108' IDENTIFIED BY 'slave';
Query OK, 0 rows affected (0.02 sec)

mysql> CREATE USER 'slave'@'172.17.0.6' IDENTIFIED BY 'slave';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'192.168.1.108';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'172.17.0.6';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql>

replication配置

配置mysql_master

mysql> change master to master_host='192.168.1.139',master_user='slave',master_password='slave',master_log_file='slave-log.000001',master_port=3307, master_log_pos=1460;
Query OK, 0 rows affected, 2 warnings (1.17 sec)

mysql> change master to master_host='192.168.1.139',master_user='slave',master_password='slave',master_auto_position=1,get_master_public_key=1;
Query OK, 0 rows affected, 2 warnings (0.45 sec)

配置mysql_slave

mysql> change master to master_host='192.168.1.108',master_user='slave',master_password='slave',master_log_file='master-log.000001',master_port=3307, master_log_pos=156;
Query OK, 0 rows affected, 2 warnings (0.15 sec)

mysql> change master to master_host='192.168.1.108',master_user='slave',master_password='slave',master_auto_position=1,get_master_public_key=1;
Query OK, 0 rows affected, 2 warnings (0.14 sec)

開啟復制

mysql_master上start slave
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

查看slave status,發(fā)現(xiàn)并沒有成功啟動復制。出現(xiàn)錯誤 Access denied for user 'slave'@'172.17.0.1' (using password: YES) 需要到mysql_slave上創(chuàng)建響應的用戶和權(quán)限,

mysql> show slave status \G;
*************************** 1. row ***************************
  Slave_IO_State: Connecting to master
   Master_Host: 192.168.1.139
   Master_User: slave
   Master_Port: 3307
  Connect_Retry: 60
  Master_Log_File:
  Read_Master_Log_Pos: 4
  Relay_Log_File: relay-log.000001
  Relay_Log_Pos: 4
 Relay_Master_Log_File:
  Slave_IO_Running: Connecting
  Slave_SQL_Running: Yes
  Replicate_Do_DB:
  Replicate_Ignore_DB: mysql,information_schema,performance_schema,sys
  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: 0
  Relay_Log_Space: 156
  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: 1045
  Last_IO_Error: error connecting to master 'slave@192.168.1.139:3307' - retry-time: 60 retries: 2 message: Access denied for user 'slave'@'172.17.0.1' (using password: YES)
  Last_SQL_Errno: 0
  Last_SQL_Error:
 Replicate_Ignore_Server_Ids:
  Master_Server_Id: 0
   Master_UUID:
  Master_Info_File: mysql.slave_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: 210110 13:02:12
 Last_SQL_Error_Timestamp:
  Master_SSL_Crl:
  Master_SSL_Crlpath:
  Retrieved_Gtid_Set:
  Executed_Gtid_Set:
  Auto_Position: 1
  Replicate_Rewrite_DB:
   Channel_Name:
  Master_TLS_Version:
 Master_public_key_path:
 Get_master_public_key: 1
  Network_Namespace:
1 row in set (0.01 sec)

ERROR:
No query specified

mysql> exit

mysql_slave 啟動slave并查看slave status發(fā)現(xiàn)一切正常

mysql> show slave status;
mysql> show slave status\G;
*************************** 1. row ***************************
  Slave_IO_State: Waiting for master to send event
   Master_Host: 192.168.1.108
   Master_User: slave
   Master_Port: 3307
  Connect_Retry: 60
  Master_Log_File: master-log.000001
  Read_Master_Log_Pos: 156
  Relay_Log_File: relay-log.000002
  Relay_Log_Pos: 373
 Relay_Master_Log_File: master-log.000001
  Slave_IO_Running: Yes
  Slave_SQL_Running: Yes
  Replicate_Do_DB:
  Replicate_Ignore_DB: mysql,information_schema,performance_schema,sys
  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: 156
  Relay_Log_Space: 576
  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: 9627309d-5341-11eb-aaa3-0242ac110006
  Master_Info_File: mysql.slave_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: f102ae13-5341-11eb-a578-0242ac110002:1-5
  Auto_Position: 1
  Replicate_Rewrite_DB:
   Channel_Name:
  Master_TLS_Version:
 Master_public_key_path:
 Get_master_public_key: 1
  Network_Namespace:
1 row in set, 1 warning (0.01 sec)

ERROR:
No query specified

mysql_slave 上創(chuàng)建用戶

mysql> CREATE USER 'slave'@'172.17.0.1' IDENTIFIED BY 'slave';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'172.17.0.1';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

再次查看mysql_master上的slave status,一切正常

mysql> show slave status \G;
*************************** 1. row ***************************
  Slave_IO_State: Waiting for master to send event
   Master_Host: 192.168.1.139
   Master_User: slave
   Master_Port: 3307
  Connect_Retry: 60
  Master_Log_File: slave-log.000001
  Read_Master_Log_Pos: 2022
  Relay_Log_File: relay-log.000002
  Relay_Log_Pos: 2237
 Relay_Master_Log_File: slave-log.000001
  Slave_IO_Running: Yes
  Slave_SQL_Running: Yes
  Replicate_Do_DB:
  Replicate_Ignore_DB: mysql,information_schema,performance_schema,sys
  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: 2022
  Relay_Log_Space: 2440
  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: 2
   Master_UUID: f102ae13-5341-11eb-a578-0242ac110002
  Master_Info_File: mysql.slave_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: f102ae13-5341-11eb-a578-0242ac110002:1-7
  Executed_Gtid_Set: f102ae13-5341-11eb-a578-0242ac110002:1-7
  Auto_Position: 1
  Replicate_Rewrite_DB:
   Channel_Name:
  Master_TLS_Version:
 Master_public_key_path:
 Get_master_public_key: 1
  Network_Namespace:
1 row in set (0.01 sec)

ERROR:
No query specified

測試復制
mysql_slave上創(chuàng)建test_db_slave

mysql> CREATE DATABASE test_db_slave;
Query OK, 1 row affected (0.01 sec)

mysql_master上創(chuàng)建test_db_master

mysql> CREATE DATABASE test_db_master;
Query OK, 1 row affected (0.24 sec)

查看mysql_slave上的databases

mysql> show databases;
+--------------------+
| Database  |
+--------------------+
| information_schema |
| mysql  |
| performance_schema |
| sys  |
| test_db_master |
| test_db_slave |
+--------------------+
6 rows in set (0.00 sec)

mysql>

查看mysql_master上的databases

mysql> show databases;
+--------------------+
| Database  |
+--------------------+
| information_schema |
| mysql  |
| performance_schema |
| sys  |
| test_db_master |
| test_db_slave |
+--------------------+
6 rows in set (0.00 sec)

mysql>

至此,mysql replication配置完畢。

到此這篇關(guān)于mysql容器之間的replication配置的文章就介紹到這了,更多相關(guān)mysql容器的replication配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Docker容器啟動時初始化Mysql數(shù)據(jù)庫的方法
  • Docker容器中Mysql數(shù)據(jù)的導入/導出詳解
  • 詳解通過Docker搭建Mysql容器+Tomcat容器連接環(huán)境
  • Docker創(chuàng)建MySQL容器的方法
  • Docker創(chuàng)建Mysql容器的簡單步驟

標簽:鹽城 拉薩 珠海 黔東 徐州 沈陽 沈陽 移動

巨人網(wǎng)絡通訊聲明:本文標題《mysql容器之間的replication配置實例詳解》,本文關(guān)鍵詞  mysql,容器,之,間的,replication,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《mysql容器之間的replication配置實例詳解》相關(guān)的同類信息!
  • 本頁收集關(guān)于mysql容器之間的replication配置實例詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    河南省| 罗山县| 金华市| 花莲市| 宁海县| 清水河县| 修武县| 扶余县| 城市| 高要市| 灵璧县| 镇赉县| 泸州市| 土默特右旗| 盖州市| 年辖:市辖区| 保靖县| 长白| 南陵县| 济阳县| 特克斯县| 海宁市| 黑龙江省| 辉县市| 盈江县| 武邑县| 乌审旗| 舟曲县| 绥阳县| 仁怀市| 大理市| 米易县| 梁山县| 财经| 都昌县| 太谷县| 屯昌县| 陈巴尔虎旗| 于田县| 资源县| 卓尼县|