1. 安裝MySQL
1.1 MySQL 下載
-
下載mysql安裝
https://dev.mysql.com/downloads/mysql/
1.2 安裝MySQL8.0
在三個(gè)節(jié)點(diǎn)均執(zhí)行以下操作:
- 查看mariadb并移除
# 1、查看 mariadb 的安裝包
rpm -qa | grep mariadb </pre>
# 2、卸載mariadb 需要管理員權(quán)限,否在會(huì)報(bào)錯(cuò)
rpm -e XXXXXX --nodeps
# 3、再次查看
rpm -qa | grep mariadb
- 安裝mysql
# 1、在/usr/local(存放本地的共享資源)目錄下創(chuàng)建mysql文件夾,通過ll查看目錄結(jié)構(gòu)
cd /usr/local
mkdir mysql
ll
# 2、上傳下載的mysql壓縮包文件到mysql文件下
# 3、解壓文件
tar -xvf mysql-8.0.23-1.el7.x86_64.rpm-bundle.tar</pre>
# 4、安裝common,libs, client,server
rpm -ivh mysql-community-common-8.0.23-1.el7.x86_64.rpm --nodeps --force
rpm -ivh mysql-community-libs-8.0.23-1.el7.x86_64.rpm --nodeps --force
rpm -ivh mysql-community-client-8.0.23-1.el7.x86_64.rpm --nodeps --force
rpm -ivh mysql-community-server-8.0.23-1.el7.x86_64.rpm --nodeps --force
# 5、檢查mysql安裝情況
rpm -qa | grep mysql
1.3 配置MySQL相關(guān)信息
# 1、初始化和配置mysql
mysqld --initialize;
chown mysql:mysql /var/lib/mysql -R;
systemctl start mysqld.service;
systemctl enable mysqld;
# 2、查看數(shù)據(jù)庫(kù)密碼
cat /var/log/mysqld.log | grep password
# 3、登錄mysql密碼直接復(fù)制粘貼上面隨機(jī)的密碼
mysql -uroot -p
# 4、修改mysql密碼為123456
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
# 5、進(jìn)行遠(yuǎn)程訪問的授權(quán)
create user 'root'@'%' identified with mysql_native_password by '123456';
grant all privileges on *.* to 'root'@'%' with grant option;
flush privileges;
# 6、修改加密規(guī)則
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER;
flush privileges;
exit; # 提出mysql登錄
1.4 Navicat連接數(shù)據(jù)庫(kù)
# 1、嘗試用navicat連接數(shù)據(jù)庫(kù),但需要關(guān)閉firewall
systemctl stop firewalld.service;
systemctl disable firewalld.service;
systemctl mask firewalld.service;
# 2、安裝 iptables 防火墻
yum -y install iptables-services
# 3、啟動(dòng)設(shè)置防火墻
systemctl enable iptables;
systemctl start iptables;
# 4、命令編輯防火墻 添加端口
vim /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8090 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 33061 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 33062 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 33063 -j ACCEPT
# 5、重啟防火墻使配置生效
systemctl restart iptables.service
# 6、設(shè)置防火墻開機(jī)啟動(dòng)
systemctl enable iptables.service
# 7、ifconfig 命令查看 ip
使用navicat本地測(cè)試連接
2. 搭建MGR群
2.1 環(huán)境信息
- 服務(wù)器基本信息
| 主機(jī) | 操作系統(tǒng) | IP與HostName映射 |
|---|---|---|
| 服務(wù)器1 | Centos7 | 172.20.10.14 node1.mgr.com |
| 服務(wù)器2 | Centos7 | 172.20.10.13 node2.mgr.com |
| 服務(wù)器3 | Centos7 | 172.20.10.2 node3.mgr.com |
- 設(shè)置hostname和ip映射信息
# 在三個(gè)節(jié)點(diǎn)每個(gè)虛擬機(jī)上均要配置
vi /etc/hosts
172.20.10.14 node1.mgr.com
172.20.10.13 node2.mgr.com
172.20.10.2 node3.mgr.com
-
修改mysql配置文件信息(三個(gè)節(jié)點(diǎn)均需配置)
修改每個(gè)節(jié)點(diǎn)的配置,3個(gè)節(jié)點(diǎn)除了server_id、loose-group_replication_local_address參數(shù)不一樣外,其他保持一致。注意:其中33061,33062,33063并非mysql服務(wù)端口號(hào)。
# 打開編輯配置文件
vim /etc/my.cnf
# 具體配置信息內(nèi)容如下(以第一個(gè)節(jié)點(diǎn)為例):
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
server_id=1
gtid_mode=ON
enforce_gtid_consistency=ON
binlog_checksum=NONE
log_bin=binlog
log_slave_updates=ON
binlog_format=ROW
master_info_repository=TABLE
relay_log_info_repository=TABLE
transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="aadaaaaa-adda-adda-aaaa-aaaaaaddaaaa"
loose-group_replication_start_on_boot=OFF
loose-group_replication_local_address= "node1.mgr.com:33061"
loose-group_replication_group_seeds= "node1.mgr.com:33061,node2.mgr.com:33062,node3.mgr.com:33063"
loose-group_replication_bootstrap_group=OFF
第二個(gè)節(jié)點(diǎn)修改內(nèi)容如下:
server_id=2
loose-group_replication_local_address= "node2.mgr.com:33062"
第三個(gè)節(jié)點(diǎn)修改內(nèi)容如下:
server_id=3
loose-group_replication_local_address= "node3.mgr.com:330</pre>
2.2 創(chuàng)建復(fù)制環(huán)境
- 創(chuàng)建復(fù)制賬號(hào)(三個(gè)節(jié)點(diǎn)均需配置)
# 設(shè)置復(fù)制賬號(hào)
mysql> SET SQL_LOG_BIN=0;
mysql> CREATE USER repl@'%' IDENTIFIED BY 'repl';
mysql> GRANT REPLICATION SLAVE ON *.* TO repl@'%';
mysql> FLUSH PRIVILEGES;
mysql> SET SQL_LOG_BIN=1;
mysql> CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='repl' FOR CHANNEL 'group_replication_recovery';
- 安裝MGR插件
# 安裝插件
mysql> install PLUGIN group_replication SONAME 'group_replication.so';
-- 查看group replication組件
mysql> show plugins;
2.3 啟動(dòng)MGR單主模式
2.3.1 啟動(dòng)MGR
- 主節(jié)點(diǎn)執(zhí)行命令
# 1、啟動(dòng)MGR,在主庫(kù)(172.20.10.14)上執(zhí)行
mysql> SET GLOBAL group_replication_bootstrap_group=ON;
mysql> START GROUP_REPLICATION;
mysql> SET GLOBAL group_replication_bootstrap_group=OFF;
# 2、查看MGR組信息
mysql> SELECT * FROM performance_schema.replication_group_members;
在 START GROUP_REPLICATION時(shí),報(bào)錯(cuò)如下:
[Repl] Plugin group_replication reported: '[GCS] Error connecting to the local group communication engine instance.'
[Repl] Plugin group_replication reported: '[GCS] The member was unable to join the group. Local port: 33061'
解決措施如下:
1.關(guān)閉SELinux,不太安全,不特別推薦
setenforce 0
2.開放通訊端口(推薦)
yum install -y policycoreutils-python
semanage port -a -t mysqld_port_t -p tcp 33061
- 從節(jié)點(diǎn)執(zhí)行命令
# 1、其他節(jié)點(diǎn)加入MGR,在從庫(kù)(172.20.10.13,172.20.10.2)上執(zhí)行
mysql> START GROUP_REPLICATION;
# 2、查看MGR組信息
mysql> SELECT * FROM performance_schema.replication_group_members;
此中方式在查看MGR組信息時(shí),2個(gè)從節(jié)點(diǎn)狀態(tài)一直處于RECOVING狀態(tài),通過cat /var/log/mysqld.log查看報(bào)錯(cuò)信息如下:
[ERROR] [MY-010584] [Repl] Slave I/O for channel 'group_replication_recovery': error connecting to master 'repl@node1.mgr.com:3306' - retry-time: 60 retries: 1 message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection. Error_code: MY-002061
[ERROR] [MY-011582] [Repl] Plugin group_replication reported: 'There was an error when connecting to the donor server. Please check that group_replication_recovery channel credentials and all MEMBER_HOST column values of performance_schema.replication_group_members table are correct and DNS resolvable.'
[ERROR] [MY-011583] [Repl] Plugin group_replication reported: 'For details please check performance_schema.replication_connection_status table and error log messages of Slave I/O for channel group_replication_recovery.'
解決措施如下:
mysql8.0之后加密規(guī)則變成 caching_sha2_password,需打開公鑰訪問(在每個(gè)從節(jié)點(diǎn)執(zhí)行)
mysql> set global group_replication_recovery_get_public_key=on;
mysql> start group replication;
再次查看mgr信息
mysql>SELECT * FROM performance_schema.replication_group_members;
3個(gè)節(jié)點(diǎn)狀態(tài)為online,并且主節(jié)點(diǎn)為node1.mgr.com,只有主節(jié)點(diǎn)可以寫入,其他節(jié)點(diǎn)只讀,MGR單主模式搭建成功。
2.3.2 簡(jiǎn)單測(cè)試
- node1.mgr.com上創(chuàng)建測(cè)試庫(kù)、表,并添加數(shù)據(jù),測(cè)試從庫(kù)是否能同步數(shù)據(jù);在從節(jié)點(diǎn)node2.mgr.com,node3.mgr.com寫入數(shù)據(jù)是否可以寫入。
# 1、在node1.mgr.com點(diǎn)執(zhí)行
mysql> CREATE DATABASE test;
mysql> USE test;
mysql> CREATE TABLE t1 (c1 INT PRIMARY KEY, c2 TEXT NOT NULL);
mysql> INSERT INTO t1 VALUES (1, 'Luis');
# 2、在3個(gè)節(jié)點(diǎn)查看均可查看到相同結(jié)果均有數(shù)據(jù)表和數(shù)據(jù)
mysql> select * from test.t1;
# 3、在從節(jié)點(diǎn)測(cè)試寫入,驗(yàn)證不支持寫入操作
- node1.mgr.com上創(chuàng)建測(cè)試庫(kù)、表,并添加數(shù)據(jù),測(cè)試新節(jié)點(diǎn)接入到組中后,是否同步數(shù)據(jù)。
# 1、在node2.mgr.com上執(zhí)行,從mgr組中去除
mysql> stop group_replication;
# 2、在node1.mgr.com或node3.mgr.com查看,僅剩2個(gè)節(jié)點(diǎn)
mysql> SELECT * FROM performance_schema.replication_group_members;
# 3、在node1.mgr.com進(jìn)行寫操作,此時(shí)查看node2.mgr.com數(shù)據(jù)庫(kù)信息并沒有信息同步
# 4、將node2.mgr.com加入mgr組,在node2.mgr.com執(zhí)行,之后在查看信息,數(shù)據(jù)庫(kù)信息已同步
mysql> start group_replication;
2.4 切換MGR模式(單到多,多到單)
2.4.1 切換多主模式
MGR切換模式需要重新啟動(dòng)組復(fù)制,在所有節(jié)點(diǎn)上先關(guān)閉組復(fù)制,設(shè)置 group_replication_single_primary_mode=OFF 等參數(shù),再啟動(dòng)組復(fù)制。
# 1、停止組復(fù)制(所有節(jié)點(diǎn)執(zhí)行):
mysql> stop group_replication;
mysql> set global group_replication_single_primary_mode=OFF;
mysql> set global group_replication_enforce_update_everywhere_checks=ON;
# 2、隨便選擇某個(gè)節(jié)點(diǎn)執(zhí)行
mysql> SET GLOBAL group_replication_bootstrap_group=ON;
mysql> START GROUP_REPLICATION;
mysql> SET GLOBAL group_replication_bootstrap_group=OFF;
# 3、其他節(jié)點(diǎn)執(zhí)行
mysql> START GROUP_REPLICATION;
# 4、查看組信息,所有節(jié)點(diǎn)的 MEMBER_ROLE 都為 PRIMARY
mysql> SELECT * FROM performance_schema.replication_group_member
可以看到所有節(jié)點(diǎn)狀態(tài)都是online,角色都是PRIMARY,MGR多主模式搭建成功。
2.4.1.1 簡(jiǎn)單測(cè)試
# 1、在任意節(jié)點(diǎn)均可執(zhí)行寫操作
mysql> CREATE DATABASE test;
mysql> USE test;
mysql> CREATE TABLE t1 (c1 INT PRIMARY KEY, c2 TEXT NOT NULL);
mysql> INSERT INTO t1 VALUES (1, 'Luis');
# 2、在3個(gè)節(jié)點(diǎn)查看均可查看到相同結(jié)果
mysql> select * from test.t1;
2.4.2 切換單主模式
# 1、所有節(jié)點(diǎn)執(zhí)行
mysql> stop group_replication;
mysql> set global group_replication_enforce_update_everywhere_checks=OFF;
mysql> set global group_replication_single_primary_mode=ON;
# 2、主節(jié)點(diǎn)(172.20.10.14)執(zhí)行
SET GLOBAL group_replication_bootstrap_group=ON;
START GROUP_REPLICATION;
SET GLOBAL group_replication_bootstrap_group=OFF;
# 3、從節(jié)點(diǎn)(172.20.10.13、172.20.10.2)執(zhí)行
START GROUP_REPLICATION;
# 4、查看MGR組信息
mysql> SELECT * FROM performance_schema.replication_group_members;