1.master/slave hosts檔案
root login
hosts(master/slave)
127.0.0.1 localhost
10.8.1.5 mdb01
10.8.1.6 sdb01
cp soft/mysql-5.7.9/support-files/my-default.cnf /etc/my.cnf
2.master server-id
vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin(DATADIR/mysql-bin)
server-id=1
3.Slave server-id
server-id=2
4.master 複制使用者,鎖表禁止插入
mysql> CREATE USER 'repl'@'%' IDENTIFIED BY 'repl';
Query OK, 0 rows affected (0.18 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
Query OK, 0 rows affected (0.05 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.09 sec)
鎖定表
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.44 sec)
在master一個不同的session
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
| mysql-bin.000006 | 154 | | | |
1 row in set (0.00 sec)
mysql>
5.如果存在同步主從資料
master
mysql> UNLOCK TABLES;
6.Slave 設定日志開始複制位置
mysql> CHANGE MASTER TO
-> MASTER_HOST='mdb01',
-> MASTER_USER='repl',
-> MASTER_PASSWORD='repl',
-> MASTER_LOG_FILE='mysql-bin.000006',
-> MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.09 sec)
7.啟動slave
mysql> START SLAVE;
Query OK, 0 rows affected (0.07 sec)
8.檢查slave狀态
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: mdb01
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 319
Relay_Log_File: sdb01-relay-bin.000003
Relay_Log_Pos: 485
Relay_Master_Log_File: mysql-bin.000006
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: 319
Relay_Log_Space: 692
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: 42ad8740-7e88-11e5-83de-000c29270868
Master_Info_File: /opt/mysql/data/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:
ERROR:
No query specified
配置可能遇到的問題:
主從如果是同一個虛拟機複制生成的兩台auto.cnf檔案中server-uuid相同,Slave_IO_Running: Null 程序可能無法啟動。
error日志提示:
[ERROR] Slave I/O for channel '': Fatal error: The slave I/O thread stops because master and slave hav
e equal MySQL server UUIDs; these UUIDs must be different for replication to work. Error_code: 1593
需要修改其中任何一個MySQL資料庫DataDir目錄下的auto.cnf檔案中修改為不同值,然後停止slave後,重新啟動MySQL服務,然後再啟動Slave便正常運作了。
[auto]
server-uuid=42ad8740-7e88-11e5-83de-000c29270869
9、主從測試
1).master
mysql> create database testdb;
mysql> use testdb;
mysql> create table user (
-> uid int auto_increment,
-> data json,primary key(uid)
-> );
Query OK, 0 rows affected (0.27 sec)
mysql> insert into user values (NULL,'{"name":"David","mail":"[email protected]","address":"Shangahai"}');
Query OK, 1 row affected (0.64 sec)
mysql> insert into user values (NULL,'{"name":"Amy","mail":"[email protected]"}');
Query OK, 1 row affected (0.14 sec)
mysql> select data->"$.name" from user;
+----------------+
| data->"$.name" |
| "David" |
| "Amy" |
2 rows in set (0.16 sec)
2).slave
mysql> show databases;
+--------------------+
| Database |
| information_schema |
| mysql |
| performance_schema |
| sys |
| testdb |
5 rows in set (0.00 sec)
mysql> use testdb
Database changed
mysql> show tables;
+------------------+
| Tables_in_testdb |
| user |
mysql> desc user;
+-------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
| uid | int(11) | NO | PRI | NULL | auto_increment |
| data | json | YES | | NULL | |
2 rows in set (0.01 sec)
mysql> SHOW CREATE TABLE user\G;
Table: user
Create Table: CREATE TABLE `user` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
`data` json DEFAULT NULL,
PRIMARY KEY (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
mysql> select data ->"$.name" from user;
+-----------------+
| data ->"$.name" |
| "David" |
| "Amy" |
2 rows in set (0.03 sec)
10.Slave并行複制配置
Enhanced Multi-Threaded Slave配置,配置後重新開機MySQL伺服器。
說了這麼多,要開啟enhanced multi-threaded slave其實很簡單,隻需根據如下設定:
# slave
slave-parallel-type=LOGICAL_CLOCK
slave-parallel-workers=16
master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log_recovery=ON
并行複制監控
複制的監控依舊可以通過SHOW SLAVE STATUS\G,但是MySQL 5.7在performance_schema架構下多了以下這些中繼資料表,使用者可以更細力度的進行監控:
mysql> show tables like 'replication%';
+---------------------------------------------+
| Tables_in_performance_schema (replication%) |
| replication_applier_configuration |
| replication_applier_status |
| replication_applier_status_by_coordinator |
| replication_applier_status_by_worker |
| replication_connection_configuration |
| replication_connection_status |
| replication_group_member_stats |
| replication_group_members |
8 rows in set (0.00 sec)
slave狀态:
mysql> SHOW SLAVE STATUS\G;
Read_Master_Log_Pos: 1196
Relay_Log_File: sdb01-relay-bin.000005
Relay_Log_Pos: 320
Exec_Master_Log_Pos: 1196
Relay_Log_Space: 527
Master_Info_File: mysql.slave_master_info
5 rows in set (0.01 sec)
mysql> use performance_schema
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
mysql>
參考文獻
http://www.innomysql.net/article/16317.html
本文轉自 pgmia 51CTO部落格,原文連結:http://blog.51cto.com/heyiyi/1708255