mysql cluster搭建和router配置实验操作

一、三台云服务器准备(实验环境为RedHat7.4)
1、关闭防火墙
systemctl disable firewalld
systemctl stop firewalld
2、关闭SElinux
vi /etc/selinux/config
SELINUX=disable
3、关闭iptables
iptables -F
4、设置hostname
server11
5、设置hosts
192.168.1.11 server11
192.168.1.12 server12
192.168.1.13 server13
6、删除软件干扰实验软件如:
rpm -e postfix
rpm -e mariadb-libs
7、复制安装软件
mysql-commercial-common-8.0.18-1.1.el7.x86_64.rpm
mysql-commercial-libs-8.0.18-1.1.el7.x86_64.rpm
mysql-commercial-client-8.0.18-1.1.el7.x86_64.rpm
mysql-commercial-server-8.0.18-1.1.el7.x86_64.rpm
rpm -ivh mysql*
二、配置mysql
1、启动服务
systemctl start mysqld
systemctl enable mysqld
2、查看初始密码:grep password /var/log/mysqld.log
3、首次登录:mysql -p'V!qo>!2AWkX='
4、登录后改密码:alter user user() identified by 'Qaz_1234';
5、mysql -pQaz_1234
三、搭建mycluster
Server11: 有数据
Server12:没有数据
Server13:没有数据

1、在每个mysqld中分别创建用户账号,并指定权限:

set sql_log_bin=0;
create user 'g_repl'@'192.168.1.%' identified by 'Qaz_1234';
GRANT ALL ON *.* TO 'g_repl'@'192.168.1.%' WITH GRANT OPTION;
set sql_log_bin=1;

2、安装MySQL SHELL(不用每台装,有一台就可以),运行命令mysqlsh,分别检查每个mysqld是否满足条件,能够称为Cluster中的成员
mysqlsh -u g_repl -pQaz_1234 -h Server11
dba.checkInstanceConfiguration('g_repl@192.168.1.11',{password:"Qaz_1234"});
dba.checkInstanceConfiguration('g_repl@192.168.1.12',{password:"Qaz_1234"});
dba.checkInstanceConfiguration('g_repl@192.168.1.13',{password:"Qaz_1234"});
3、如果一个mysqld不满足要求,执行下面的函数解决问题:
dba.configureInstance('g_repl@192.168.0.215',{password:'Qaz_1234'})
5、利用mysqlsh连接第一个mysqld,创建集群
dba.createCluster('myCluster')
6、把其他mysqld加入集群
var cluster = dba.getCluster()
cluster.status()
cluster.addInstance('g_repl@192.168.1.12',{recoveryMethod:'clone',waitRecovery:1,password:"Qaz_1234"})
cluster.addInstance('g_repl@192.168.1.13',{recoveryMethod:'clone',waitRecovery:1,password:"Qaz_1234"})
cluster.status()
至此完成集群搭建
四、全部关机后重启集群方法
mysqlsh -u g_repl -pQaz_1234 -h Server11
执行命令重启集群:
dba.rebootClusterFromCompleteOutage('myCluster')
查看Cluster的状态:
var cluster = dba.getCluster()
cluster.status()
shell退出: \exit
启动router
mysqlrouter -c /run/mysqlrouter/mysqlrouter.conf&
五、cluster的维护:
var cluster = dba.getCluster()
cluster.status()
cluster.describe()
cluster. addInstance('g_repl@192.168.0.214',{password:'Qaz_1234'})
cluster.dissolve()
cluster.switchToMultiPrimaryMode()
cluster.switchToSinglePrimaryMode('g_repl@192.168.1.11')
从cluster中删除一个成员:
cluster.removeInstance('g_repl@192.168.1.14')
cluster.setInstanceOption(instance, option, value)
cluster.setInstanceOption('g_repl@192.168.1.14','memberWeight',30);
增加一个新成员:
cluster.addInstance('g_repl@192.168.1.14',{password:"Qaz_1234"})

六、把router集成到Cluster中
1、在客户端安装router软件(需要用最新版8.0.22)
2、对mysqlrouter用户解锁
usermod -s /bin/bash mysqlrouter
mkdir /var/lib/mysqlrouter
chown mysqlrouter /var/lib/mysqlrouter
passwd mysqlrouter
3、mysqlrouter用户登录操作系统,连接其中一个Cluster成员,获得集群的结构信息,写入本地文件
mysqlrouter --bootstrap g_repl@192.168.1.11:3306 --directory /var/run/mysqlrouter --conf-use-sockets --account routerfriend
如果配置文件不存在,重新执行下面的命令:
mysqlrouter --bootstrap g_repl@192.168.1.11:3306 --directory /var/run/mysqlrouter --conf-use-sockets --force
4、启动router服务
mysqlrouter -c /run/mysqlrouter/mysqlrouter.conf&
5、为了测试,在mysqld中创建用户账号,比如smith@'192.168.1.%'; 并指定权限
在primary中创建
create user smith@'192.168.1.%' identified by 'Qaz_1234';
grant all on *.* to smith@'192.168.1.%';
6、客户端通过连接router,连接Cluster的成员
mysql -u smith -pQaz_1234 -h 127.0.0.1 -P 6446
mysql -u smith -pQaz_1234 -h 127.0.0.1 -P 6447
mysql -u smith -pQaz_1234 -h 192.168.1.11 -P 6446
mysql> show variables like 'hostname';

router的两个端口:
6446
6447

七、测试:
mysql -u smith -pQaz_1234 -h 127.0.0.1 -P 6446 连接primary
查看变量:show variables like 'hostname'
测试关闭primary,观察切换
在客户端执行任意命令,重新连接新的primary

上一篇:redis主从同步收到以下参数影响


下一篇:Redis集群方案(高可用)之哨兵模式(一主二从三哨兵)