MariaDB Master-Master 復(fù)制服務(wù)器,可提高速度并減少延遲。使用replication功能,兩個獨立的 MySQL 服務(wù)器充當(dāng)一個集群。服務(wù)器相互同步,以便在發(fā)生故障時,其他服務(wù)器可以接管并且不會丟失數(shù)據(jù)。
環(huán) 境
OS:CentOS 8.5
MariaDB: MariaDB 10.3.28
兩臺主機(jī)名稱如下:
Hostname: MasterA ,IP:192.168.232.130
Hostname: MasterB ,IP:192.168.232.131
安裝MariaDB
使用下面命令在兩天服務(wù)器中安裝mariadb服務(wù):
# MasterA中安裝:
[root@MasterA ~]# yum -y install mariadb mariadb-server
# MasterB中安裝:
[root@MasterB ~]# yum -y install mariadb mariadb-server
啟動mariadb服務(wù):
[root@MasterA ~]# systemctl enable mariadb --now
[root@MasterB ~]# systemctl enable mariadb --now

編輯my.cnf配置文件
編輯/etc/my.cnf.d/mariadb-server.cnf配置文件
修改MasterA節(jié)點的mariadb-server.cnf配置文件:
[root@MasterA my.cnf.d]# vim mariadb-server.cnf
在mysqld部分下面添加server-id,log-bin 和 log-basename

保存配置,并重啟MasterA的MariaDB服務(wù)。
[root@MasterA ~]# systemctl restart mariadb
在MasterA的數(shù)據(jù)庫中創(chuàng)建一個帳戶,用戶名為replica_user,密碼為123456,指定slave的IP地址為192.168.232.131,也就是MasterB的IP地址。
MariaDB [(none)]> grant replication slave on *.* to 'replica_user'@192.168.232.131 identified by '123456';
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.001 sec)

修改MasterB節(jié)點的mariadb-server.cnf配置文件:
[root@MasterB ~]# vim /etc/my.cnf.d/mariadb-server.cnf
在mysqld部分下面添加server-id,log-bin 和 log-basename

保存配置,并重啟MasterA的MariaDB服務(wù)。
[root@MasterA ~]# systemctl restart mariadb
在MasterB的數(shù)據(jù)庫中創(chuàng)建一個帳戶,用戶名為replica_user,密碼為123456,指定slave的IP地址為192.168.232.130,也就是MasterA的IP地址。
MariaDB [(none)]> grant replication slave on *.* to 'replica_user'@192.168.232.130 identified by '123456';
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.001 sec)

配置雙主復(fù)制
首先進(jìn)入MasterA操作系統(tǒng),進(jìn)入數(shù)據(jù)庫,使用show master status;查看二進(jìn)制日志名稱和pos值:
[root@MasterA ~]# mysql -u root -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 19
Server version: 10.3.28-MariaDB-log MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show master status;
+---------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------------+----------+--------------+------------------+
| master01-bin.000004 | 5078 | | |
+---------------------+----------+--------------+------------------+
1 row in set (0.000 sec)
MariaDB [(none)]>

在MasterB系統(tǒng)中進(jìn)入數(shù)據(jù)庫,指定MasterA服務(wù)器的信息,并指定剛才從MasterA獲取的bin-log文件名和position值,并啟動slave:
MariaDB [(none)]> change master to master_host='192.168.232.130',
-> master_user='replica_user',
-> master_password='123456',
-> master_log_file='master01-bin.000004',
-> master_log_pos=5078;
Query OK, 0 rows affected (0.007 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.002 sec)

查看slave狀態(tài)是否有報錯:

看到上圖中,Slave_IO_Running和Slave_SQL_Running?都為yes,Last_Error沒有錯誤信息。
其次,在MasterB的數(shù)據(jù)庫中查詢master相關(guān)信息:
MariaDB [(none)]> show master status;
+---------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------------+----------+--------------+------------------+
| master02-bin.000001 | 1080 | | |
+---------------------+----------+--------------+------------------+
1 row in set (0.000 sec)

在MasterA系統(tǒng)中進(jìn)入數(shù)據(jù)庫,指定MasterB服務(wù)器的信息,并指定剛才從MasterB獲取的bin-log文件名和position值,并啟動slave:
MariaDB [(none)]> change master to master_host='192.168.232.131',
-> master_user='replica_user',
-> master_password='123456',
-> master_log_file='master02-bin.000001',
-> master_log_pos=1080;
Query OK, 0 rows affected (0.010 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.002 sec)

查看slave狀態(tài)是否有報錯:

?看到上圖中,Slave_IO_Running和Slave_SQL_Running?都為yes,Last_Error沒有錯誤信息。
任意一臺數(shù)據(jù)庫,創(chuàng)建數(shù)據(jù)庫后,另一臺也可以看到了。下面實在MasterB中創(chuàng)建的數(shù)據(jù)庫:?
MariaDB [test_replica]> create database mydb;
Query OK, 1 row affected (0.000 sec)

在MasterA中查看是否有mydb數(shù)據(jù)庫:

下面是在MasterA中創(chuàng)建數(shù)據(jù)庫:
MariaDB [(none)]> create database mydb_02;
Query OK, 1 row affected (0.000 sec)
在MasterB中查看是否有mydb_02數(shù)據(jù)庫:

下面實例將MasterA數(shù)據(jù)庫中的test_replica庫備份,并導(dǎo)入到MasterB的數(shù)據(jù)庫中,然后在MasterB中的數(shù)據(jù)庫中添加數(shù)據(jù),查看是否會同步:

[root@MasterA ~]# mysqldump -u root -p123456 test_replica > a.sql
[root@MasterA ~]# scp a.sql root@192.168.232.131:~
切換到MasterB操作系統(tǒng),創(chuàng)建一個數(shù)據(jù)庫名稱為test_replica:
[root@MasterB ~]# mysql -u root -p123456 -e 'create database test_replica;'
[root@MasterB ~]# mysql -u root -p123456 -e 'show databases;'

將MasterA到處的數(shù)據(jù)導(dǎo)入到MasterB系統(tǒng)中的test_replica庫中:
[root@MasterB ~]# mysql -u root -p123456 test_replica < a.sql

看到已經(jīng)導(dǎo)入了數(shù)據(jù)表。
下面在MasterB中,進(jìn)入test_replica庫,向Admins表添加數(shù)據(jù),然后在MasterA中查看是否也存在同樣數(shù)據(jù):
MariaDB [test_replica]> insert into Admins values ('user01','123');
Query OK, 1 row affected (0.002 sec)

在MasterA中查看test_replica數(shù)據(jù)庫中的Admins表:
[root@MasterA ~]# mysql -u root -p123456 -e 'select * from test_replica.Admins;'
+--------+------+
| Aname | Apwd |
+--------+------+
| admin | 123 |
| user01 | 123 |
+--------+------+

可以看到數(shù)據(jù)存在。下面在MasterA中向Admins表添加數(shù)據(jù),查看MasterB數(shù)據(jù)庫中是否會同步:
MariaDB [test_replica]> insert into Admins values ('user02','321') ;
Query OK, 1 row affected (0.002 sec)

在MasterB中也可以看到剛剛創(chuàng)建的信息:
[root@MasterB ~]# mysql -u root -p123456 -e 'select * from test_replica.Admins;'
+--------+------+
| Aname | Apwd |
+--------+------+
| admin | 123 |
| user01 | 123 |
| user02 | 321 |
+--------+------+

這樣就完成啦。