MySQL的主主同步

                                            主 主 同 步

   先准备好两台主机,都安装上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库中创建表测试。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章