mysql主從配置詳見http://chinawu.blog.51cto.com/10692884/1739327
mysql主主備份同步,其實就是互相主從,A對B主從,B對A主從
注:在實驗之前請先關閉iptables和selinux ,iptables -F && service iptables save
vim /etc/selinux/config 中selinux值改成disabled
環境描述:A:192.168.3.116
B:192.168.3.251
Mysql版本mysql5.1.73
Linux系統版本:CentOS6.5
安裝mysql略過:詳情見lamp安裝http://chinawu.blog.51cto.com/10692884/1739296
給ABmysql設置登錄密碼,命令:mysqladmin -uroot password 'yourpass'
各登錄ABmysql
A:grant replication slave,file on *.* to 'repl'@'192.168.3.251' identified by '123456';
flush privileges;
B: grant replication slave,file on *.* to 'repl'@'192.168.3.116' identified by '123456';
flush privileges;
配置各mysql配置文件
A:
vim /etc/my.cnf
在server-id出修改並添加如下語句
server-id = 1
binlog-do-db=test
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
B:
server-id = 2
binlog-do-db=test
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=2
其中:log-slave-updates 將執行的複製sql記錄到二進制日誌
sync_binlog 當有二進制日誌寫入binlog文件的時候,mysql服務器將它同步到磁盤上
auto_increment_increment,auto_increment_offset 主要用於主主複製中,用來控制AUTO_INCREMENT列的操作
重啓ABmysql服務,並進入MySQL數據庫
A:flush tables with read lock;
show master status;查看此時A的master_log_file 和master_log_pos值
unlock tables;
slave stop;
change master to master_host='192.168.3.251',master_user='repl',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=106;#此處的master_log_file和master_log_pos爲B上show master status值
start slave;
B:
flush tables with read lock;
show master status;查看此時B的master_log_file 和master_log_pos值
unlock tables;
slave stop;
change master to master_host='192.168.3.116',master_user='repl',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=106;#此處的master_log_file和master_log_pos爲A上show master status值
start slave;
查看狀態,在A和B上分別show slave status;若同時出現
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
則表示互爲主從配置成功;
測試:針對同步的數據庫test操作,分別在A上create table tb1 ,在B上show tables;若出現A上創建的tb1則表示A到B同步成功;在B上create table tb2,A上show tables;出現tb2,則表示B到A同步成功;