上一篇 MySQL Group Replication 学习记录是用的 SQL 语句来创建 MGR集群,本篇将使用 mysql-shell 来创建并管理 MGR 集群。
安装
规划 3个 节点,如下
mgr-1 | 192.168.10.201 |
mgr-2 | 192.168.10.202 |
mgr-3 | 192.168.10.203 |
每个节点上都安装 mysql-shell 和 mysql-server
1 2 |
yum install -y https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm yum install -y mysql-community-server mysql-shell |
修改root用户密码
在每个节点上执行。
1 2 3 4 5 6 7 |
cat > /tmp/init.sql <<EOF ALTER USER 'root'@'localhost' IDENTIFIED BY '$MYSQL_ROOT' PASSWORD EXPIRE NEVER; ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '$MYSQL_ROOT'; create user root@'%' identified WITH mysql_native_password BY '$MYSQL_ROOT'; grant all privileges on *.* to root@'%' with grant option; flush privileges; EOF |
首先用 insecure
模式初始化数据库,然后执行密码修改。注意要在 /etc/my.cnf
中设置 report_hosts
为 IP,这样就不需要在 /etc/hosts
中解析主机名了。
1 2 3 4 5 6 7 8 9 10 |
rm -fr /var/lib/mysql mysqld --initialize-insecure --user=mysql systemctl start mysqld # (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2),重启生成 sock 文件 grep "report_host" /etc/my.cnf || echo "report_host=$MYIP" >> /etc/my.cnf systemctl restart mysqld systemctl status mysqld mysql -uroot -e "show databases;" mysql -uroot < /tmp/init.sql |
配置本地实例
在每个节点上执行。
1 2 3 4 5 6 7 |
cat << EOF > /tmp/config_local_instance.js dba.configureLocalInstance('root@$MYIP:3306', {'password': 'root', 'interactive': false}) EOF # 配置本地实例 mysqlsh --js --file=/tmp/config_local_instance.js systemctl restart mysqld |
检查配置
在每个节点上执行。
1 2 3 4 5 6 |
cat << EOF > /tmp/check_local_instance.js shell.connect('root@192.168.10.201:3306', 'root') dba.checkInstanceConfiguration() EOF # 检查本地实例 mysqlsh --js --file=/tmp/check_local_instance.js |
创建MGR集群
等全部节点启动后,只在 mgr-1
节点上执行一次。
1 2 3 4 5 6 7 8 9 10 11 12 |
cat << EOF > /tmp/init_cluster.js shell.connect('root@192.168.10.201:3306', 'root') dba.createCluster('$CLUSTER_NAME', {'localAddress': '192.168.10.201','multiPrimary': true, 'force': true}) var cluster=dba.getCluster('$CLUSTER_NAME') cluster.addInstance('root@192.168.10.202:3306', {'localAddress': '192.168.10.202', 'password': 'root', 'recoveryMethod':'clone'}) cluster.addInstance('root@192.168.10.203:3306', {'localAddress': '192.168.10.203', 'password': 'root','recoveryMethod':'clone'}) EOF # 等全部节点启动后执行,只执行一次,js脚本中只在 201 节点上执行 if [ "$ID"x == "10203"x ];then mysqlsh --js --file=/tmp/init_cluster.js fi |
管理
切换到单主模式
以上创建的是多主模式的 MGR 集群。通过 mysql-shell 可以很方便的在单主和多主间切换。
1 2 |
var c = dba.getCluster() c.switchToSinglePrimaryMode() |
指定PRIMARY
宕机Primary恢复后,如果想指定其仍为 Primary,可以用如下代码。
1 2 |
var c = dba.getCluster() c.setPrimaryInstance('root@192.168.10.101') |
参考资料
1 2 3 |
1. https://jeremyxu2010.github.io/2019/05/mysql-innodb-cluster实战/ 2. https://dev.mysql.com/doc/dev/mysqlsh-api-javascript/8.0/classmysqlsh_1_1dba_1_1_dba.html |
发表回复