二、基于MHA实现MySQL的高可用
准备环境:CentOS 7
| MHA Manager节点 | IP:172.18.42.200 |
| MHA Node Master节点 | IP:172.18.42.201 |
| MHA Node Slave1节点 | IP:172.18.42.202 |
| MHA Node Slave2节点 | IP:172.18.42.203 |
前提:基于MHA实现MySQL高可用时,各节点之间需要基于ssh秘钥进行通信
[root@node0 ~]# ssh-keygen -t rsa -P '' ##在Manager节点上基于rsa算法生成秘钥,密码为空
[root@node0 ~]# cat .shh/id_rsa.pub &>> .shh/authorized_keys ##先要确认能与本地进行通信
[root@node0 ~]# chmod 600 .ssh/id_rsa .ssh/authorized_keys ##更改秘钥权限
[root@node0 ~]# scp -p .ssh/id_rsa .ssh/authorized_keys root@172.18.42.201:/root/.ssh/ ##Manager节点把秘钥、公钥发送给每个节点,使各个节点可以进行秘钥通信(第一此通信需要秘钥,后续不需要)
[root@node0 ~]# scp -p .ssh/id_rsa .ssh/authorized_keys root@172.18.42.202:/root/.ssh/
[root@node0 ~]# scp -p .ssh/id_rsa .ssh/authorized_keys root@172.18.42.203:/root/.ssh/
[root@node0 ~]# ssh 172.18.42.201 'ifconfig' ##可基于此命令让各个节点之间进行测试是否建立连接
Are you sure you want to continue connecting (yes/no)? yes ##第一次建立连接需要认证
root@node1's password:
1、部署MHA Manager节点
(1)安装mha4mysql-manager、mha4mysql-node
[root@node0 ~]# yum install mha4mysql-node-0.56-0.el6.noarch.rpm mha4mysql-manager-0.56-0.el6.noarch.rpm -y
(2)创建配置文件,并编辑
[root@node0 ~]# vim /etc/masterha/app1.cnf
[server default]
user=wtc ##能远程连接至各mysql节点的用户管理账号,需要mysql节点创建此用户
password=wtc
manager_workdir=/data/masterha/app1 ##管理节点的工作目录
manager_log=/data/masterha/app1/manager.log ##管理节点的日志文件路径
remote_workdir=/data/masterha/app1 ##远程的每个mysql节点为Manager节点提供工作目录,会自动创建
ssh_user=root ##以root用户远程管理,,基于秘钥认证无需密码;如果不基于密码,需要指明密码
repl_user=repluser ##复制权限用户账号
repl_password=replpass
ping_interval=1 ##每隔多长时间探测一次Master节点是否在线
[server1] ##定义mysql节点
hostname=172.18.42.201
candidate_master=1 ##当主节点宕机以后,这个节点是否可以成为新的主节点
##no_master=1 ##当主节点宕机后,这个节点不会成为新的主节点
[server2]
hostname=172.18.42.202
candidate_master=1
[server3]
hostname=172.18.42.203
candidate_master=1
2、部署Master节点
(1)安装mariadb-server、mha4mysql-node服务
[root@node1 ~] yum install mariadb-server mha4mysql-node-0.56-0.el6.noarch.rpm -y
(2)编辑其配置文件
[root@node1 ~]# vim /etc/my.cnf
[mysqld]
innodb_file_per_table = ON
skip_name_resolve = ON
log_bin=mysql-bin
relay-log=relay-log
server-id=1
[root@node1 ~]# systemctl start mariadb.service
MariaDB [(none)]> show master status;
mysql-bin.000003 245
MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'172.18.%.%' identified by 'replpass'; ##创建具有复制权限的用户
3、部署Slave1
(1)安装mariadb-server、mha4mysql-node服务
[root@node2 ~] yum install mariadb-server mha4mysql-node-0.56-0.el6.noarch.rpm -y
(2)编辑其配置文件
[root@node2 ~]# vim /etc/my.cnf
[mysqld]
innodb_file_per_table = ON
skip_name_resolve = ON
log_bin=mysql-bin
relay-log=relay-log
server-id=2
read_only=1 ##MHA通过这个来判断那个是mysql主服务器
relay_log_purge=0
[root@node2 ~]# systemctl start mariadb.service
MariaDB [(none)]> change master to master_host='172.18.42.201',master_user='repluser',master_password='replpass',master_log_file='mysql-bin.000003',master_log_pos=245; ##连接至Master节点
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
4、部署Slave2
(1)安装mariadb-server、mha4mysql-node服务
[root@wxpp ~] yum install mariadb-server mha4mysql-node-0.56-0.el6.noarch.rpm -y
(2)编辑其配置文件
[root@wxpp ~]# vim /etc/my.cnf
innodb_file_per_table = ON
skip_name_resolve = ON
log_bin=mysql-bin
relay-log=relay-log
server-id=3
read_only=1
relay_log_purge=0
[root@wxpp ~]# systemctl start mariadb.service
MariaDB [(none)]> change master to master_host='172.18.42.201',master_user='repluser',master_password='replpass',master_log_file='mysql-bin.000003',master_log_pos=245; ##连接至Master节点
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
5、在Master节点创建管理用户账号,在Manager节点检测各检点之间的连接
MariaDB [(none)]> grant all on *.* to 'wtc'@'172.18.%.%' identified by 'wtc'; ##实现了主从复制,在Msater节点创建的同时,slave节点也会创建
[root@node0 ~]# masterha_check_ssh --conf=/etc/masterha/app1.cnf ##测试各节点之间ssh互相通信是否ok
Wed Apr 20 10:17:40 2016 - [info] All SSH connection tests passed successfully.
[root@node0 ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf ##检查各个节点之间主从复制是否ok
172.18.42.201(172.18.42.201:3306) (current master) ##201是主节点,202、203是从节点
+--172.18.42.202(172.18.42.202:3306)
+--172.18.42.203(172.18.42.203:3306)
MySQL Replication Health is OK.
6、启动Manager节点服务,把Master节点宕机,查看是否转换
[root@node0 ~]# masterha_manager --conf=/etc/masterha/app1.cnf ##启动Manager服务
Wed Apr 20 10:27:35 2016 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Apr 20 10:27:35 2016 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Wed Apr 20 10:27:35 2016 - [info] Reading server configuration from /etc/masterha/app1.cnf..
[root@node1 ~]# systemctl stop mariadb.service ##关掉节点2的mariadb服务
MariaDB [(none)]> show slave status\G; ##查看Slave2节点的复制线程状态
Master_Host: 172.18.42.202 ##Master节点转为Slave1节点了
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
7、现在我们开启Master节点,看其是否成为从节点
[root@node1 ~]# systemctl start mariadb.service ##开启Master节点的mariadb服务
MariaDB [(none)]> change master to master_host='172.18.42.202',master_user='repluser',master_password='replpass',master_log_file='mysql-bin.000003',master_log_pos=245;
##此时“master_host”应该指向Slave1的IP
##使用的二进制日志文件、事件是事先备份好的(在实现主从复制之前,备份一份)
MariaDB [(none)]> start slave; ##启动复制线程
MariaDB [(none)]> show slave status\G;
Master_Host: 172.18.42.202 ##此时IP为Slave1的IP
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[root@node0 ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf ##在Manager节点上查看一次,主从复制是否ok
172.18.42.202(172.18.42.202:3306) (current master) ##此时,主节点成为了Slave1
+--172.18.42.201(172.18.42.201:3306)
+--172.18.42.203(172.18.42.203:3306)
MySQL Replication Health is OK.
问题小结:
1、masterha_manager运行时是工作于前台的,而且不能断开,一旦进行主从切换后,就停止工作,我们需要手动把它开启,这时需要脚本来实现自行监控
2、在使用MHA方法时,启动Manager节点的服务有可能会失败,可以尝试一下先把Manager节点的日志文件路径创建好"/data/masterha/app1"
3、在配置mysql各节点的配置文件时,主从都需要开启中继日志、二进制日志
主节点开启中继日志和二进制日志,是因为主节点宕机以后启用时会变为从节点
从节点需要开启中继日志和二进制日志,是因为主节点宕机以后,任何一个从节点都有可能转移称为主节点
本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-07/132923.htm
