实验环境:
system:centos 6.5 64位
mysql:5.6.23
hostname ip
local-db1 192.168.1.211
local-db2 192.168.1.212
1.登录mysql,查看库
2.db1配置
(1)修改配置文件
[[email protected] ~]# vi /etc/my.cnf
log-bin = mysql-bin
server-id = 1
(2)重启mysql
[[email protected] ~]# /etc/init.d/mysql restart
(3)查看配置是否生效
(4)授权可同步用户,登录mysql操作
grant replication slave on *.* to ‘mysqlbak‘@‘192.168.1.212‘ identified by ‘123456‘;
(5)锁表
flush tables with read lock; ##防止有新的数据写入,导致master状态变更
(6)导出数据
[[email protected] ~]# mysqldump -uroot -p123456 --all-database > /opt/bak.sql ##将导出的bak.sql传到db2上
(7)解锁,查看master状态
unlock tables;
show master status;
3.db2配置
(1)修改配置文件
[[email protected] ~]# vi /etc/my.cnf
log-bin = mysql-bin
server-id = 2
(2)重启mysql
[[email protected] ~]# /etc/init.d/mysql restart
(3)查看配置是否生效
(4)导入数据
[[email protected] ~]# mysql -uroot -p123456 < /opt/bak.sql ##bak.sql是从db1上拷贝的
(5)登录数据库,执行
CHANGE MASTER TO
MASTER_HOST=‘192.168.1.211‘,
MASTER_PORT=3306,
MASTER_USER=‘mysqlbak‘,
MASTER_PASSWORD=‘123456‘,
MASTER_LOG_FILE=‘mysql-bin.000001‘, ##此处内容,同主库show master status file的值
MASTER_LOG_POS=334; ##主库show master status Position值
(6)开启同步,查看同步状态
start slave; ##执行同步开关
show slave status\G ##查看同步状态
4.同步测试
(1)在db1上新建库,新建表
(2)在db2上查看是否同步成功