一 环境信息
主库 121.43.146.141 /3306 主机名 db1
备库 121.43.251.41 /3306 主机名 db2
版本 5.6.14
备注: mysql 安装略,
二 主从搭建
–主,备节点都要创建 Replication 用户
create user ‘test’@’%’ identified by ‘test’;
grant all privileges on . to ‘test’@’%’ identified by ‘test’ with grant option;
GRANT REPLICATION SLAVE ON . TO ‘rep1’@’@’;
从节点
create user ‘rep1’@‘121.43.146.141’ identified by ‘rep1abcd1243d’;
GRANT REPLICATION SLAVE ON . TO ‘rep1’@‘121.43.146.141’;
主库:
server-id=1
log-bin=mysql-bin
binlog-do-db=yile
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
从库
server-id=2
log-bin=mysql-bin
binlog-do-db=yile
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
–导出数据库
导出数据库之前先锁定数据库
flush tables with read lock; #数据库只读锁定命令,防止导出数据库的时候有数据写入
unlock tables; #解除锁定
导出数据库结构及数据:mysqldump -uroot -p yile > /home/yile.sql
导出存储过程及函数 :mysqldump -uroot -p -ntd -R yile> yile.sql
tips:-ntd导出存储过程、-R导出函数
–查询主库状态
root@localhost:db1> show master status\G
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
±-----------------±---------±-------------±--------------------------------------------±------------------+
| mysql-bin.000001 | 120 | yile | mysql,information_schema,performance_schema | |
±-----------------±---------±-------------±--------------------------------------------±------------------+
–从库指向主库
mysql> stop slave; #关闭Slave mysql> change master to master_host=‘172.20.96.5’,master_user=‘test’,master_password=‘test’,MASTER_PORT=3307,master_log_file=‘mysql-bin.000003’, master_log_pos=11838161; mysql> start slave; #开启Slave
–查询从库状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 121.43.146.141
Master_User: rep1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 120
Relay_Log_File: iZbp126awga24ievihkgnqZ-relay-bin.000002
Relay_Log_Pos: 283
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: 120
Relay_Log_Space: 474
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:
–查询从库状态
mysql> show slave hosts;
±----------±-----±-----±----------±-------------------------------------+
| Server_id | Host | Port | Master_id | Slave_UUID |
±----------±-----±-----±----------±-------------------------------------+
| 2 | | 3306 | 1 | 5d8785be-62f1-11e8-a2b6-00163f00b9d4 |
±----------±-----±-----±----------±-------------------------------------+
三 主从切换
–激活从库(从库上操作)
root@localhost:db2>stop slave;
root@localhost:db2>reset master;
root@localhost:db2>reset slave all;
root@localhost:francs>show binary logs;
±---------------±----------+
| Log_name | File_size |
±---------------±----------+
| bin-log.000001 | 120 |
±---------------±----------+
1 row in set (0.00 sec)
备注:reset slave all 命令会删除从库的 replication 参数,之后 show slave status\G 的信息返回为空。
–将原来主库变为从库
CHANGE MASTER TO
MASTER_HOST=‘121.43.251.41’,
MASTER_PORT=3306,
MASTER_USER=‘rep1’,
MASTER_PASSWORD=‘rep1abcd1243d’,
MASTER_LOG_FILE=‘bin-log.000001’,
MASTER_LOG_POS=120;
root@localhost:db1>start slave;
root@localhost:db1> show slave status\G
备注:数据同步正常,以上是对 MySQL 主备切换的初次了解,后续再补充。
四 参考
市场上还有其他开源的同步工具
阿里的otter 同步
国外的Tungsten Replicator
在source那边,执行:
flush logs;
show master status;
记下File, Position。
在target端,执行:
CHANGE MASTER TO MASTER_LOG_FILE=‘mysql-bin.000005’,MASTER_LOG_POS=105608528;
slave start;
show slave status \G
一切正常。