一聚教程网:一个值得你收藏的教程网站

热门教程

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

时间:2022-11-14 23:37:04 编辑:袖梨 来源:一聚教程网


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

一、先修改配置文件

服务器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
两台服务器都重启


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

二、同步数据

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



mysql> FLUSH TABLES WITH READ LOCK;


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


mysql> UNLOCK TABLES;

scp /tmp/test.sql root@192.168.1.252:/tmp
三、相互授权用户(在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;
四、互告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;
五、在两服务器都执行以下命令

mysql> start slave;

六、查看状态

mysql> show slave statusG
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端口都向对方打开,要不然是不能成功同步的。

热门栏目