溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

MariaDB、MySQL數(shù)據(jù)庫主從同步

發(fā)布時(shí)間:2020-07-30 22:38:14 來源:網(wǎng)絡(luò) 閱讀:2320 作者:冷暖己知 欄目:數(shù)據(jù)庫

1、Mysql主從同步異步概念

    異步:主服務(wù)器寫完日志后立即返回同步完成消息,不受從服務(wù)器的狀態(tài)和影響,mysql默認(rèn)為異步工作模式

    同步:主服務(wù)器寫入數(shù)據(jù)到本地磁盤并記錄好二進(jìn)制日志,然后等從服務(wù)器發(fā)現(xiàn)數(shù)據(jù)發(fā)送改變?cè)侔褦?shù)據(jù)同步到從服務(wù)器的日志中繼并保存到二進(jìn)制日志和磁盤,最后返回給主服務(wù)器同步完成的結(jié)果,因此比較影響性能。


2、同步過程:

    主 服務(wù)器在可能修改數(shù)據(jù)時(shí)會(huì)把行內(nèi)容記錄在二進(jìn)制日志中,而從服務(wù)會(huì)不停到主服務(wù)器請(qǐng)求數(shù)據(jù),相當(dāng)于mysql客戶端,不停請(qǐng)求服務(wù)器的3306端口,從服 務(wù)器請(qǐng)求到主服務(wù)器的日志先記錄到從服務(wù)器的中級(jí)日志下來,然后從服務(wù)器的本地線程讀取下來應(yīng)用一次保存到從服務(wù)器的本地磁盤空間,從而達(dá)到與主服務(wù)器的 內(nèi)容一致的效果。

SLAVE:IO thread:向主服務(wù)器請(qǐng)求二進(jìn)制日志中的事件

SQL thread:從中繼日志讀取事件并在本地執(zhí)行

MASTER:binglog dump:將IO thread的請(qǐng)求事件發(fā)送給對(duì)方


3、注意事項(xiàng):

1、從服務(wù)器一定不能寫數(shù)據(jù),因?yàn)閿?shù)據(jù)不會(huì)傳輸?shù)街鞣?wù)器

2、主服務(wù)器有并且只能有一個(gè)主服務(wù)器,一般不支持一叢多主

3、mariadb-10 支持多主模型,即支持多源復(fù)制(mutil-source replication

4、主從版本盡量相同,或從版本低于主版本

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


4、配置過程:

    1、主服務(wù)器:

        1、改server-id

        2、啟用二進(jìn)制日志

        3、創(chuàng)建有復(fù)制權(quán)限的賬號(hào)

  2、從服務(wù)器

    1、改server-id

    2、啟用中繼日志

    3、指向主服務(wù)器-使用主服務(wù)器創(chuàng)建的賬號(hào)和密碼

    4、啟動(dòng)復(fù)制進(jìn)程

5、環(huán)境:

系統(tǒng):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


一.新建主從結(jié)構(gòu),即主服務(wù)器沒有任何數(shù)據(jù)的情況下加入從服務(wù)器:

主服務(wù)器:

 1、改server-id

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

        server-id       = 10

  2、啟用二進(jìn)制日志

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

 3、創(chuàng)建有復(fù)制權(quán)限的賬號(hào)

      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)

  4、重啟服務(wù):

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

    Shutting down MySQL..                                      [  OK  ]

    Starting MySQL.                                            [  OK  ]


從服務(wù)器:

    1、改server-id:

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

    server-id       = 100

    2、關(guān)閉從服務(wù)器的二進(jìn)制日志:

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

   relay-log = /data/relaylogs/relay-logs #指定從服務(wù)器的日志存放路徑

  3、重啟服務(wù):    [root@node5 relaylogs]# /etc/init.d/mysqld  restart

    Shutting down MySQL..                                      [  OK  ]

    Starting MySQL.                                            [  OK  ]

  4、查看從服務(wù)器狀態(tài):

    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:


在主服務(wù)器創(chuàng)建數(shù)據(jù)庫,查看是否可以同步到從服務(wù)器:

  主服務(wù)器: 

    mysql> create databases slave; #創(chuàng)建新的數(shù)據(jù)庫

    mysql> show databases;  #查看是否創(chuàng)建完成

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

        | Database           |

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

        | hellodb            |

        | information_schema |

        | mysql              |

        | performance_schema |

        | s                  |

        | slave              |

        | test               |

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

    mysql> USE  slave; #切換的創(chuàng)建的數(shù)據(jù)庫

    Database changed

    mysql> create table t1 (id int); #創(chuàng)建新的表

        Query OK, 0 rows affected (0.07 sec)

    mysql> INSERT INTO t1 VALUES (1),(2),(3); #向表插入簡(jiǎn)單數(shù)據(jù)

        Query OK, 3 rows affected (0.01 sec)

        Records: 3  Duplicates: 0  Warnings: 

    mysql> select * from t1; #驗(yàn)證數(shù)據(jù)是否成功寫入

        +------+

        | id   |

        +------+

        |    1 |

        |    2 |

        |    3 |

        +------+

        3 rows in set (0.00 sec)

 

 從服務(wù)器驗(yàn)證:

    mysql> show databases; #查看是否有主服務(wù)器創(chuàng)建的數(shù)據(jù)庫

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

        | Database           |

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

        | hellodb            |

        | information_schema |

        | mysql              |

        | performance_schema |

        | s                  |

        | slave              |

        | test               |

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

        7 rows in set (0.07 sec)

    mysql> use slave; #切換的主服務(wù)器的數(shù)據(jù)庫

        Database changed

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

        +------+

        | id   |

        +------+

        |    1 |

        |    2 |

        |    3 |

        +------+

        3 rows in set (0.00 sec)


二:主服務(wù)器運(yùn)行中并且有一定的數(shù)據(jù)再加入從服務(wù)器:

    此過程要先把主服務(wù)器的數(shù)據(jù)備份出來,然后拷貝到從服務(wù)器上導(dǎo)入到從服務(wù)器的數(shù)據(jù)庫,然后在讓從服務(wù)器從主服務(wù)器的指定位置開始備份數(shù)據(jù)即可。

   主服務(wù)器導(dǎo)出數(shù)據(jù):

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    

    

    從服務(wù)器導(dǎo)入數(shù)據(jù):

    [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;  #指定同步主服務(wù)器的用戶、密碼、日志文件和開始的編號(hào)

    QueryOK, 0 rows affected (0.07 sec)

     

    mysql>START SLAVE;  #啟動(dòng)同步進(jìn)程

    QueryOK, 0 rows affected (0.00 sec)

     

    mysql>SHOW SLAVE STATUS\G;  #查看狀態(tài)

    ***************************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 #看到這兩個(gè)顯示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)


    從服務(wù)器驗(yàn)證數(shù)據(jù)庫是否同步成功:

    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;  #已經(jīng)成功同步到主服務(wù)器的數(shù)據(jù)

        +------+

        | id   |

        +------+

        |    1 |

        |    2 |

        |    3 |

        +------+

        3 rows in set (0.00 sec)

   在主服務(wù)器新建一個(gè)數(shù)據(jù)庫并向之前的t1表中插入數(shù)據(jù)查看是否可以同步成功: 

   主服務(wù)器:

    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

    從服務(wù)器驗(yàn)證: 

    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)

    至此,從服務(wù)器已經(jīng)從主服務(wù)器完成同步之前的數(shù)據(jù),并可以同步新的數(shù)據(jù)


注:操作過程當(dāng)中遇到兩個(gè)問題,如下:

1、主服務(wù)器的數(shù)據(jù)庫不能備份,執(zhí)行命令式報(bào)錯(cuò):

[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)

    問題分析:改錯(cuò)誤是在調(diào)用 mysqldump命令是使用的默認(rèn)路徑/usr/bin/mysqldump,但是由于MariaDB是安裝的目前的最新版本,此備份工具已經(jīng)比較舊了,有些特性不支持,因此可以使用MariaDB解壓包里面的備份工具即可。


2、從服務(wù)器一直不能與主服務(wù)器同步

具體表現(xiàn)為從服務(wù)器一直顯示IO進(jìn)程連接正在連接:

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

查看日志先顯示賬號(hào)訪問主服務(wù)器失敗,那么就判斷為要么沒有權(quán)限要么密碼不對(duì),在確認(rèn)權(quán)限正確的情況下更改密碼,然后將從服務(wù)器的slave停止,使用新密碼重新啟動(dòng)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


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI