主 主 同 步
先准备好两台主机,都安装上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 root@192.168.1.252:/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信息
在服务器Amysql> show master status;| File | Position | Binlog_Do_DB | Binlog_Ignore_DB || mysql-bin.000006 | 106 | | mysql,information_schema |在服务器Bmysql> 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: YesSlave_SQL_Running: Yes第七步:测试在test库中创建表测试。