欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mysql双主同步

程序员文章站 2022-04-10 12:00:54
...

配置db1,db2主-主同步 

1配置机器1(100)

vi /etc/my.cnf 

 

server-id=1

log-bin= donut-1-bin

replicate-do-db = testcopy2 #在从库中的目标库

replicate-rewrite-db = testcopy-> testcopy2 #在从库中配置不同名字的数据库

replicate-ignore-db=mysql

replicate-ignore-db=information_schema

replicate-ignore-db=performance_schema

expire_logs_days= 10 

max_binlog_size = 100M

auto_increment_offset = 1

auto_increment_increment = 2

skip-name-resolve

skip-slave-start

log-slave-updates  #很重要,从前一台机器上同步过来的数据才能同步到下一台机器 

 

2配置机器2(101)

vi /etc/my.cnf

 

server-id=2

log-bin= testcopy-2-bin

replicate-do-db = testcopy #在从库中的目标库

replicate-rewrite-db = testcopy2 -> testcopy #在从库中配置不同名字的数据库

replicate-ignore-db=mysql

replicate-ignore-db=information_schema

replicate-ignore-db=performance_schema

expire_logs_days= 10 

max_binlog_size = 100M

auto_increment_offset = 1

auto_increment_increment = 2

skip-name-resolve

skip-slave-start

log-slave-updates

 

3重启mysql 

/etc/init.d/mysql restart

 

4添加复制用户 

mysql -uroot -p

#授权指定网段的ip访问

GRANT REPLICATION SLAVE ON *.* TO 'testcopy'@'192.168.0.%' IDENTIFIED BY 'testcopy';

 

5如果已经存需要同步

mysql -uroot -p  #登录100

FLUSH TABLES WITH READ LOCK; # 锁定100

mysqldump -uroot -p -P3306 testcopy --triggers --routines --events >>~/testcopy.sql

mysql -uroot -p  -h192.168.0.101 -P3306 testcopy< ~/testcopy.sql

 

6100记录日志文件和pos 

mysql -uroot -p;

show master status;

 

7db-101(101的主数据库是100)

change master to master_host ='192.168.0.100',master_port=3306,master_user='testcopy',master_password='testcopy',master_log_file='testcopy-1-bin.000001',master_log_pos= 740421;

start slave;

show slave status;

 

说明:testcopy-bin.000001和327是主库配置第7步记录的信息

出现以下内容,说明同步ok 

           Slave_IO_Running: Yes 

          Slave_SQL_Running: Yes

 

8解锁100

unlock tables;

 

9如果已经存需要同步

mysql -uroot -p  #登录101

FLUSH TABLES WITH READ LOCK; # 锁定101

mysqldump -uroot -p -P3306 testcopy --triggers --routines --events >>~/testcopy.sql

mysql -uroot -p  -h192.168.0.100 -P3306 testcopy< ~/testcopy.sql

 

10.101记录日志文件和pos 

mysql -uroot -p 

show master status;

 

11db-100(100的主数据库是101)

change master to master_host ='192.168.0.101',master_port=3306,master_user='testcopy',master_password='testcopy',master_log_file='testcopy-2-bin.000007',master_log_pos= 35089377;

start slave;

show slave status;

 

说明:testcopy-bin.000001和327是主库配置第5步记录的信息

出现以下内容,说明同步ok 

           Slave_IO_Running: Yes 

          Slave_SQL_Running: Yes

12解锁207

unlock tables;

 

 

13测试主-主同步 

 

 

14EERROR 1201 (HY000)

stop slave; 

reset slave;  

change master to 重复上述操作, 6,7