Mysql版本
通過docker啟動的mysql容器
mysql版本
root@1651d1cab219:/# mysql --version
mysql Ver 14.14 Distrib 5.6.19-v1, for Linux (x86_64) using EditLine wrapper
配置Master
Master需要一個唯一性的服務器ID。
一個二進制文件記錄數據的變動。
一個可以讀取二進制文件的mysql賬號。
打開mysql配置文件找到如下內容。
#server-id = 1
log_bin = /etc/mysql/data/mysql-bin.log
expire_logs_days = 2
max_binlog_size = 100M
#binlog_do_db = include_database_name
#binlog_ignore_db = include_database_name
最后把內容改成如下內容
server-id = 1
log_bin = /etc/mysql/data/mysql-bin.log
log_bin_index = /etc/mysql/data/mysql-bin.index
expire_logs_days = 2
max_binlog_size = 100M
binlog_do_db = test
binlog_ignore_db = mysql
重啟mysql 在宿主機器上重啟該mysql容器
重新進入到容器
在mysql的test庫中創建一張測試表
先查看master 二進制位置
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 | 121 | test | mysql | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> reset master;
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 121 | test | mysql | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
說明master配置已經運行
創建 復制賬號
GRANT REPLICATION SLAVE ON *.* TO 'repl_user'@'%' IDENTIFIED BY '';
已經不能在語句上賦值數據庫名稱了,如果需要過濾數據庫需要配置文件的配置項
#binlog_do_db = include_database_name
#binlog_ignore_db = include_database_name
分別表示需要記錄的數據庫和不需要記錄的數據庫選項。
配置Slave
在slave服務器mysql的配置文件中添加
server-id = 2
relay_log_index = /etc/mysql/data/slave-relay-bin.index
relay_log = /etc/mysql/data/slave-relay-bin.log
重啟docker容器
連接 Slave到Master
CHANGE MASTER TO MASTER_HOST='172.17.0.2',MASTER_PORT=3306,MASTER_USER = 'repl_user',MASTER_PASSWORD='';
START SLAVE;
Master服務器添加數據
mysql> use test;
Database changed
mysql> CREATE TABLE `a` (
-> `id` int(11) NOT NULL AUTO_INCREMENT,
-> `name` varchar(16) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
-> PRIMARY KEY (`id`)
-> ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Query OK, 0 rows affected (0.11 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| a |
+----------------+
1 row in set (0.00 sec)
Slave查看同步結果與狀態
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| a |
+----------------+
1 row in set (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: repl_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 401
Relay_Log_File: slave-relay-bin.000002
Relay_Log_Pos: 565
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: 401
Relay_Log_Space: 739
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: 8bb5a52d-5fe2-11e6-841c-0242ac110002
Master_Info_File: /etc/mysql/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
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
1 row in set (0.00 sec)
我們的基本的主從Mysql搭建已經完成。