Mysql5.6主从复制
搭建(192.168.1.10 -> 192.168.1.20)
1 master 上执行
阻塞 DML
flush tables with read lock;
记录 File 和 Position
show master status;
mysqldump -u root -p --all-databases > /data/dbbak/db.dmp
如果此 master 同时亦为 slave
mysqldump -u root -p --all-databases --dump-slave > /data/dbbak/db.dmp
unlock tables;
scp /data/dbbak/db.dmp mysql@192.168.1.20:/data/dbbak
2 slave 上执行
如果备份产生于 slave
mysqladmin -u root -p shutdown
/usr/local/mysql/bin/mysqld_safe --user=mysql --skip-slave-start &
mysql -u root -p < /data/dbbak/db.dmp
3 master 上执行
[mysqld]
server-id = 110
log-bin = /data/mysql/mysql-bin
binlog_format = MIXED
skip-slave-start = 1
#auto_increment_offset = 1
#auto_increment_increment = 1
create user 'repl'@'192.168.1.20' identified by 'xxxxxxxx';
grant replication slave on *.* to 'repl'@'192.168.1.20';
注明: 这里的IP指的从库的IP,如主库是192.168.1.10,这里加的连接用户是:192.168.1.20,不是1.10的IP。
4 slave 上执行
[mysqld]
server-id = 120
log-bin = /data/mysql/mysql-bin
binlog_format = MIXED
skip-slave-start = 1
relay-log = rep_relay_log
relay-log-index = rep_relay_log
read_only = 1
#sync_master_info = 0
#sync_relay_log = 0
#sync_relay_log_info = 0
#log-slave-updates = 1
#replicate-rewrite-db = cisdb -> newdb
#replicate-do-db = cisdb
#replicate-ignore-db = cisdb
#replicate-do-table = cisdb.t1
#replicate-ignore-table = cisdb.t1
#replicate-wild-do-table = cisdb.%
#replicate-wild-ignore-table = cisdb.%
#slave-skip-errors = xxxx,xxxx
#relay_log_purge
#relay_log_space_limit
#slave_exec_mode
#slave_compressed_protocol
#slave_parallel_workers
change master to
MASTER_HOST='192.168.60.202',
MASTER_USER='repl',
MASTER_PASSWORD='1',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=120;
start slave;
5 salve 上执行
检查 Slave_IO_Running 和 Slave_SQL_Running 是否为 Yes
检查 Seconds_Behind_Master
show slave status\G;
检查 Slave_SQL_Running 线程的 Time 值(延时,单位为秒)
show processlist\G;
为方便主从切换:
create user 'repl'@'192.168.1.10' identified by 'xxxxxxxx';
grant replication slave on *.* to 'repl'@'192.168.1.10';
注明: 这里的IP指的从库的IP,如从库是192.168.1.20,切换后从库就是1.10,这里加的连接用户是:192.168.1.10,部署1.20的IP。
新加的用户是,从库连接的IP,用户名和密码。
当用户权限中没有SUPER权限(ALL权限是包括SUPER的)时,从库的read-only生效!
使用start slave命令开启失败:
mysql> start slave;
ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository
使用reset slave命令解决:
mysql> reset slave;
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;