1. 程式人生 > >MySQL 主主復制

MySQL 主主復制

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-bin
relay-log-index=slave-relay-bin.index
重啟服務
[[email protected]
/* */~]# service mysqld restart

slave:開啟二進制日誌

編輯/etc/my.conf添加
log-bin=master-bin
log-slave-update=true
重啟服務
[[email protected] ~]# service mysqld restart

2.配置同步

上一篇文章中已在master添加授權賬號,並且在slave上面配置同步操作;

這裏只需要配置slave添加授權賬號,在master上面配置同步操作;

slave:

mysql> grant replication slave on *.* to [email protected]
/* */%‘ 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: 0
Master_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主主復制配置完畢!


本文出自 “隔壁老大哥” 博客,請務必保留此出處http://guoxh.blog.51cto.com/10976315/1922657

MySQL 主主復制