环境:centos7 、 Mysql8
准备三台服务器(2核 4G)
10.200.1.77 、 10.200.1.78 、 10.200.1.79
开放 3306 、 6446、6447 、6448 、6449 端口
确保三台服务器所有端口可相互访问
1、配置host:
三台上都配置etc/hosts:
10.200.1.77 n0
10.200.1.78 n1
10.200.1.79 n2
三台都关闭selinux
setenforce 0
三台分别执行:
n0:
hostnamectl set-hostname n0
n1:
hostnamectl set-hostname n1
n2:
hostnamectl set-hostname n2
2、Centos7安装Mysql8及组件
在https://dev.mysql.com/downloads/repo/yum/中找到对应版本的rpm文件下载路径
wget https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm
yum install -y mysql80-community-release-el7-7.noarch.rpm
安装Mysql、Mysql Shell、MysqlRouter
yum install -y mysql-community-server
yum install -y mysql-shell
yum install -y mysql-router
启动MySQL
service mysqld start;
查看初始密码:
cat /var/log/mysqld.log |grep password
登陆Mysql
mysql -uroot -p'初始密码'
配置账号
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'xxxxxxxxx'; #更新一下用户的密码
FLUSH PRIVILEGES; #刷新权限
mysql> use mysql;
mysql> update user set host='%' where user='root';
mysql> FLUSH PRIVILEGES;
mysql> grant all on *.* to 'root'@'%' with grant option;
mysql> FLUSH PRIVILEGES;
// 如果报错 ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
先把密码改成复杂的,再使用以下:
mysql> set global validate_password.policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password.length=8;
Query OK, 0 rows affected (0.00 sec)
3、InnoDB Cluster 配置及初始化
用MySQL Shell连接上n0
mysqlsh root@n0
执行每个节点的参数及权限配置预需求检测
dba.checkInstanceConfiguration('root@n0')
dba.checkInstanceConfiguration('root@n1')
dba.checkInstanceConfiguration('root@n2')
第一次都会检查失败,对每个节点,通过dba.configureInstance()进行集群相关信息配置,过程中所有提示都选 y。
dba.configureInstance('root@n0')
dba.configureInstance('root@n1')
dba.configureInstance('root@n2')
再次进行检查,确保每个节点都检查通过
创建集群
MySQL n0:3306 ssl JS > \c root@n0:3306
MySQL n0:3306 ssl JS > var cluster=dba.createCluster('myCluster')
添加节点
MySQL n0:3306 ssl JS > var cluster=dba.getCluster('myCluster')
MySQL n0:3306 ssl JS > cluster.addInstance('root@n1')
MySQL n0:3306 ssl JS > cluster.addInstance('root@n2')
3.3.3 查看集群
MySQL n0:3306 ssl JS > cluster.status()
至此,集群配置完成。查看集群状态,正常显示所有节点ONLINE。
4、Mysql router配置及初始化
在三台服务器上分别执行
n0:
mysqlrouter --bootstrap root@n0:3306 --user root
n1:
mysqlrouter --bootstrap root@n1:3306 --user root
n2:
mysqlrouter --bootstrap root@n2:3306 --user root
每台都启动Mysql router
mysqlrouter -c /etc/mysqlrouter/mysqlrouter.conf &
用客户端访问6446端口,连接成功
停止Mysql router命令
ps -ef|grep mysqlrouter #查出进程ID
kill -15 pid #关闭进程
5、MySQL Shell常用运维操作
1)MySQL Shell操作命令:
dba.checkInstanceConfiguration("root@hostname:3306") #检查节点配置实例,用于加入cluster之前
dba.rebootClusterFromCompleteOutage('myCluster'); #重启
dba.dropMetadataSchema(); #删除schema
var cluster = dba.getCluster('myCluster') #获取当前集群
cluster.status() #检查cluster里节点状态
cluster.checkInstanceState("root@hostname:3306") #检查cluster里节点状态
cluster.rejoinInstance("root@hostname:3306") #重新加入节点,我本地测试的时候发现rejoin一直无效,每次是delete后
addcluster.dissolve({force:true}) #删除集群
cluster.addInstance("root@hostname:3306") #增加节点
cluster.removeInstance("root@hostname:3306") #删除节点
cluster.removeInstance('root@host:3306',{force:true}) #强制删除节点
cluster.dissolve({force:true}) #解散集群
cluster.describe() #集群描述
cluster.setOption('n0:3306','memberWeight',100) #配置所有节点权重
cluster.setInstanceOption('n0:3306','memberWeight',100) #配置指定节点权重
2)removeInstance && addInstance
cluster.removeInstance("root@n1:3306",{force:true})
cluster.rescan()
cluster.addInstance("root@n1:3306")
3)集群多数节点异常恢复
当集群多个节点异常,则失去了仲裁机制,剩下的一个节点
// 将集群剥离为单节点运行
JS > cluster.forceQuorumUsingPartitionOf("root@node3:3306")
// 重新加另外2个节点加入
JS > cluster.rejoinInstance("root@n0:3306")
JS > cluster.rejoinInstance("root@n1:3306")
4)完整关闭的集群恢复
任意节点执行:
dba.rebootClusterFromCompleteOutage()
5)集群节点角色切换
dba.getCluster().setPrimaryInstance("IP:PORT") //切换主节点
6、Keepalived部署
1)分别在三台上安装Keepalived:
yum install keepalived -y
2)配置
将10.200.1.81定位虚拟IP
n0配置:
! Configuration File for keepalived
global_defs {
router_id Router1
vrrp_skip_check_adv_addr
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_mysqlrouter {
#判断目标进程状态,如果不存在则降低权重
script "killall -0 mysqlrouter"
interval 2 # every 2 seconds
weight -30 # add -30 points if OK
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 2
authentication {
auth_type PASS
auth_pass itdeve
}
virtual_ipaddress {
10.200.1.81
}
track_script {
chk_mysqlrouter
}
}
n1配置:
! Configuration File for keepalived
global_defs {
router_id Router2
vrrp_skip_check_adv_addr
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_mysqlrouter {
#判断目标进程状态,如果不存在则降低权重
script "killall -0 mysqlrouter"
interval 2 # every 2 seconds
weight -30 # add -30 points if OK
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 2
authentication {
auth_type PASS
auth_pass itdeve
}
virtual_ipaddress {
10.200.1.81
}
track_script {
chk_mysqlrouter
}
}
n2配置:
! Configuration File for keepalived
global_defs {
router_id Router3
vrrp_skip_check_adv_addr
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_mysqlrouter {
#判断目标进程状态,如果不存在则降低权重
script "killall -0 mysqlrouter"
interval 2 # every 2 seconds
weight -30 # add -30 points if OK
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 80
advert_int 2
authentication {
auth_type PASS
auth_pass itdeve
}
virtual_ipaddress {
10.200.1.81
}
track_script {
chk_mysqlrouter
}
}
3)启动服务
三台服务都启动keepalived
service keepalived start
服务启动成功
用Mysql客户端通过虚拟IP连接数据库,成功连接
注意: 每台服务器的防火墙必须相互放行,且都要对虚拟IP放行,否则启动后由于主机间无法通信,会变成了三个独立的keepalived节点
4)测试
①停止n0上的Mysql router
ps -ef|grep mysqlrouter #查出进程ID
kill -15 pid #关闭进程
虚拟IP成功漂移到其他节点,客户端连接正常
重启n0的Mysql router,VIP重新漂移回n0
②停止n0上的keepalived
service keepalived stop
虚拟IP成功漂移到其他节点,客户端连接正常
重启n0的keepalived,VIP重新漂移回n0
7、服务器重启服务启动顺序
1)先启动mysql
2)启动mysql router
3)启动keepalived