配置主从和数据备份
初始主节点master配置:
[root@CentOS7-master ~]# vim /etc/my.cnf
[mysqld]
server-id = 1
log-bin = master-log
relay-log = relay-log
skip_name_resolve = ON #这个参数是禁止域名解析的
所有slave节点依赖的配置:
[root@CentOS7-slave1 ~]# vim /etc/my.cnf [mysqld] server-id = 2 #复制集群中的各节点的id均必须唯一; relay-log = relay-log #开启中继日志; log-bin = master-log #开启二进制日志,因为每一台slave都可能会变成master; read_only = ON #开启只读权限; relay_log_purge = 0 #是否自动清空不再需要的中继日志; skip_name_resolve = ON #不进行域名解析
[root@CentOS7-slave2 ~]# vim /etc/my.cnf [mysqld] server-id = 3 #复制集群中的各节点的id均必须唯一; relay-log = relay-log log-bin = master-log read_only = ON relay_log_purge = 0 #是否自动清空不再需要中继日志 skip_name_resolve = ON
配置主从
主执行语句
创建用户
GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'slave'@'%' IDENTIFIED BY '123456';
刷新权限
FLUSH PRIVILEGES;
从执行语句
CHANGE MASTER TO MASTER_HOST='192.168.111.201', MASTER_USER='slave', MASTER_PASSWORD='123456', MASTER_LOG_FILE='master-log.000002', MASTER_LOG_POS=245;
CHANGE MASTER TO MASTER_HOST='192.168.111.202', MASTER_USER='slave', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000005', MASTER_LOG_POS=632;
备份数据
(1)在Master 192.168.2.128(server02)上备份一份完整的数据:
192.168.2.128 [root ~]$ mysqldump -uroot -p123456 --master-data=2 --single-transaction -R --triggers -A > all.sql
其中--master-data=2代表备份时刻记录master的Binlog位置和Position,--single-transaction意思是获取一致性快照,-R意思是备份存储过程和函数,--triggres的意思是备份触发器,-A代表备份所有的库。
(2)在Master 192.168.2.128(server02)上创建复制用户:
mysql> grant replication slave on . to 'repl'@'192.168.2.%' identified by '123456'; Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
(3)查看主库备份时的binlog名称和位置,MASTER_LOG_FILE和MASTER_LOG_POS:
192.168.2.128 [root ~]$ head -n 30 all.sql | grep 'CHANGE MASTER TO' -- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000004', MASTER_LOG_POS=245;
(4)把备份复制到192.168.2.129和192.168.2.130
192.168.2.128 [root ~]$ scp all.sql 192.168.2.129:/root/
all.sql 100% 500KB 500.5KB/s 00:00
192.168.2.128 [root ~]$ scp all.sql 192.168.2.130:/root/
all.sql
(5)分别在两台服务器上导入备份,执行复制相关命令
在slave主机server03 192.168.2.129上操作:
192.168.2.129 [root ~]$ mysql -uroot -p123456 < ./all.sql