mysql主主复制(双主复制)配置步骤

MySQL主主复制结构区别于主从复制结构。在主主复制结构中,两台服务器的任何一台上面的数据库存发生了改变都会同步到另外一台服务器上,这样两台服务器互为主从,而且都能向外提供服务。
有了上一节的主从复制,那么主主复制就很容易了。mysql

1、先修改配置文件sql

服务器A(192.168.1.254)配置以下数据库

 
log-bin   = mysql-bin
server-id = 1 
expire-logs-days  = 100 
replicate-do-db   = test
binlog-ignore-db  = mysql
binlog-ignore-db  = information_schema
auto-increment-increment = 2 
auto-increment-offset = 1
服务器B(192.168.1.252)配置服务器

 
log-bin   = mysql-bin
server-id = 2
expire-logs-days  = 100
replicate-do-db   = test
binlog-ignore-db  = mysql
binlog-ignore-db  = information_schema
auto-increment-increment = 2
auto-increment-offset = 2
两台服务器都重启ide

 
mysql> service mysqld restart
注:二都只有server-id不一样和 auto-increment- offset不一样
auto-increment-offset是用来设定数据库中自动增加的起点的,回为这两能服务器都设定了一次自动增加值2,因此它们的起点必须得不一样,这样才能避免两台服务器数据同步时出现主键冲突
replicate-do-db 指定同步的数据库,咱们只在两台服务器间同步test数据库
另:auto-increment-increment的值应设为整个结构中服务器的总数,本案例用到两台服务器,因此值设为2测试

2、同步数据spa

本文是用test作的实验,导出将test.sql文件从254服务器拷贝到252服务器
备份数据前先锁表,保证数据一致性rest


 
mysql> FLUSH TABLES WITH READ LOCK;orm

 
# mysqldump -uroot -p123456 test> /tmp/test.sql;server

 
mysql> UNLOCK TABLES;

 scp /tmp/test.sql root@192.168.1.252:/tmp
3、相互受权用户(在A服务器受权一个容许B访问的用户,反之亦然)

在服务器A(192.168.1.254)上


 
mysql> GRANT REPLICATION SLAVE ON *.* TO 'mysync'@'192.168.1.252' IDENTIFIED BY PASSWORD '123456';
mysql> flush privileges;
在服务器B(192.168.1.252)上

 
mysql> GRANT REPLICATION SLAVE ON *.* TO 'mysync'@'192.168.1.254' IDENTIFIED BY PASSWORD '123456';
mysql> flush privileges;
4、互告bin-log信息

在服务器A(192.168.1.254)


 
mysql> show master status;
+------------------+----------+--------------+--------------------------+
| File     | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql-bin.000006 |      106 |      | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
在服务器A(192.168.1.252)

 
mysql> show master status;
+------------------+----------+--------------+--------------------------+
| File     | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql-bin.000008 |      192 |      | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
在A服务器(192.168.1.254)上执行


 
mysql> change master to master_host='192.168.1.252',master_user='mysync',master_password='123456',master_log_file='mysql-bin.000008',master_log_pos=192;
在B服务器(192.168.1.252)上执行


 
mysql> change master to master_host='192.168.1.254',master_user='mysync',master_password='123456',master_log_file='mysql-bin.000006',master_log_pos=106;
5、在两服务器都执行如下命令

mysql> start slave;

6、查看状态

mysql> show slave status\G
A服务器(192.168.1.254)状态以下:

 Slave_IO_State: Waiting for master to send event
  Master_Host: 192.168.1.252
  Master_User: mysync
  Master_Port: 3306
Connect_Retry: 60
      Master_Log_File: mysql-bin.000008
  Read_Master_Log_Pos: 192
       Relay_Log_File: mysqld-relay-bin.000009
Relay_Log_Pos: 337
Relay_Master_Log_File: mysql-bin.000008
     Slave_IO_Running: Yes
    Slave_SQL_Running: Yes
      Replicate_Do_DB: test
B服务器(192.168.1.252)状态以下:


  Slave_IO_State: Waiting for master to send event
  Master_Host: 192.168.1.254
  Master_User: mysync
  Master_Port: 3306
Connect_Retry: 60
      Master_Log_File: mysql-bin.000006
  Read_Master_Log_Pos: 106
       Relay_Log_File: mysqld-relay-bin.000014
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000006
     Slave_IO_Running: Yes
    Slave_SQL_Running: Yes
      Replicate_Do_DB: test
当看到了两个yes,即:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
说明已经配置成功了

接下来看能够作一下实验,测试一下是否同步

PS:在测试的过程中,我也遇到一些问题主要是两台机器互相通讯的问题请注意,必定要保持两台的服务器的mysql端口都向对方打开,要否则是不能成功同步的。

相关文章
相关标签/搜索