1. 安裝MySQL
1.1 MySQL 下載
-
下載mysql安裝
https://dev.mysql.com/downloads/mysql/
1.2 安裝MySQL8.0
在三個節(jié)點均執(zhí)行以下操作:
- 查看mariadb并移除
# 1滔韵、查看 mariadb 的安裝包
rpm -qa | grep mariadb </pre>
# 2奋构、卸載mariadb 需要管理員權(quán)限,否在會報錯
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ù)庫密碼
cat /var/log/mysqld.log | grep password
# 3媒熊、登錄mysql密碼直接復(fù)制粘貼上面隨機的密碼
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ù)庫
# 1、嘗試用navicat連接數(shù)據(jù)庫葛账,但需要關(guān)閉firewall
systemctl stop firewalld.service;
systemctl disable firewalld.service;
systemctl mask firewalld.service;
# 2怜校、安裝 iptables 防火墻
yum -y install iptables-services
# 3、啟動設(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è)置防火墻開機啟動
systemctl enable iptables.service
# 7巩割、ifconfig 命令查看 ip
使用navicat本地測試連接
2. 搭建MGR群
2.1 環(huán)境信息
- 服務(wù)器基本信息
主機 | 操作系統(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映射信息
# 在三個節(jié)點每個虛擬機上均要配置
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配置文件信息(三個節(jié)點均需配置)
修改每個節(jié)點的配置,3個節(jié)點除了server_id付燥、loose-group_replication_local_address參數(shù)不一樣外宣谈,其他保持一致。注意:其中33061键科,33062闻丑,33063并非mysql服務(wù)端口號。
# 打開編輯配置文件
vim /etc/my.cnf
# 具體配置信息內(nèi)容如下(以第一個節(jié)點為例):
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
第二個節(jié)點修改內(nèi)容如下:
server_id=2
loose-group_replication_local_address= "node2.mgr.com:33062"
第三個節(jié)點修改內(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ù)制賬號(三個節(jié)點均需配置)
# 設(shè)置復(fù)制賬號
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 啟動MGR單主模式
2.3.1 啟動MGR
- 主節(jié)點執(zhí)行命令
# 1勋颖、啟動MGR嗦嗡,在主庫(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時饭玲,報錯如下:
[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é)點執(zhí)行命令
# 1、其他節(jié)點加入MGR次哈,在從庫(172.20.10.13,172.20.10.2)上執(zhí)行
mysql> START GROUP_REPLICATION;
# 2窑滞、查看MGR組信息
mysql> SELECT * FROM performance_schema.replication_group_members;
此中方式在查看MGR組信息時恢筝,2個從節(jié)點狀態(tài)一直處于RECOVING狀態(tài)障陶,通過cat /var/log/mysqld.log查看報錯信息如下:
[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,需打開公鑰訪問(在每個從節(jié)點執(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個節(jié)點狀態(tài)為online抱究,并且主節(jié)點為node1.mgr.com恢氯,只有主節(jié)點可以寫入,其他節(jié)點只讀鼓寺,MGR單主模式搭建成功勋拟。
2.3.2 簡單測試
- node1.mgr.com上創(chuàng)建測試庫、表妈候,并添加數(shù)據(jù)敢靡,測試從庫是否能同步數(shù)據(jù);在從節(jié)點node2.mgr.com苦银,node3.mgr.com寫入數(shù)據(jù)是否可以寫入啸胧。
# 1、在node1.mgr.com點執(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個節(jié)點查看均可查看到相同結(jié)果均有數(shù)據(jù)表和數(shù)據(jù)
mysql> select * from test.t1;
# 3纺念、在從節(jié)點測試寫入,驗證不支持寫入操作
- node1.mgr.com上創(chuàng)建測試庫想括、表陷谱,并添加數(shù)據(jù),測試新節(jié)點接入到組中后瑟蜈,是否同步數(shù)據(jù)烟逊。
# 1、在node2.mgr.com上執(zhí)行铺根,從mgr組中去除
mysql> stop group_replication;
# 2宪躯、在node1.mgr.com或node3.mgr.com查看,僅剩2個節(jié)點
mysql> SELECT * FROM performance_schema.replication_group_members;
# 3夷都、在node1.mgr.com進(jìn)行寫操作眷唉,此時查看node2.mgr.com數(shù)據(jù)庫信息并沒有信息同步
# 4、將node2.mgr.com加入mgr組囤官,在node2.mgr.com執(zhí)行冬阳,之后在查看信息,數(shù)據(jù)庫信息已同步
mysql> start group_replication;
2.4 切換MGR模式(單到多党饮,多到單)
2.4.1 切換多主模式
MGR切換模式需要重新啟動組復(fù)制肝陪,在所有節(jié)點上先關(guān)閉組復(fù)制,設(shè)置 group_replication_single_primary_mode=OFF 等參數(shù)刑顺,再啟動組復(fù)制氯窍。
# 1饲常、停止組復(fù)制(所有節(jié)點執(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贝淤、隨便選擇某個節(jié)點執(zhí)行
mysql> SET GLOBAL group_replication_bootstrap_group=ON;
mysql> START GROUP_REPLICATION;
mysql> SET GLOBAL group_replication_bootstrap_group=OFF;
# 3播聪、其他節(jié)點執(zhí)行
mysql> START GROUP_REPLICATION;
# 4布隔、查看組信息衅檀,所有節(jié)點的 MEMBER_ROLE 都為 PRIMARY
mysql> SELECT * FROM performance_schema.replication_group_member
可以看到所有節(jié)點狀態(tài)都是online哀军,角色都是PRIMARY杉适,MGR多主模式搭建成功淘衙。
2.4.1.1 簡單測試
# 1彤守、在任意節(jié)點均可執(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個節(jié)點查看均可查看到相同結(jié)果
mysql> select * from test.t1;
2.4.2 切換單主模式
# 1筝蚕、所有節(jié)點執(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é)點(172.20.10.14)執(zhí)行
SET GLOBAL group_replication_bootstrap_group=ON;
START GROUP_REPLICATION;
SET GLOBAL group_replication_bootstrap_group=OFF;
# 3济榨、從節(jié)點(172.20.10.13擒滑、172.20.10.2)執(zhí)行
START GROUP_REPLICATION;
# 4、查看MGR組信息
mysql> SELECT * FROM performance_schema.replication_group_members;