步骤:
1)两个MySQL实例:
IP地址:10.0.0.201 端口:3306 (MySQL的Master)
IP地址:10.0.0.201 端口:3307 (MySQL的Slave)
2)两台数据库实例,需要开启log-bin、GTID、和级联复制的选线log_slave_updates
my.cnf配置参数如下:
master(3306)
server-id = 1203306
log-bin = /data/mysqldata/3306/binlog/mysql-bin
#*********** GTID settting*******************
gtid_mode=ON
enforce-gtid-consistency=true
binlog_format= row
skip-slave-start=1
log_slave_updates = 1
slave(3307)
server-id = 1203307
log-bin = /data/mysqldata/3307/binlog/mysql-bin
#*********** GTID settting*******************
gtid_mode=ON
enforce-gtid-consistency=true
binlog_format= row
skip-slave-start=1
log_slave_updates = 1
3)为Replication创建一个用户
在master上,配置一个新用户,可以让slave服务器能读取master服务器
mysql> CREATE USER 'repl'@'10.0.0.201' IDENTIFIED BY 'repl@3307';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'10.0.0.201';
4)Master保证数据一致性,设置为只读
mysql> SET @@global.read_only = ON;
(root@localhost) [(none)]> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000006
Position: 737
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 4160e9b3-58d9-11e8-b174-005056af6f24:1-21
1 row in set (0.00 sec)
********备份******************
/usr/local/mysql/bin/mysqldump -uroot -p'zsd@7101' -S /data/mysqldata/3306/mysql.sock -A --flush-logs --single-transaction -e | gzip > mysql_3306_full_backup_`date +%F`.sql.gz
-A参数 参数解释:--all-databases, -A。备份所有数据库的所有表。
--flush-logs参数 参数解释:在数据dump之前,刷新数据库的日志。
--single-transaction参数 参数解释:服务器dump之前,发起一个BEGIN SQL的语句, START TRANSACTION 放进一个事务里面。
-e参数 参数解释:--extended-insert, -e 产生一个小的dump文件,加速插入的速度
master导出备份
/usr/local/mysql/bin/mysqldump -uroot -p'zsd@7101' -S /data/mysqldata/3306/mysql.sock --all-databases --triggers --routines --events --flush-logs --single-transaction -e --default-character-set=utf8 | gzip > mysql_3306_full_backup_`date +%F`.sql.gz
slave导入备份
/usr/local/mysql/bin/mysql -uroot -p'zsd@3307' -S /data/mysqldata/3307/mysql.sock --default-character-set=utf8 < mysql_3306_full_backup_2018-06-01.sql
5)SLAVE追日志
------------------抽象语句-----------------------------
mysql> CHANGE MASTER TO
> MASTER_HOST = host,
> MASTER_PORT = port,
> MASTER_USER = user,
> MASTER_PASSWORD = password,
> MASTER_AUTO_POSITION = 1;
-----------------实战语句------------------------------
CHANGE MASTER TO MASTER_HOST = '10.0.0.201',MASTER_PORT = 3306,MASTER_USER = 'repl',MASTER_PASSWORD = 'repl@3307',MASTER_AUTO_POSITION = 1;
6)启动slave
mysql> START SLAVE;
7)Master服务,关闭只读
mysql> SET @@global.read_only = OFF;
完全删除replication的拓扑结构
(root@localhost) [(none)]> stop slave;
(root@localhost) [(none)]> RESET SLAVE ALL;