由日本DeNA公司开发的一套实现mysql高可用的解决方案,可以保障数据库自动故障切换操作在0~30s之内,可以确保切换过程中数据的一致,实现真正意义上的高可用。也就是在A主机上访问数据,中途服务器故障,客户可以在继续在B主机上访问,而且数据同步。
MHA Manager(管理节点) | MHA Node(数据节点) |
---|---|
单独部署在一台独立的机器,管理其他起点 | 运行在每台MySQL服务器 |
也可以部署在slave节点上 |
|
配置MHA
第一部分:环境准备
准备7台服务器:客户端client -192.168.4.50,MHA_manager 192.168.4.56 ,数据库服务器192.168.4.51-192.168.4.55,见下图
安装perl依赖包(在51-56机器上,仅以51为例)
[root@mysql51 ~]# cd mha-soft-student/
[root@mysql51 mha-soft-student]# ls
app1.cnf
master_ip_failover
mha4mysql-manager-0.56.tar.gz
mha4mysql-node-0.56-0.el6.noarch.rpm
perl-Config-Tiny-2.14-7.el7.noarch.rpm
perl-Email-Date-Format-1.002-15.el7.noarch.rpm
perl-Log-Dispatch-2.41-1.el7.1.noarch.rpm
perl-Mail-Sender-0.8.23-1.el7.noarch.rpm
perl-Mail-Sendmail-0.79-21.el7.art.noarch.rpm
perl-MIME-Lite-3.030-1.el7.noarch.rpm
perl-MIME-Types-1.38-2.el7.noarch.rpm
perl-Parallel-ForkManager-1.18-2.el7.noarch.rpm
[root@mysql51 mha-soft-student]# yum -y install perl-*.rpm
[root@mysql51 ~]#yum -y install perl-* //安装光盘上的perl包
所有数据库服务器之间实现ssh免密登录(51-55)
[root@mysql51 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): //回车就可以,默认存储路径
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): //回车就可以
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:rJOgGlx4twTN9ds3BmPdNe5gL455AyWVKe635rqN2DM root@mysql51
The key's randomart image is:
+---[RSA 2048]----+
| . .o..|
| o . . .ooo o|
| . o ..=.= o |
| . . . +.* + |
| . o.o S..o = o |
|. o.o..o .B.o |
|... .+ o.+. |
| o . oE+o. |
|. . =Bo |
+----[SHA256]-----+
[root@mysql51~]# for i in 192.168.4.{52,53,54,55}
> do
> ssh-copy-id $i
> done
配置MHA_Manager (192.168.4.56)免密登录所有数据库服务器(192.168.4.51-55)
[root@mysql56 ~]# ssh-keygen
[root@mysql56 ~]#for i in 192.168.4.{52,53,54,51,55}; do ssh-copy-id $i; done
在所有数据库服务器上安装mha-node包(51-55),安装mysql(为了实验简便,可以更改密码权限,这里略)
[root@mysql51 ~]# cd mha-soft-student/
[root@mysql51 mha-soft-student]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
准备中... ################################# [100%]
正在升级/安装...
1:mha4mysql-node-0.56-0.el6 ################################# [100%]
[root@mysql51 ~]#tar -xf mysql-5.7.17.tar
[root@mysql51 ~]rpm -Uvh mysql-community-*.rpm
[root@mysql51~]#systemctl start mysqld
[root@mysql51~]# vim /etc/my.cnf
[mysqld]
validate_password_policy=0
validate_password_length=6
[root@mysql51~]#systemctl restart mysqld
在管理主机(56)上先安装mha-node包,后安装mha4mysql-manager-0.56.tar.gz
[root@mysql56 ~]# cd mha-soft-student/
[root@mysql56 mha-soft-student]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
准备中... ################################# [100%]
正在升级/安装...
1:mha4mysql-node-0.56-0.el6 ################################# [100%]
[root@mysql56 mha-soft-student]# tar -zxf mha4mysql-manager-0.56.tar.gz
[root@mysql56 mha-soft-student]# cd mha4mysql-manager-0.56/
[root@mysql56 mha4mysql-manager-0.56]# ls
AUTHORS COPYING inc Makefile.PL META.yml rpm t
bin debian lib MANIFEST README samples tests
[root@mysql56 mha4mysql-manager-0.56]# perl Makefile.PL //类似于c编程里的./configure
*** Module::AutoInstall version 1.03
*** Checking for Perl dependencies...
[Core Features]
- DBI ...loaded. (1.627)
- DBD::mysql ...loaded. (4.023)
- Time::HiRes ...loaded. (1.9725)
- Config::Tiny ...loaded. (2.14)
- Log::Dispatch ...loaded. (2.41)
- Parallel::ForkManager ...loaded. (1.18)
- MHA::NodeConst ...loaded. (0.56)
*** Module::AutoInstall configuration finished. //配置完成
Checking if your kit is complete...
Looks good
Writing Makefile for mha4mysql::manager
Writing MYMETA.yml and MYMETA.json
[root@mysql56 mha4mysql-manager-0.56]# make
[root@mysql56 mha4mysql-manager-0.56]# make install
第二部分:配置MHA集群环境
配置数据库服务器192.168.4.51(master)
[root@mysql51~]# vim /etc/my.cnf
[mysqld]
validate_password_policy=0
validate_password_length=6
server_id=51
log_bin=master51
binlog-format="mixed"
plugin-load="rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl_semi_sync_master-enabled=1
rpl_semi_sync_slave_enabled=1
relay_log_purge=off #不自动删除日志
[root@mysql51 ~]# systemctl restart mysqld
[root@mysql51 ~]# mysql -uroot -p123456
mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| master51.000001 | 154 | | | |
+-----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> grant replication slave on *.* to repluser@"%" identified by "123456";
Query OK, 0 rows affected, 1 warning (10.03 sec)
mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| master51.000001 | 441 | | | |
+-----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
配置数据库服务器192.168.4.52(备用master1),192.168.4.53(备用master2)
[root@mysql52~]# vim /etc/my.cnf
[mysqld]
validate_password_policy=0
validate_password_length=6
server_id=52
log_bin=master52
binlog-format="mixed"
plugin-load="rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl_semi_sync_master-enabled=1
rpl_semi_sync_slave_enabled=1
relay_log_purge=off #不自动删除日志
[root@mysql51 ~]# systemctl restart mysqld
[root@mysql51 ~]# mysql -uroot -p123456
mysql> change master to
-> master_host="192.168.4.51",
-> master_user="repluser",
-> master_password="123456",
-> master_log_file="master51.000001",
-> master_log_pos=154; //设置偏移量为初始154,则可以同步用户
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G;
...
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
mysql> show grants for repluser@"%";
+--------------------------------------------------+
| Grants for repluser@% |
+--------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'%' |
+--------------------------------------------------+
1 row in set (0.00 sec)
[root@mysql53~]# vim /etc/my.cnf
[mysqld]
validate_password_policy=0
validate_password_length=6
server_id=53
log_bin=master53
... //以下所有操作同52
...
配置从数据库服务器192.168.4.54和192.168.4.55
[root@mysql54 ~]# vim /etc/my.cnf
[mysqld]
validate_password_policy=0
validate_password_length=6
server_id=54
plugin-load="rpl_semi_sync_slave=semisync_slave.so"
rpl_semi_sync_slave_enabled=1
relay_log_purge=off
[root@mysql54 ~]# systemctl restart mysqld
[root@mysql54 ~]# mysql -uroot -p123456
mysql> change master to master_host="192.168.4.51",master_user="repluser",master_password="123456",master_log_file="master51.000001",master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.51 sec)
mysql> start slave;
Query OK, 0 rows affected (0.10 sec)
mysql> show slave status\G;
...
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
mysql> show grants for repluser@"%";
+--------------------------------------------------+
| Grants for repluser@% |
+--------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'%' |
+--------------------------------------------------+
1 row in set (0.00 sec)
[root@mysql54 ~]# vim /etc/my.cnf
[mysqld]
validate_password_policy=0
validate_password_length=6
server_id=55 //其余同54
...
第三部分:配置MHA-Manager管理服务器(192.168.4.56)
修改配置文件
[root@mysql56 samples]# mkdir /etc/mha
[root@mysql56 samples]# pwd
/root/mha-soft-student/mha4mysql-manager-0.56/samples
[root@mysql56 samples]# cp conf/app1.cnf /etc/mha/ //拷贝模板
[root@mysql56 samples]# ls /etc/mha/app1.cnf
/etc/mha/app1.cnf
[root@mysql56 samples]# vim /etc/mha/app1.cnf
[server default]
manager_workdir=/etc/mha
manager_log=/etc/mha/manager.log
ssh_user=root
ssh_port=22
repl_user=repluser //主从同步用户
repl_password=123456
user=root //数据库用户名
password=123456
[server1]
hostname=192.168.4.51
candidate_master=1
[server2]
hostname=192.168.4.52
candidate_master=1
[server3]
hostname=192.168.4.53
candidate_master=1
[server4]
hostname=192.168.4.54
no_master=1
[server5]
hostname=192.168.4.55
no_master=1
根据配置文件的配置做对应的设置
1.在51上授权用户
[root@mysql51 ~]# mysql -uroot -p123456 //登录主库授权数据库用户
mysql> grant replication slave on *.* to root@"%" identified by "123456";
Query OK, 0 rows affected, 1 warning (10.03 sec)
2.在56上配置vip及自动更换master脚本
[root@mysql56 ~]# ls mha-soft-student/
master_ip_failover ...
[root@mysql56 ~]# cp /root/mha-soft-student/master_ip_failover /etc/mha/
[root@mysql56 ~]# vim /etc/mha/master_ip_failover
...
my $vip = '192.168.4.100/24'; # Virtual IP
...
[root@mysql56 ~]# chmod 755 /etc/mha/master_ip_failover
vim /etc/mha/app1.cnf
[server default]
...
master_ip_failover_script=/etc/mha/master_ip_failover #自动failover的脚本
[root@mysql51 ~]# ifconfig eth0:1 192.168.4.100/24 //临时配置vip在51上
[root@mysql51 ~]# ifconfig eth0:1
eth0:1: flags=4163 mtu 1500
inet 192.168.4.100 netmask 255.255.255.0 broadcast 192.168.4.255
ether 52:54:00:0d:20:84 txqueuelen 1000 (Ethernet)
检测配置
在56管理服务器上检测/etc/mha/app1.conf配置
1.检测ssh免密登录是否成功
[root@mysql56 ~]# masterha_
masterha_check_repl masterha_conf_host masterha_master_switch
masterha_check_ssh masterha_manager masterha_secondary_check
masterha_check_status masterha_master_monitor masterha_stop
[root@mysql56 ~]# masterha_check_ssh --conf=/etc/mha/app1.cnf
...
Mon Dec 17 17:36:15 2018 - [info] All SSH connection tests passed successfully
//证明ssh免密成功
2.检测主从同步配置
[root@mysql56 ~]# masterha_check_repl --conf=/etc/mha/app1.cnf
...
MySQL Replication Health is OK. //配置成功
启动管理服务
[root@mysql56 ~]# masterha_check_status --conf=/etc/mha/app1.cnf
app1 is stopped(2:NOT_RUNNING).
[root@mysql56 ~]# masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover
//启动服务,删除宕机主库配置,忽略xxx.health文件
Mon Dec 17 17:54:32 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Dec 17 17:54:32 2018 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Mon Dec 17 17:54:32 2018 - [info] Reading server configuration from /etc/mha/app1.cnf..
//会占用一个终端,需另开一个终端查看状态
[root@mysql56 ~]# masterha_check_status --conf=/etc/mha/app1.cnf
app1 (pid:8451) is running(0:PING_OK), master:192.168.4.51
第四部分:测试集群配置
1.在主库51上添加测试数据库的链接用户
mysql> create database db8;
Query OK, 1 row affected (0.04 sec)
mysql> create table db8.a(id int);
Query OK, 0 rows affected (0.27 sec)
mysql> grant select,insert on db8.a to admin@"%" identified by "123456";
Query OK, 0 rows affected, 1 warning (0.02 sec)
2.在客户端50上链接vip地址,访问集群
[root@mysql50 ~]# mysql -uadmin -h192.168.4.100 -p123456
mysql> insert into db8.a values(999);
Query OK, 1 row affected (0.07 sec)
mysql> select * from db8.a; //在其余数据库服务器上同样验证
+------+
| id |
+------+
| 999 |
+------+
1 row in set (0.00 sec)
3.停止51主机的数据库服务
[root@mysql51 ~]# systemctl stop mysqld.service
4.在两个备用的主库上查看现在的vip地址在52还是53上
[root@mysql52 ~]# ifconfig eth0:1
eth0:1: flags=4163 mtu 1500
inet 192.168.4.100 netmask 255.255.255.0 broadcast 192.168.4.255
ether 52:54:00:ce:74:9a txqueuelen 1000 (Ethernet)
[root@mysql53 ~]# ifconfig eth0:1
eth0:1: flags=4163 mtu 1500
ether 52:54:00:99:fd:2f txqueuelen 1000 (Ethernet)
5.在50上开终端链接数据库服务vip地址,并且可以正常查看和存储数据
mysql> insert into db8.a values(111);
Query OK, 1 row affected (0.05 sec)
mysql> select * from db8.a; //在53,54,55上继续查看是否同步
+------+
| id |
+------+
| 999 |
| 111 |
+------+
2 rows in set (0.00 sec)
6.查看管理节点的app1.cnf文件,主库51的配置自动删除
[root@mysql56 ~]# vim /etc/mha/app1.cnf
[server default]
...
ssh_user=root
user=root
[server2]
candidate_master=1
hostname=192.168.4.52
...
7.54和55会自动把主库的ip地址指向新的主库地址
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.4.52
...
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
...
第五部分:维护集群
将故障的数据库服务器再添加到集群里
1.启动51数据库服务器,配置为当前主库的从库
mysql> show master status; //在新的主库上查看主库信息
+-----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| master52.000001 | 422 | | | |
+-----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec
[root@mysql51 ~]# systemctl restart mysqld.service
mysql> change master to master_host="192.168.4.52",master_user="repluser",master_password="123456",master_log_file="master52.000001",master_log_pos=422;
mysql> start slave;
Query OK, 0 rows affected (0.03 sec)
mysql> show slave status\G;
...
Master_Host: 192.168.4.52
...
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
2.修改56的app1.cnf文件,添加51的配置信息
[root@mysql56 ~]# vim /etc/mha/app1.cnf
...
[server1]
candidate_master=1
hostname=192.168.4.51
...
3.启动管理主机56上的管理服务,并查看状态信息
[root@mysql56 ~]# masterha_check_status --conf=/etc/mha/app1.cnf
app1 is stopped(2:NOT_RUNNING).
[root@mysql56 ~]# masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover
Mon Dec 17 20:35:49 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Dec 17 20:35:49 2018 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Mon Dec 17 20:35:49 2018 - [info] Reading server configuration from /etc/mha/app1.cnf..
[root@mysql56 ~]# masterha_check_status --conf=/etc/mha/app1.cnf
app1 (pid:12778) is running(0:PING_OK), master:192.168.4.52