MySQL-主從複製

MySQL-主從-主主配置

標籤(空格分隔): mysqlmysql


環境準備

兩臺linux虛擬機:
A: 192.168.135.140 [主服務器]
B: 192.168.135.141 [從服務器]

主從複製

步驟-思路:linux

1 修改兩臺msyq配置文件  [vi /etc/my.cnf]

2 在主服務器[A]建立用戶:
grant replication slave on *.* to '自定義用戶名'@'從服務器IP[B]' identified by '自定義用戶密碼';
    
3 刷新權限
flush privileges;

4 查看主服務器[A]二進制日誌名和位置
show master status;

5 在從服務器[B]告知二進制文件名與位置
slave stop; 關閉複製

change master to master_host='主服務器IP[A]', master_user='在服務器建立的用戶', master_password='密碼', master_log_file='主服務器的日誌名', master_log_pos=主服務器的位置;

6 查看slave
slave start;
show slave status\G;

修改配置sql

依次修改A B 兩臺服務器MySQL配置文件
vi /etc/my.cnf
A :
    log-bin=mysql-bin
    binlog_format=mixed
    server-id   = 1
    expire_logs_days = 10

B :
    log-bin=mysql-bin
    binlog_format=mixed
    server-id   = 2
    expire_logs_days = 10
    
注意:二進制日誌必須開啓,由於數據的同步實質上就是其餘的MySQL數據庫服務器將這個數據變動的二進制日誌在本機上再執行一遍。

開始主從複製數據庫

在A服務器中建立一個B服務器能夠登錄的mysql用戶
username: user2
password: user2

在A服務器:
mysql> grant replication slave on *.* to 'user2'@'192.168.135.141' identified by 'user2';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000019 |      338 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

在B服務:
mysql> slave stop;

mysql> change master to master_host='192.168.135.140', master_user='user2',     master_password='user2', master_log_file='mysql-bin.000019', master_log_pos=338;
Query OK, 0 rows affected (0.04 sec)

mysql> slave start;

測試服務器

mysql> slave start;
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.135.140
                  Master_User: user2
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000009
          Read_Master_Log_Pos: 796
               Relay_Log_File: cherry-relay-bin.000002
                Relay_Log_Pos: 253
        Relay_Master_Log_File: mysql-bin.000009
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: yanweifeng
          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: 796
              Relay_Log_Space: 410
              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 都是Yes時才配置成功

主主配置

思路和主從配置同樣,就是把上邊的操做反過來再執行一遍就能夠。接着上面繼續執行

在從服務器[B]建立用戶,刷新權限,查看二進制文件名和位置,
關閉複製,在主服務器[A]告知二進制文件名與位置,開啓複製,查看slave服務

從服務器:ide

mysql> grant replication slave on *.* to 'user1'@'192.168.135.140' 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\G;
*************************** 1. row ***************************
            File: mysql-bin.000016
        Position: 432
    Binlog_Do_DB: 
Binlog_Ignore_DB: 
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql> 

----------------------------------------------------------------------------------------------

主服務器:測試

mysql> slave stop;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to master_host='192.168.135.141', master_user='user2', master_password='123456', master_log_file='mysql-bin.000016', master_log_pos=432;
Query OK, 0 rows affected (0.02 sec)

mysql> 
mysql> slave start;
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.135.141
                  Master_User: user2
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000016
          Read_Master_Log_Pos: 432
               Relay_Log_File: cherry-relay-bin.000002
                Relay_Log_Pos: 253
        Relay_Master_Log_File: mysql-bin.000016
             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: 432
              Relay_Log_Space: 410
              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: 2
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql>

測試

分別在兩臺服務器操做 看是否同步

常見出錯點:

    1 兩臺數據庫都存在db數據庫,而第一臺MySQL db中有tab1,第二臺MySQL db中沒有tab1,那確定不能成功。
    2 已經獲取了數據的二進制日誌名和位置,又進行了數據操做,致使POS發生變動。在配置CHANGE MASTER時仍是用到以前的POS。
    3 stop slave後,數據變動,再start slave。出錯。
相關文章
相關標籤/搜索