千家信息网

MySQL 主主复制

发表于:2025-01-20 作者:千家信息网编辑
千家信息网最后更新 2025年01月20日,MySQL主主复制其实就是基于主从复制做的双向同步。主从复制:对master做操作会同步到slave,对slave做操作不会同步到master;主主复制:可以向两台MySQL做操作,并且都可以同步到另
千家信息网最后更新 2025年01月20日MySQL 主主复制

MySQL主主复制其实就是基于主从复制做的双向同步。

主从复制:对master做操作会同步到slave,对slave做操作不会同步到master;

主主复制:可以向两台MySQL做操作,并且都可以同步到另外一台MySQL数据库。


一.配置主从复制

请参考主从复制博文(http://guoxh.blog.51cto.com/10976315/1922643)

二.配置主主复制

1.修改MySQL配置文件

master:开启中继日志

编辑/etc/my.conf添加relay-log=relay-log-binrelay-log-index=slave-relay-bin.index重启服务[root@master~]# service mysqld restart

slave:开启二进制日志

编辑/etc/my.conf添加log-bin=master-binlog-slave-update=true重启服务[root@slave ~]# service mysqld restart

2.配置同步

上一篇文章中已在master添加授权账号,并且在slave上面配置同步操作;

这里只需要配置slave添加授权账号,在master上面配置同步操作;

slave:

mysql> grant replication slave on *.* to 'slave'@'192.168.0.%' identified by '123456';Query OK, 0 rows affected (0.00 sec)mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)mysql> show master status;+-------------------+----------+--------------+------------------+| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |+-------------------+----------+--------------+------------------+| master-bin.000001 |      106|              |                  |+-------------------+----------+--------------+------------------+1 row in set (0.00 sec)

master:

mysql> change master to master_host='192.168.0.135',master_user='slave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=106;Query OK, 0 rows affected (0.40 sec)mysql> start slave; #启动同步Query OK, 0 rows affected (0.00 sec)mysql> show slave status \G #查看同步状态*************************** 1. row ***************************               Slave_IO_State: Waiting for master to send event                  Master_Host: 192.168.0.135                  Master_User: slave                  Master_Port: 3306                Connect_Retry: 60              Master_Log_File: master-bin.000005          Read_Master_Log_Pos: 106               Relay_Log_File: relay-log-bin.000012                Relay_Log_Pos: 252        Relay_Master_Log_File: master-bin.000005             Slave_IO_Running: Yes            Slave_SQL_Running: Yes              Replicate_Do_DB:           Replicate_Ignore_DB:            Replicate_Do_Table:        Replicate_Ignore_Table:       Replicate_Wild_Do_Table:   Replicate_Wild_Ignore_Table:                    Last_Errno: 0                   Last_Error:                  Skip_Counter: 0          Exec_Master_Log_Pos: 106              Relay_Log_Space: 551              Until_Condition: None               Until_Log_File:                 Until_Log_Pos: 0           Master_SSL_Allowed: No           Master_SSL_CA_File:            Master_SSL_CA_Path:               Master_SSL_Cert:             Master_SSL_Cipher:                Master_SSL_Key:         Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: No                Last_IO_Errno: 0                Last_IO_Error:                Last_SQL_Errno: 0               Last_SQL_Error: 1 row in set (0.00 sec)



#Slave_IO和Slave_SQL为YES说明同步成功。


三.测试

1.在slave上面新建一个数据库

mysql> create database master;Query OK, 1 row affected (0.00 sec)mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || aaa                || master             || mysql              || test               |+--------------------+5 rows in set (0.00 sec)

2.在master上面查看

mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || aaa                || master             || mysql              || test               |+--------------------+5 rows in set (0.00 sec)

3.在master删除一个数据库

mysql> drop database aaa;Query OK, 0 rows affected (0.00 sec)mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || master             || mysql              || test               |+--------------------+4 rows in set (0.00 sec)

4.在slave上面查看

mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || master             || mysql              || test               |+--------------------+4 rows in set (0.00 sec)


#到这里,MySQL主主复制配置完毕!


0