MariaDB系列之三:基於日誌(binlog)主主複製(Master-Master)

weixin_34232744發表於2019-02-22

title: MariaDB系列之三:基於日誌(binlog)主主複製(Master-Master)
categories: Linux
tags:
- MariaDB
- MySQL
timezone: Asia/Shanghai
date: 2019-02-01


環境

[root@centos181001 ~]# cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core)

MariaDB [(none)]> status
--------------
mysql  Ver 15.1 Distrib 5.5.60-MariaDB, for Linux (x86_64) using readline 5.1

節點1:11.11.11.61
節點2:11.11.11.62

第一步:關閉系統預設防火牆(by all)

setenforce 0
sed -i -r "/^SELINUX=/c SELINUX=disabled" /etc/selinux/config
which systemctl && systemctl stop firewalld
which systemctl && systemctl disable firewalld
which systemctl && systemctl stop iptables || service iptables stop
which systemctl && systemctl disable iptables || chkconfig iptables off

第二步:安裝MariaDB並設定開機自動啟動(by all)

# 1.安裝
yum install mariadb mariadb-server mariadb-libs mariadb-devel -y

# 2.啟動MariaDB並設定開機自動啟動
systemctl start mariadb
systemctl status mariadb
systemctl enable mariadb

# 3.初始化資料庫
mysql_secure_installation
    Enter current password for root (enter for none):       # 輸入密碼,預設為空
    Set root password? [Y/n] y                              # 是否設定root密碼
    Remove anonymous users? [Y/n] y                         # 是否移除anonymous使用者
    Disallow root login remotely? [Y/n] n                   # 是否禁止遠端登入
    Remove test database and access to it? [Y/n] y          # 是否移除預設的演示資料庫
    Reload privilege tables now? [Y/n] y                    # 是否重新載入許可權表?

第三步:修改配置檔案(by all)

注意根據節點1和節點2分別修改auto_increment_offset

1.兩個節點分別編輯配置檔案並在[mysqld]新增以下內容

vi /etc/my.cnf.d/server.cnf 

#任意自然數n,只要保證兩臺MySQL主機不重複就可以了。
server-id=11

#開啟二進位制日誌
log-bin=mysql-bin

# 步進值auto_imcrement。一般有n臺主MySQL就填n
auto_increment_increment=2

#起始值。一般填第n臺主MySQL。
auto_increment_offset=1
# auto_increment_offset=2

#忽略mysql庫
binlog-ignore=mysql

#忽略information_schema庫
binlog-ignore=information_schema   

#要同步的資料庫,預設所有庫
replicate-do-db=test

2.兩個節點分別重啟MariaDB服務

systemctl restart mariadb

3.兩個節點分別建立MySQL賬戶並授權對方伺服器可以訪問

節點1:
mysql -uroot -pxiaoliu
grant replication slave, replication client on *.* to 'admin'@'11.11.11.62' identified by 'xiaoliu';
flush privileges;

節點2:
mysql -uroot -pxiaoliu
grant replication slave, replication client on *.* to 'admin'@'11.11.11.61' identified by 'xiaoliu';
flush privileges;

4.兩個節點分別建立test庫

mysql -uroot -pxiaoliu
create database test;
commit;

5.兩臺伺服器分別檢視日誌檔案和Position並記錄

mysql -uroot -pxiaoliu
show master status;

6.兩臺伺服器分別設定

節點1:(填入對方伺服器的日誌檔案和Position)
change master to master_host='11.11.11.62',
master_user='admin',
master_password='xiaoliu',
master_port=3306,
master_log_file='mysql-bin.000001',
master_log_pos=592,
master_connect_retry=30;

節點2:(填入對方伺服器的日誌檔案和Position)
change master to master_host='11.11.11.61',
master_user='admin',
master_password='xiaoliu',
master_port=3306,
master_log_file='mysql-bin.000001',
master_log_pos=592,
master_connect_retry=30;

7.檢視同步狀態

# 1.檢視同步狀態
show slave status\G;

    # 可看到Slave_IO_State為空
    # Slave_IO_Runngin和Slave_SQL_Running是No
    # 表示Slave還是沒有開始複製過程。

# 2.開啟主從同步
start slave;

# 3.再次檢視狀態
show slave status\G;

    # 主要檢視以下3個狀態
       Slave_IO_State: Waiting for master to send event
     Slave_IO_Running: Yes
    Slave_SQL_Running: Yes

# 4.可以分別在主從節點檢視I/O執行緒建立的連線
show processlist\G;

第四步:測試

節點1執行以下操作,登入節點2檢視是否有同步過來:

use test; 
create table linux(username varchar(15) not null,password varchar(15) not null);
insert into linux values ('XiaoMing', 'xiaoliu');
commit;

節點2執行以下操作,登入節點1檢視是否有同步過來

use test; 
insert into linux values ('aaabbb', '123456');
commit;

相關文章