實驗環境:mysql
system:centos 6.5 64位sql
mysql:5.6.23數據庫
hostname ipcentos
local-db1 192.168.1.211ide
local-db2 192.168.1.212測試
1.登陸mysql,查看庫rest
2.db1配置server
(1)修改配置文件blog
[root@local-db1 ~]# vi /etc/my.cnf ip
log-bin = mysql-bin
server-id = 1
(2)重啓mysql
[root@local-db1 ~]# /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)導出數據
[root@local-db1 ~]# mysqldump -uroot -p123456 --all-databases > /opt/bak.sql ##將導出的bak.sql傳到db2上
(7)解鎖,查看master狀態
unlock tables;
show master status;
3.db2配置
(1)修改配置文件
[root@local-db1 ~]# vi /etc/my.cnf
log-bin = mysql-bin
server-id = 2
(2)重啓mysql
[root@local-db2 ~]# /etc/init.d/mysql restart
(3)查看配置是否生效
(4)導入數據
[root@local-db2 ~]# 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上查看是否同步成功