mysql雙主複製

兩臺服務器分別爲:192.168.1.11,192.168.1.13 (此處也能夠是兩個外網ip,即不在同一機房內的兩臺機器也能夠)
好如今開始進行配置,方便起見下面兩臺機器同時操做,你們不要看混亂哦!混亂的地方我會用服務器IP標識出來!
(1)首先確認兩臺機器上的mysql版本,版本最好一致,mysql升級後,bin-log會有更改。
即便兩臺機器上的mysql版本不一致也須要Slave的版本高於Master,而咱們如今是雙主同步,必定要一致哦!
(2)在192.168.1.13上首先創建同步所用的賬號:python

mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'192.168.1.11' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.13 sec)
mysql> flush privileges

在192.168.1.11 一樣創建同步賬號:mysql

mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'192.168.1.13' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.13 sec)
mysql> flush privileges

(3)修改192.168.1.13數據庫的配置文件my.cnf,開啓BINLOG,並設置server-id的值,修改以後必須重啓Mysql服務。sql

[mysqld]
log-bin=mysql-bin 
#網絡惟一標識,建議是ip的最後一痊
server-id=13
#須要同步的數據庫
binlog-do-db = mydb
binlog-ignore-db=mysql

修改192.168.1.11數據庫的配置文件my.cnf數據庫

[mysqld]
log-bin=mysql-bin 
#網絡惟一標識,建議是ip的最後一痊
server-id=11
#須要同步的數據庫
binlog-do-db = mydb
binlog-ignore-db=mysql

(4)分別得到服務器當前二進制日誌名和偏移量,這個操做的目的是爲了在從數據庫啓動後,從這個點開始進行數據的恢復
192.168.1.13的:服務器

mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000006
Position: 1249
Binlog_Do_DB: 
Binlog_Ignore_DB: 
1 row in set (0.00 sec)

 192.168.1.11的:網絡

mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000008
Position: 338
Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)

(5)中止對主庫的更新操做,並將192.168.1.13數據庫文件導出一份,而且導入到192.168.1.11中(這裏隨意了,若是新安裝的數據庫就不須要)ui

#首先添加一個讀鎖保證數據庫的一致性
mysql> flush tables with read lock;
mysql> quit;
mysqldump -uroot -p -P 3306 mydb > /usr/local/mysql/mydb.sql

#而後將test.sql傳到Slave機器上,而且導入。這裏要求從庫必須是啓動的。
mysql> -uroot -p --default-character-set=utf8 mydb < mydb.sql

#最後恢復Master機器的讀鎖
mysql> unlock tables

(6)配置從服務器
在192.168.1.11上執行:日誌

mysql>change master to master_host='192.168.1.13',master_user='slave',master_password='123456',master_log_file='mysql-bin.000006',master_log_pos=1249; //注意不要斷開
start slave;#啓動slave進程

在192.168.1.13上執行:code

mysql>change master to master_host='192.168.1.11',master_user='slave',master_password='123456',master_log_file='mysql-bin.000008',master_log_pos=338;
start slave;#啓動slave進程

(7)分別執行show salve status驗證主從配置是否生效server

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.157.13
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000008
          Read_Master_Log_Pos: 834
               Relay_Log_File: lvstest-relay-bin.000004
                Relay_Log_Pos: 980
        Relay_Master_Log_File: mysql-bin.000008
             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: 834
              Relay_Log_Space: 1284
              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: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
1 row in set (0.00 sec)

主要看這兩項:

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

都顯示如上,說明配置成功。

以上是主主的配製,若是須要主人的話,將192.168.1.11上的這些操做去掉就好了。

相關文章
相關標籤/搜索