Mysql

关注公众号 jb51net

关闭
首页 > 数据库 > Mysql > MySQL8.0高可用MIC

MySQL8.0高可用MIC的实现

作者:爱喝旺仔的昊昊

本文介绍了如何实现MySQL8.0高可用MIC,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

一、机器准备

机器

主机名

ip地址

角色

软件

mic-master

192.168.252.148

主节点

mysql8.0.23 mysql-shell-8.0.23

mic-node1

192.168.252.142

node1

mysql8.0.23

mic-node2

192.168.252.145

node2

mysql8.0.23

关闭防火墙

systemctl stop firewalld
setenforce 0

二、环境准备

安装mysql8.0.23

官方地址:MySQL :: Begin Your Download

三台机器均安装

1.安装工具
yum -y install perl-JSON perl-Test-Simple
2.获取安装包
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-community-server-8.0.23-1.el7.x86_64.rpm
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-community-client-8.0.23-1.el7.x86_64.rpm
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-community-client-plugins-8.0.23-1.el7.x86_64.rpm
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-community-devel-8.0.23-1.el7.x86_64.rpm
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-community-common-8.0.23-1.el7.x86_64.rpm
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-community-libs-8.0.23-1.el7.x86_64.rpm 
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-community-libs-compat-8.0.23-1.el7.x86_64.rpm
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-community-embedded-compat-8.0.23-1.el7.x86_64.rpm
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-community-test-8.0.23-1.el7.x86_64.rpm
3.安装
rpm -ivh mysql-community-common-8.0.23-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-plugins-8.0.23-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.23-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-8.0.23-1.el7.x86_64.rpm
rpm -ivh mysql-community-devel-8.0.23-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-compat-8.0.23-1.el7.x86_64.rpm
rpm -ivh mysql-community-embedded-compat-8.0.23-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.0.23-1.el7.x86_64.rpm
rpm -ivh mysql-community-test-8.0.23-1.el7.x86_64.rpm

修改配置文件

所有节点修改进行修改配置文件

添加配置的模版
server_id=[id]  #每一台的都不一样,要唯一
report_host=[当前服务器ip]
report_port=[mysql端口号]
loose-group_replication_ip_whitelist="[ip1],[ip2],[ip3]"

进行修改

vim /etc/my.cnf

192.168.252.148:

server-id=1
report_host=192.168.252.148
report_port=3306
loose-group_replication_ip_whitelist="192.168.252.148,192.168.252.142,192.168.252.145"

192.168.252.142:

server-id=2
report_host=192.168.252.142
report_port=3306
loose-group_replication_ip_whitelist="192.168.252.148,192.168.252.142,192.168.252.145"

192.168.252.145:

server-id=3
report_host=192.168.252.145
report_port=3306
loose-group_replication_ip_whitelist="192.168.252.148,192.168.252.142,192.168.252.145"

启动并修改密码

systemctl start mysqld

192.168.252.148:

cat /var/log/mysqld.log | grep "password"
mysqladmin -uroot -p'Cu)ghjzfK3_J' password @Syh2025659

192.168.252.142:

cat /var/log/mysqld.log | grep "password"
mysqladmin -uroot -p'*qywTIZHh8-j' password '@Syh2025659'

192.168.252.145:

cat /var/log/mysqld.log | grep "password"
mysqladmin -uroot -p'Z+Ly31m>Y#sy' password '@Syh2025659'

主服务器安装mysql-shell-8.0.23

下载链接:https://downloads.mysql.com/archives/get/p/43/file/mysql-shell-8.0.23-linux-glibc2.12-x86-64bit.tar.gz

wget https://downloads.mysql.com/archives/get/p/43/file/mysql-shell-8.0.23-linux-glibc2.12-x86-64bit.tar.gz
解压:
tar -xzf mysql-shell-8.0.23-linux-glibc2.12-x86-64bit.tar.gz -C /usr/local/
cd /usr/local	
mv mysql-shell-8.0.23-linux-glibc2.12-x86-64bi mysql-shell

三、终端操作

MySQL操作

创建用户并授权

三台机器均添加

1.创建用户
create user syh identified by '@Syh2025659';
2.授权给用户
GRANT BACKUP_ADMIN, CLONE_ADMIN, CREATE USER, EXECUTE, FILE, PERSIST_RO_VARIABLES_ADMIN, PROCESS, RELOAD, REPLICATION CLIENT, REPLICATION SLAVE, SELECT, SHUTDOWN, SUPER, SYSTEM_VARIABLES_ADMIN ON *.* TO 'syh'@'%' WITH GRANT OPTION;
GRANT DELETE, INSERT, UPDATE ON mysql.* TO 'syh'@'%' WITH GRANT OPTION;
GRANT ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE TEMPORARY TABLES, CREATE VIEW, DELETE, DROP, EVENT, EXECUTE, INDEX, INSERT, LOCK TABLES, REFERENCES, SHOW VIEW, TRIGGER, UPDATE ON mysql_innodb_cluster_metadata.* TO 'syh'@'%' WITH GRANT OPTION;
GRANT ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE TEMPORARY TABLES, CREATE VIEW, DELETE, DROP, EVENT, EXECUTE, INDEX, INSERT, LOCK TABLES, REFERENCES, SHOW VIEW, TRIGGER, UPDATE ON mysql_innodb_cluster_metadata_bkp.* TO 'syh'@'%' WITH GRANT OPTION;
GRANT ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE TEMPORARY TABLES, CREATE VIEW, DELETE, DROP, EVENT, EXECUTE, INDEX, INSERT, LOCK TABLES, REFERENCES, SHOW VIEW, TRIGGER, UPDATE ON mysql_innodb_cluster_metadata_previous.* TO 'syh'@'%' WITH GRANT OPTION;
GRANT ALL ON *.* to 'syh'@'%';
GRANT ALL PRIVILEGES on *.* to 'syh'@'%' WITH GRANT OPTION;
3.刷新权限
FLUSH PRIVILEGES;

192.168.252.148:

192.168.252.142:

192.168.252.145:

MySQL-Shell操作

进入mysql-shell

/usr/local/mysql-shell/bin/mysqlsh   #启动

连接主库信息

\c syh@192.168.252.148:3306     

配置集群

dba.configureInstance('syh@192.168.252.148:3306');
dba.configureInstance('syh@192.168.252.142:3306');
dba.configureInstance('syh@192.168.252.145:3306');

创建一个集群

var cluster=dba.createCluster('mysqlcluster');

查看主机群状态

dba.getCluster().status();

添加两个从节点

var cluster=dba.getCluster();
cluster.addInstance('syh@192.168.252.142:3306');
cluster.addInstance('syh@192.168.252.145:3306');

查看节点

cluster.status();

清空集群

dba.dropMetadataSchema(); 清除所有集群

四、故障恢复

安装mysql-router

192.168.252.148:

wget https://downloads.mysql.com/archives/get/p/41/file/mysql-router-8.0.23-linux-glibc2.12-x86_64.tar.xz
tar xf mysql-router-8.0.23-linux-glibc2.12-x86_64.tar.xz -C /usr/local
cd /usr/local
mv mysql-router-8.0.23-linux-glibc2.12-x86_64 mysql-router

修改配置文件

首次启动

192.168.252.148:

cd /usr/local/mysql-router/bin
./mysqlrouter --bootstrap syh@192.168.252.148:3306 --user=root

修改配置文件

vim /usr/local/mysql-router/mysqlrouter.conf
#dynamic_state=/usr/local/mysql-router/bin/../var/lib/mysqlrouter/state.json
bootstrap_server_addresses=mysql://192.168.252.148:3306,mysql://192.168.252.142:3306,mysql://192.168.252.145:3306

启动

192.168.252.148:

cd /usr/local/mysql-router/bin
./mysqlrouter -c ../mysqlrouter.conf &
netstat -tnpl

停掉主库

192.168.252.148:

systemctl stop mysqld

操作mysql-shell

192.168.252.148:

/usr/local/mysql-shell/bin/mysqlsh
\c syh@192.168.252.145:3306
var cluster=dba.getCluster();
cluster.status();

192.168.252.148:

systemctl start mysqld

再次查看

恢复!

到此这篇关于MySQL8.0高可用MIC的实现的文章就介绍到这了,更多相关MySQL8.0高可用MIC内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文