主 主 同 步
先準備好兩臺主機,都安裝上mysql。
假設A主機的IP爲:192.168.216.10
B主機的IP爲:192.168.216.11
目標:同步test庫內所有表
第一步 : 配置文件
主機A配置
用VIM編輯器編輯/etc/my.conf
#/etc/my.conf
添加如下內容:
log-bin=mysql-bin
server-id=1
expire-logs-days=99
replicate-do-db=test
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
auto-increment-increment=2
auto-increment-offset=1
然後重啓mysql服務
主機B配置
同樣先編輯文件
#/etc/my.conf
log-bin=mysql-bin
server-id=2
expire-logs-days=99
replicate-do-db=test
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
auto-increment-increment=2
auto-increment-offset=2
然後重啓服務
第二步 : 同步數據
進入mysql內
mysql>flush tables with read lock;
#mysqldump -uroot -p123456 test> /tmp
mysql>unlock tables;
scp /tmp/test.sql [email protected]:/tmp
第三步 : 相互授權用戶
A主機
mysql> grant replication slave on *.* to 'zhangsna'@'192.168.216.11' identified by '123456'
mysql> flush privileges;
B主機
mysql>grant replocation slave on *.* to 'zhangsan'@'192.168.216.10' identified by '123456'
mysql> flush privileges;
第四步 : 告知bin-log信息
在服務器A
mysql> show master status;
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
| mysql-bin.000006 | 106 | | mysql,information_schema |
在服務器B
mysql> show master status;
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
| mysql-bin.000008 | 192 | | mysql,information_schema |
在A服務器上執行
mysql> change master to master_host='192.168.216.17',master_user='zhangsan',master_password='123456',master_log_file='mysql-bin.000008',master_log_pos=192;
在B服務器上執行
mysql> change master to master_host='192.168.216.16',master_user='zhangsan',master_password='123456',master_log_file='mysql-bin.000006',master_log_pos=106;
第五步 :在兩服務器都執行以下命令
mysql> start slave;
第六步:在兩臺服務器上分別查看狀態
mysql> show slave status\G
有以下兩項則配置成功:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
第七步:測試
在test庫中創建表測試。