MariaDB、MySQL數據庫主從同步

一、Mysql主從同步異步概念
node

    異步:主服務器寫完日誌後當即返回同步完成消息,不受從服務器的狀態和影響,mysql默認爲異步工做模式mysql

    同步:主服務器寫入數據到本地磁盤並記錄好二進制日誌,而後等從服務器發現數據發送改變再把數據同步到從服務器的日誌中繼並保存到二進制日誌和磁盤,最後返回給主服務器同步完成的結果,所以比較影響性能。linux


二、同步過程:sql

    主 服務器在可能修改數據時會把行內容記錄在二進制日誌中,而從服務會不停到主服務器請求數據,至關於mysql客戶端,不停請求服務器的3306端口,從服 務器請求到主服務器的日誌先記錄到從服務器的中級日誌下來,而後從服務器的本地線程讀取下來應用一次保存到從服務器的本地磁盤空間,從而達到與主服務器的 內容一致的效果。數據庫

SLAVE:IO thread:向主服務器請求二進制日誌中的事件vim

SQL thread:從中繼日誌讀取事件並在本地執行服務器

MASTER:binglog dump:將IO thread的請求事件發送給對方異步


三、注意事項:ide

1、從服務器必定不能寫數據,由於數據不會傳輸到主服務器工具

2、主服務器有而且只能有一個主服務器,通常不支持一叢多主

3mariadb-10 支持多主模型,即支持多源複製(mutil-source replication

四、主從版本儘可能相同,或從版本低於主版本

五、主從的server-id不能相同


四、配置過程:

    一、主服務器:

        1、改server-id

        2、啓用二進制日誌

        3、建立有複製權限的帳號

  2、從服務器

    1、改server-id

    2、啓用中繼日誌

    3、指向主服務器-使用主服務器建立的帳號和密碼

    4、啓動複製進程

五、環境:

系統:CentOS 6.5_x86_64

主IP:192.168.10.204

從IP:192.168.10.205

MariaDB版本:mariadb-10.0.15-linux-x86_64.tar.gz


一.新建主從結構,即主服務器沒有任何數據的狀況下加入從服務器:

主服務器:

 1、改server-id

    [root@node4 binlogs]# vim /etc/mysql/my.cnf

        server-id       = 10

  2、啓用二進制日誌

    log-bin=/data/binlogs/master-log

 3、建立有複製權限的帳號

      mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repluser'@'192.168.%.%' IDENTIFIED BY '123456';  

   Query OK, 0 rows affected (0.03 sec)

 

    mysql> FLUSH PRIVILEGES; #刷新表

    Query OK, 0 rows affected (0.00 sec)

  四、重啓服務:

    [root@node4 binlogs]# /etc/init.d/mysqld  restart

    Shutting down MySQL..                                      [  OK  ]

    Starting MySQL.                                            [  OK  ]


從服務器:

    一、改server-id:

    [root@node5 ~]# vim /etc/mysql/my.cnf

    server-id       = 100

    二、關閉從服務器的二進制日誌:

    #log-bin=/data/binlogs/master-bin

   relay-log = /data/relaylogs/relay-logs #指定從服務器的日誌存放路徑

  三、重啓服務:    [root@node5 relaylogs]# /etc/init.d/mysqld  restart

    Shutting down MySQL..                                      [  OK  ]

    Starting MySQL.                                            [  OK  ]

  四、查看從服務器狀態:

    mysql> show slave status\G;

    *************************** 1. row***************************

    Slave_IO_State: Waiting for master to sendevent

    Master_Host: 192.168.10.204

    Master_User: jack

    Master_Port: 3306

    Connect_Retry: 60

    Master_Log_File: master-log.000008

    Read_Master_Log_Pos: 1537

    Relay_Log_File: relay-bin.000004

    Relay_Log_Pos: 536

    Relay_Master_Log_File: master-log.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: 1537

    Relay_Log_Space: 827

    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: 10

    Master_SSL_Crl:

    Master_SSL_Crlpath:

    Using_Gtid: No

    Gtid_IO_Pos:


在主服務器建立數據庫,查看是否能夠同步到從服務器:

  主服務器: 

    mysql> create databases slave; #建立新的數據庫

    mysql> show databases;  #查看是否建立完成

        +--------------------+

        | Database           |

        +--------------------+

        | hellodb            |

        | information_schema |

        | mysql              |

        | performance_schema |

        | s                  |

        | slave              |

        | test               |

        +--------------------+

    mysql> USE  slave; #切換的建立的數據庫

    Database changed

    mysql> create table t1 (id int); #建立新的表

        Query OK, 0 rows affected (0.07 sec)

    mysql> INSERT INTO t1 VALUES (1),(2),(3); #向表插入簡單數據

        Query OK, 3 rows affected (0.01 sec)

        Records: 3  Duplicates: 0  Warnings: 

    mysql> select * from t1; #驗證數據是否成功寫入

        +------+

        | id   |

        +------+

        |    1 |

        |    2 |

        |    3 |

        +------+

        3 rows in set (0.00 sec)

 

 從服務器驗證:

    mysql> show databases; #查看是否有主服務器建立的數據庫

        +--------------------+

        | Database           |

        +--------------------+

        | hellodb            |

        | information_schema |

        | mysql              |

        | performance_schema |

        | s                  |

        | slave              |

        | test               |

        +--------------------+

        7 rows in set (0.07 sec)

    mysql> use slave; #切換的主服務器的數據庫

        Database changed

    mysql> select * from t1;  @查詢是否有主服務器表的信息

        +------+

        | id   |

        +------+

        |    1 |

        |    2 |

        |    3 |

        +------+

        3 rows in set (0.00 sec)


二:主服務器運行中而且有必定的數據再加入從服務器:

    此過程要先把主服務器的數據備份出來,而後拷貝到從服務器上導入到從服務器的數據庫,而後在讓從服務器從主服務器的指定位置開始備份數據便可。

   主服務器導出數據:

root@node4 binlogs]# /usr/local/mysql/bin/mysqldump --all-databases --flush-logs --master-data=2 --lock-tables > /backup.sql

[root@node4 binlogs]# scp /backup.sql  192.168.10.205:/

    root@192.168.10.205's password: 

    backup.sql                               100%  518KB 518.0KB/s   00:00    

    

    從服務器導入數據:

    [root@node5 relaylogs]# mysql < /backup.sql

    mysql>CHANGE MASTER TOMASTER_HOST='192.168.10.204',MASTER_USER='jack',MASTER_PASSWORD='123456',MASTER_LOG_FILE='master-log.000009',MASTER_LOG_POS=367;  #指定同步主服務器的用戶、密碼、日誌文件和開始的編號

    QueryOK, 0 rows affected (0.07 sec)

     

    mysql>START SLAVE;  #啓動同步進程

    QueryOK, 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.10.204

    Master_User:jack

    Master_Port:3306

    Connect_Retry:60

    Master_Log_File:master-log.000009

    Read_Master_Log_Pos:367

    Relay_Log_File:relay-bin.000002

    Relay_Log_Pos:536

    Relay_Master_Log_File:master-log.000009

    Slave_IO_Running:Yes

    Slave_SQL_Running:Yes #看到這兩個顯示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:367

    Relay_Log_Space:827

    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:10

    Master_SSL_Crl:

    Master_SSL_Crlpath:

    Using_Gtid:No

    Gtid_IO_Pos:

    1 row inset (0.00 sec)


    從服務器驗證數據庫是否同步成功:

    mysql> show databases;

        +--------------------+

        | Database           |

        +--------------------+

        | hellodb            |

        | information_schema |

        | mysql              |

        | performance_schema |

        | s                  |

        | slave              |

        | test               |

        +--------------------+

        7 rows in set (0.00 sec)

    mysql> use slave;

        Database changed

    mysql> show tables;

        +-----------------+

        | Tables_in_slave |

        +-----------------+

        | t1              |

        +-----------------+

        1 row in set (0.00 sec)

    mysql> select * from t1;  #已經成功同步到主服務器的數據

        +------+

        | id   |

        +------+

        |    1 |

        |    2 |

        |    3 |

        +------+

        3 rows in set (0.00 sec)

   在主服務器新建一個數據庫並向以前的t1表中插入數據查看是否能夠同步成功: 

   主服務器:

    mysql> create database Slave2;

        Query OK, 1 row affected (0.00 sec)

    mysql> INSERT INTO t1 VALUES (4),(5),(6);

        Query OK, 3 rows affected (0.00 sec)

        Records: 3  Duplicates: 0  Warnings: 0

    從服務器驗證: 

    mysql> show databases;

        +--------------------+

        | Database           |

        +--------------------+

        | Slave2             |

        | hellodb            |

        | information_schema |

        | mysql              |

        | performance_schema |

        | s                  |

        | slave              |

        | test               |

        +--------------------+

        8 rows in set (0.00 sec)

    mysql> use slave;

        Database changed

    mysql> show tables;

        +-----------------+

        | Tables_in_slave |

        +-----------------+

        | t1              |

        +-----------------+

        1 row in set (0.00 sec)

    mysql> select * from t1;

        +------+

        | id   |

        +------+

        |    1 |

        |    2 |

        |    3 |

        |    4 |

        |    5 |

        |    6 |

        +------+

        9 rows in set (0.00 sec)

    至此,從服務器已經從主服務器完成同步以前的數據,並能夠同步新的數據


注:操做過程中遇到兩個問題,以下:

一、主服務器的數據庫不能備份,執行命令式報錯:

[root@node4 binlogs]# mysqldump --all-databases --flush-logs --master-data=2 --lock-tables > /Backup.sql

mysqldump: Couldn't execute 'SET OPTION SQL_QUOTE_SHOW_CREATE=1': Unknown system variable 'OPTION' (1193)

    問題分析:改錯誤是在調用 mysqldump命令是使用的默認路徑/usr/bin/mysqldump,可是因爲MariaDB是安裝的目前的最新版本,此備份工具已經比較舊了,有些特性不支持,所以可使用MariaDB解壓包裏面的備份工具便可。


二、從服務器一直不能與主服務器同步

具體表現爲從服務器一直顯示IO進程鏈接正在鏈接:

mysql>show slave status\G;

***************************1. row ***************************

Slave_IO_State:Connecting to master

Master_Host:192.168.10.204

Master_User:jack

Master_Port:3306

Connect_Retry:60

Master_Log_File:

Read_Master_Log_Pos:4

Relay_Log_File:relay-bin.000001

Relay_Log_Pos:4

Relay_Master_Log_File:

Slave_IO_Running:Connecting

Slave_SQL_Running:Yes

查看日誌先顯示帳號訪問主服務器失敗,那麼就判斷爲要麼沒有權限要麼密碼不對,在確認權限正確的狀況下更改密碼,而後將從服務器的slave中止,使用新密碼從新啓動slav便可,以下;

    50119  1:38:25 [ERROR] Slave I/O: error connecting to master 'jack@192.168.10.204:3306' - retry-time: 60  retries: 86400  message: Access denied for user 'jack'@'node5.a.com' (using password: YES), Internal MariaDB error code: 1045

相關文章
相關標籤/搜索