文章詳情頁
MariaDB配置雙主復(fù)制方案
瀏覽:208日期:2023-03-30 13:21:15
本文環(huán)境
Debian 8
MariaDB 10.1.21
配置文件 1
修改服務(wù)器 1 上 mysql 配置文件 /etc/mysql/my.cnf
有些配置默認就存在的,如果你有潔癖,請先搜索,再添加配置項。
[mysqld] server-id= 1 log_bin = /var/log/mysql/mariadb-bin log_bin_index = /var/log/mysql/mariadb-bin.index binlog-do-db= tudou1 #需要同步的數(shù)據(jù)庫,這里同步tudou1和tudou2兩個數(shù)據(jù)庫 binlog-do-db= tudou2 binlog-ignore-db = mysql #忽略同步的數(shù)據(jù)庫 log_slave_updates #把從庫的寫操作記錄到binlog中 (缺少之后,雙主創(chuàng)建失敗) expire_logs_days = 365 #日志文件過期天數(shù),默認是 0,表示不過期 auto_increment_increment= 2 #設(shè)定為主服務(wù)器的數(shù)量,防止auto_increment字段重復(fù) auto_increment_offset = 1 #自增長字段的初始值,在多臺master環(huán)境下,不會出現(xiàn)自增長ID重復(fù)
配置文件 2
[mysqld] [mysqld] server-id= 2 log_bin = /var/log/mysql/mariadb-bin log_bin_index = /var/log/mysql/mariadb-bin.index binlog-do-db = tudou1 #需要同步的數(shù)據(jù)庫,這里同步tudou1和tudou2兩個數(shù)據(jù)庫 binlog-do-db = tudou2 binlog-ignore-db = mysql #忽略同步的數(shù)據(jù)庫 log_slave_updates #把從庫的寫操作記錄到binlog中 (缺少之后,雙主創(chuàng)建失敗) expire_logs_days = 365 #日志文件過期天數(shù),默認是 0,表示不過期 auto_increment_increment= 2 #設(shè)定為主服務(wù)器的數(shù)量,防止auto_increment字段重復(fù) auto_increment_offset = 2 #自增長字段的初始值,在多臺master環(huán)境下,不會出現(xiàn)自增長ID重復(fù)
注意:
log slave updates 表示把從庫的寫操作記錄到binlog中,缺少之后,雙主創(chuàng)建失敗。雙主同步時該項必須有
binlog-do-db 需要同步的數(shù)據(jù)庫,可寫多個
binlog-ignore-db 表示忽略同步的數(shù)據(jù)庫
創(chuàng)建同步賬戶
// 服務(wù)器 1 GRANT REPLICATION SLAVE ON *.* TO "repuser"@"server-2" IDENTIFIED BY "repuser"; FLUSH PRIVILEGES; // 服務(wù)器 2 GRANT REPLICATION SLAVE ON *.* TO "repuser"@"server-1" IDENTIFIED BY "repuser"; FLUSH PRIVILEGES;
可以順便在另一臺服務(wù)器測試能不能登錄,如果不能,把 bind-address 那行注釋掉即可。
$ mysql -urepuser -prepuser -hserver-1
查看 master 狀態(tài)
服務(wù)器 1 中
MariaDB [mysql]> show master status; +--------------------+----------+--------------+------------------+ | File| Position | Binlog_Do_DB | Binlog_Ignore_DB | +--------------------+----------+--------------+------------------+ | mariadb-bin.000514 | 639 | xxxxxxxx | mysql | +--------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
服務(wù)器 2 中
MariaDB [mysql]> show master status; +--------------------+----------+--------------+------------------+ | File| Position | Binlog_Do_DB | Binlog_Ignore_DB | +--------------------+----------+--------------+------------------+ | mariadb-bin.000006 | 1057 | xxxxxxxx | mysql | +--------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
設(shè)置同步
// 服務(wù)器 2 MariaDB [mysql]> CHANGE MASTER TO MASTER_HOST="server-1",MASTER_PORT=3306,MASTER_USER="repuser",MASTER_PASSWORD="repuser",MASTER_LOG_FILE="mariadb-bin.000514",MASTER_LOG_POS=639; MariaDB [mysql]> START SLAVE; // 服務(wù)器 1 MariaDB [mysql]> CHANGE MASTER TO MASTER_HOST="server-2",MASTER_PORT=3306,MASTER_USER="repuser",MASTER_PASSWORD="repuser",MASTER_LOG_FILE="mariadb-bin.000006",MASTER_LOG_POS=1057; MariaDB [mysql]> START SLAVE; // 完畢之后,分別執(zhí)行 MariaDB [mysql]> SHOW SLAVE STATUS\G
如出現(xiàn)以下兩項,則說明配置成功!
Slave_IO_Running: Yes Slave_SQL_Running: Yes
雙主同步測試
在服務(wù)器 1 數(shù)據(jù)庫中創(chuàng)建一個表,看看服務(wù)器 2 會不會出現(xiàn),按照上面教程,如果沒問題的話,就是可以同步的。
標簽:
MariaDB
相關(guān)文章:
1. debian10 mariadb安裝過程詳解2. Mysql/MariaDB啟動時處于進度條狀態(tài)導(dǎo)致啟動失敗的原因及解決辦法3. centos編譯安裝mariadb的詳細過程4. MySQL主從復(fù)制延遲原因以及解決方案5. 關(guān)于MariaDB安裝問題小記(CMake Error at)6. MySQL主從復(fù)制原理以及需要注意的地方7. centos中找回MariaDB數(shù)據(jù)庫root用戶權(quán)限的方法8. 淺談MySQL和mariadb區(qū)別9. MariaDB數(shù)據(jù)庫的外鍵約束實例詳解10. 目前學(xué)習(xí)到的常用命令之Mariadb
排行榜
