加入收藏 | 设为首页 | 会员中心 | 我要投稿 拼字网 - 核心网 (https://www.hexinwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

MYSQL Group Replication的搭建过程

发布时间:2022-01-18 23:09:41 所属栏目:MySql教程 来源:互联网
导读:这篇文章主要介绍MYSQL Group Replication的搭建过程,在日常操作中,相信很多人在MYSQL Group Replication的搭建过程问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答MYSQL Group Replication的搭建过程的疑惑有所帮助!接下
        这篇文章主要介绍“MYSQL Group Replication的搭建过程”,在日常操作中,相信很多人在MYSQL Group Replication的搭建过程问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”MYSQL Group Replication的搭建过程”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
 
1. 环境准备
操作系统redhat linux 6.8
虚拟机3台
一台用于primary, 另外两台用于做secondary
注意:
请先设置好各个主机的hostname与/etc/hosts
mysql版本: mysql-5.7.20
  主名        ip地址      在mgr中的角色
 
mgrhost01       192.168.43.143      primary
 
mgrhost02    192.168.43.144      seconde
 
mgrhost03    192.168.43.145      seconde
 
2. 安装mysql数据服务
 cd /tmp/
 wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
 
 tar -xvf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
 cd /usr/local/
 ln -s mysql-5.7.20-linux-glibc2.12-x86_64 mysql
 
注意:三个环境都需要安装
 
在三台主机上执行以下命令:
mkdir -p /database/mysql/data/3306
useradd mysql
chown -R mysql:mysql /database/mysql/data/3306
chown -R mysql:mysql /usr/local/mysql*
 
 
3.配置mysql服务
三台主机都增加配置文件: /etc/my.cnf
 
其中143的配置文件内容:
basedir=/usr/local/mysql/
 
datadir=/database/mysql/data/3306
 
port=3306
 
socket=/tmp/mysql.sock
 
server_id=143
 
gtid_mode=on
 
enforce_gtid_consistency=on
 
master_info_repository=table
 
relay_log_info_repository=table
 
binlog_checksum=none
 
log_slave_updates=on
 
log_bin=mysql-bin
 
binlog_format=row
 
relay-log-recovery=1
 
transaction_write_set_extraction=XXHASH64
 
loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
 
loose-group_replication_start_on_boot=off
 
loose-group_replication_local_address= "192.168.43.143:33060"
 
loose-group_replication_group_seeds= "192.168.43.143:33060,192.168.43.144:33060,192.168.43.145:33060"
 
loose-group_replication_bootstrap_group= off
 
192.168.43.144的/etc/my.cnf
 
[mysqld]
 
basedir=/usr/local/mysql/
 
datadir=/database/mysql/data/3306
 
port=3306
 
socket=/tmp/mysql.sock
 
server_id=144
 
gtid_mode=on
 
enforce_gtid_consistency=on
 
master_info_repository=table
 
relay_log_info_repository=table
 
binlog_checksum=none
 
log_slave_updates=on
 
log_bin=mysql-bin
 
binlog_format=row
 
 
relay-log-recovery=1
 
transaction_write_set_extraction=XXHASH64
 
loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
 
loose-group_replication_start_on_boot=off
 
loose-group_replication_local_address= "192.168.43.144:33060"
 
loose-group_replication_group_seeds= "192.168.43.143:33060,192.168.43.144:33060,192.168.43.145:3306"
 
loose-group_replication_bootstrap_group= off
 
192.168.43.145的/etc/my.cnf
[mysqld]
 
basedir=/usr/local/mysql/
 
datadir=/database/mysql/data/3306
 
port=3306
 
socket=/tmp/mysql.sock
 
server_id=145
 
gtid_mode=on
 
enforce_gtid_consistency=on
 
master_info_repository=table
 
relay_log_info_repository=table
 
binlog_checksum=none
 
log_slave_updates=on
 
log_bin=mysql-bin
 
binlog_format=row
 
relay-log-recovery=1
 
transaction_write_set_extraction=XXHASH64
 
loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
 
loose-group_replication_start_on_boot=off
 
loose-group_replication_local_address= "192.168.43.145:33060"
 
loose-group_replication_group_seeds= "192.168.43.143:33060,192.168.43.144:33060,192.168.43.145:3306"
 
loose-group_replication_bootstrap_group= off 在这里有一些技术细节要说明一下:
 
    上面的三个配置文件省略了所有不必要的配置项、但是看起来还是有点多、这些都是mgr环境要求的。
 
    server_id 每个实例都要不要样
 
    loose-group_replication_group_name:为mgr高可用组起一个名字,这个名字一定要是uuid格式的。
 
    loose-group_replication_local_address:mgr各实例之前都是要进行通信的、这个配置项设置的就是本实例所监听的ip:端口
 
    loose-group_replication_group_seeds:各mgr实例所监听的ip:端口信息 三个环境初始化mysql(注意,此处mysql的root用户密码会被设置为空值):
/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --datadir=/database/mysql/data/3306/ --user=mysql --initialize-insecure
 
三个环境将mysql加入系统服务
cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld
 
chkconfig mysqld on
 
修改环境变量:
echo 'PATH=/usr/local/mysql/bin/:$PATH' >>/etc/profile
 
启动mysql服务:
service mysqld start
 
停止mysql服务:
service mysqld stop
 
4.配置MGR
 
  4.1 创建mgr所需的用户(第一个节点)
  set sql_log_bin=0;
 
    create user mgruser@'%' identified by 'mtls@352';
 
    grant replication slave,replication client on *.* to mgruser@'%';
 
    create user mgruser@'127.0.0.1' identified by 'mtls@352';
 
    grant replication slave,replication client on *.* to mgruser@'127.0.0.1';
 
    create user mgruser@'localhost' identified by 'mtls@352';
 
    grant replication slave,replication client on *.* to mgruser@'localhost';
 
set sql_log_bin=1;
 
 4.2 配置复制所使用的用户
    change master to
 
    master_user='mgruser',
 
    master_password='123456'
 
    for channel 'group_replication_recovery';
  
 
4.3 安装MGR插件     install plugin group_replication soname 'group_replication.so';
4.4.  初始化一个复制组
    set global group_replication_bootstrap_group=on;
 
start group_replication;
 
set global group_replication_bootstrap_group=off;
   
 
4.5. 配置MGR的第二个节点
set sql_log_bin=0;
 
    create user mgruser@'%' identified by '123456';
 
    grant replication slave,replication client on *.* to mgruser@'%';
 
    create user mgruser@'127.0.0.1' identified by '123456';
 
    grant replication slave,replication client on *.* to mgruser@'127.0.0.1';
 
    create user mgruser@'localhost' identified by '123456';
 
    grant replication slave,replication client on *.* to mgruser@'localhost';
 
set sql_log_bin=1;
 
 change master to
 
    master_user='mgruser',
 
    master_password='123456'
 
    for channel 'group_replication_recovery';
 
install plugin group_replication soname 'group_replication.so'; start group_replication;
 
使用以上脚本依次处理剩余节点。
 
验证:
 
至此,配置完成
 
mgr中所有的结点都属于一个逻辑上的组、这个组就像是QQ群一样、是由群主建起来的、有了这个上组之后、其它的结点就可以加入到这个组中来了。
 
搭建中的注意项:
 
1.       安装虚拟机操作系统时,因hostname安装时未能设置好,修改了hostname, 重启了操作系统之后,group_replication不能正常启动
mysql> start group_replication;
 
set global group_replication_bootstrap_group=off;
 
ERROR 3094 (HY000): The START GROUP_REPLICATION command failed as the applier module failed to start.
 
 
在参数文件/etc/my.cnf中加入了relay-log-recovery=1之后,才正常启动复制
 
2. 安装mysql服务前,最好先设置好hostname与/etc/hosts
 
本案例中/etc/hosts的内容为:
 
192.168.43.143 mgrhost01
 
192.168.43.144 mgrhost02
 
192.168.43.145 mgrhost03
 
3. select * from performance_schema.replication_group_members ; 这个SQL查询的列的member_state字段应都为state才是正常的
到此,关于“MYSQL Group Replication的搭建过程”的学习就结束了,希望能够解决大家的疑惑。

(编辑:拼字网 - 核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!