基本环境
Master | Slave | |
MySQL版本 | MySQL-5.7.16-X86_64 | MySQL-5.7.16-X86_64 |
IP | 192.168.56.156 | 192.168.56.157 |
Port | 3306 | 3306 |
数据库环境的部署:
两边安装好相同的数据库软件,初始化,可以启动起来。
检查事项:
两边防火墙是否开启,对应的端口号是否通(可以通过telnet的方式,或者远程登录的方式验证)
主库配置:
开启 log_bin 、server_id、gtid_mode、enforce_gtid_consistency、skip-slave-start、log_slave_updates 参数。
server_id=15633006
gtid_mode=on
enforce_gtid_consistency=1
skip-slave-start=1
log_slave_updates=1
另外需要注意的是:
binlog_format 控制二进制文件格式,生成环境建议使用row格式。
log_bin_basename 控制二进制文件的路径和命名规则。
server_id建议使用:端口号+IP最后一位的形式。
主库创建复制账号:
create user 'repl'@'192.168.56.%' identified by 'oracle';
grant replication slave on *.* to 'repl'@'192.168.56.%';
flush privileges;
创建完之后,在另外一台服务器测试是否能连接上。
mysql -h 192.168.56.156 -urepl -p
从库配置:
开启 log_bin 、server_id、gtid_mode、enforce_gtid_consistency、skip-slave-start、log_slave_updates 参数。
server_id=15733006
gtid_mode=on
enforce_gtid_consistency=1
skip-slave-start=1
log_slave_updates=1
注意:主库和从库的server_id不能一样。
使用mysqldump备份数据,然后scp到从库上:
mysqldump -utest -poracle --single-transaction --master-data=2 -S /data/mysql/3306/tmp/3306.sock --databases testdb testdb2>testdb1-2.sql
在从库上恢复:
source /root/testdb1-2.sql
mysql> show master status G;
*************************** 1. row ***************************
File: mysql-bin.000007
Position: 194
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: b16b53da-c1a1-11e6-915f-000c29355816:1-32
1 row in set (0.00 sec)
在从库上执行:
change master to master_host='192.168.56.156',master_user='repl',master_password='oracle',master_port=3306,MASTER_AUTO_POSITION = 1;
在从库上查看状态:
mysql> show slave statusG;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.56.156
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File:
Read_Master_Log_Pos: 4
Relay_Log_File: relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File:
Slave_IO_Running: No
Slave_SQL_Running: No
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: 0
Relay_Log_Space: 154
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: NULL
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: 0
Master_UUID:
Master_Info_File: /data/mysql/3306/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
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: 6350cf26-c1a2-11e6-a787-000c299e3b2b:1-12,
b16b53da-c1a1-11e6-915f-000c29355816:22-32
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
在从库上执行:
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
再次在从库上查看状态:
mysql> show slave status G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.56.156
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 194
Relay_Log_File: relay-bin.000012
Relay_Log_Pos: 407
Relay_Master_Log_File: mysql-bin.000007
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: 194
Relay_Log_Space: 861
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: 1563306
Master_UUID: b16b53da-c1a1-11e6-915f-000c29355816
Master_Info_File: /data/mysql/3306/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: b16b53da-c1a1-11e6-915f-000c29355816:1-21
Executed_Gtid_Set: 6350cf26-c1a2-11e6-a787-000c299e3b2b:1-18,
b16b53da-c1a1-11e6-915f-000c29355816:1-32
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
关注上面 Slave_IO_Running 和 Slave_SQL_Running都是 Yes,表示已经连接上了master,说明已经成功。