搭建MHA
架构图: MHA至少需要三台主机
1、环境要求:
一、配置所有数据节点主机之间可以互相以ssh密钥对方式认证登陆
二、配置manager56主机 无密码ssh登录 所有数据节点主机
配置管理主机56
(主)
master51
|
|
______________________________________________________
| | |
slave52 slave53
(备) (从)
mgm56
Manager(为了节省资源,管理主机搭在从节点上)
三、配置主从同步,要求如下:
51 主库 开半同步复制
52 从库(备用主库) 开半同步复制
2、公共配置:
在所有主机上安装软件包(网上搜索一下安装包)
perl-Config-Tiny-2.14-7.el7.noarch.rpm
perl-Mail-Sender-0.8.23-1.el7.noarch.rpm
perl-MIME-Types-1.38-2.el7.noarch.rpm
perl-Email-Date-Format-1.002-15.el7.noarch.rpm
perl-Mail-Sendmail-0.79-21.el7.art.noarch.rpm
perl-Parallel-ForkManager-1.18-2.el7.noarch.rpm
perl-Log-Dispatch-2.41-1.el7.1.noarch.rpm
perl-MIME-Lite-3.030-1.el7.noarch.rpm
yum -y install perl-*.rpm
安装软件mha-node
yum -y install perl-DBD-mysql
rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
授权监控用户
grant all on *.* to 'root'@'%' identified by "123456";
3、配置一主一从一备:
master配置(主库):
vim /etc/my.cnf
[mysqld]
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 #开启从半同步复制
server_id=51
log-bin=master51
binlog-format="mixed"
:wq
systemctl restart mysqld
[root@db107 ~]# ls /var/lib/mysql/master51.*
/var/lib/mysql/master51.000001 /var/lib/mysql/master51.index
[root@db107 ~]# mysql -uroot -p123456
mysql> grant replication slave on *.* to repluser@"%" identified by "123456";
Query OK, 0 rows affected, 1 warning (10.04 sec)
mysql> set global relay_log_purge=off; #不自动删除本机的中继日志文件
Query OK, 0 rows affected (0.15 sec)
mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| master51.000001 | 441 | | | |
+-----------------+----------+--------------+------------------+-------------------+
mysql> quit;
slave52(备库)的配置
vim /etc/my.cnf
[mysqld]
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
server_id=52
log-bin=master52
binlog-format="mixed"
]# systemctl restart mysqld
[root@db108 ~]# ls /var/lib/mysql/master52.*
/var/lib/mysql/master52.000001 /var/lib/mysql/master52.index
[root@db108 ~]# mysql -uroot -p123456
mysql> grant replication slave on *.* to repluser@"%" identified by "123456";
Query OK, 0 rows affected, 1 warning (10.04 sec)
mysql> set global relay_log_purge=off; //不自动删除本机的中继日志文件
Query OK, 0 rows affected (0.13 sec)
mysql> change master to
-> master_host="192.168.4.51", #主库ip
-> master_user="repluser", #同步用户
-> master_password="123456", #同步用户密码
-> master_log_file="master51.000001", #主库的file
-> master_log_pos=441; #主库的Position 在主库执行show master status可以看到
Query OK, 0 rows affected, 2 warnings (0.04 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
[root@db108 ~]# mysql -uroot -p123456 -e "show slave status\G" | grep -i YES
mysql: [Warning] Using a password on the command line interface can be insecure.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
slave54(从库)的配置
vim /etc/my.cnf
[mysqld]
server_id=54
:wq
[root@db111 ~]# systemctl restart mysqld
[root@db111 ~]# 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=441;
Query OK, 0 rows affected, 2 warnings (0.09 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
注:如果有多台从库,不需要参与主从切换的则不需要开启binlog与半同步复制
4、在客户端测试主从同步配置
3.6.1 在主库51上添加访问数据的授权用户
[root@db107 ~]# mysql -uroot -p123456
mysql> grant all on gamedb.* to admin@"%" identified by "123456";
3.6.2 在客户端主机连接主库51 建库表记录
mysql> create database gamedb;
Query OK, 1 row affected (0.01 sec)
mysql> create table gamedb.t1 (id int);
Query OK, 0 rows affected (0.04 sec)
mysql> insert into gamedb.t1 values(999);
Query OK, 1 row affected (0.15 sec)
mysql> insert into gamedb.t1 values(999);
Query OK, 1 row affected (0.05 sec)
mysql> select * from gamedb.t1;
+------+
| id |
+------+
| 999 |
| 999 |
+------+
2 rows in set (0.00 sec)
mysql>
在客户端使用授权用户连接从库也能看到同样的库表及记录
[root@host114 ~]# mysql -h从库IP地址 -uadmin -p123456
mysql> select * from gamedb.t1;
+------+
| id |
+------+
| 999 |
| 999 |
+------+
5、 配置管理主机
2.2.1 安装依赖的perl软件包
2.2.2装包
]# yum -y install perl-DBD-mysql
]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
]# yum -y install perl-ExtUtils-* perl-CPAN-*
下载解压
]#tar -zxf mha4mysql-manager-0.56.tar.gz
]#cd mha4mysql-manager-0.56
]# perl Makefile.PL
]# make
]# make install
2.2.3 指定命令所在的路径
]# cd mha4mysql-manager-0.56
]# mkdir /root/bin
]# cp bin/* /root/bin/
]# ls /root/bin
2.2.4修改配置文件
]# mkdir /etc/mha_manager/
]# cd mha4mysql-manager-0.56/samples/conf/
]# cp app1.cnf /etc/mha_manager/
]# vim /etc/mha_manager/app1.cnf
[server default]
manager_workdir=/etc/mha_manager //修改
manager_log=/etc/mha_manager/manager.log //修改
master_ip_failover_script=/etc/mha_manager/master_ip_failover
//MHA脚本迁移vip地址
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 // 设置为候选 master
(port=3306,如果mysql端口是3306,就不用写)
[server2]
hostname=192.168.4.52
candidate_master=1
[server5]
hostname=192.168.4.53
no_master=1
]# cd mha-soft-student
]# cp master_ip_failover /etc/mha_manager/
]# vim /etc/mha_manager/master_ip_failover
35 my $vip = '192.168.4.100/24'; # Virtual IP
36 my $key = "1";
37 my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
38 my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
测试配置文件
]# vim /etc/mha_manager/app1.cnf
#master_ip_failover_script=/etc/mha_manager/master_ip_failover
:wq
]# masterha_check_ssh --conf=/etc/mha_manager/app1.cnf
.... All SSH connection tests passed successfully.
]# masterha_check_repl --conf=/etc/mha_manager/app1.cnf
.....
.....
MySQL Replication Health is OK.
17:15
3 启动服务
3.1 把vip 地址手动绑定在当前的主库51上
]# ifconfig eth0:1 192.168.4.100/24
]# ifconfig eth0:1
3.2 启动服务host56
]# vim /etc/mha_manager/app1.cnf
master_ip_failover_script=/etc/mha_manager/master_ip_failover
:wq
]#nohup masterha_manager --conf=/etc/mha_manager/app1.cnf --remove_dead_master_conf --ignore_last_failover 2>&1 &
]#masterha_check_status --conf=/etc/mha_manager/app1.cnf
参数解读:
参数信息 | 参数含义 | 备注 |
manager_workdir | 设置manager的工作目录 | $1 |
manager_log | 设置manager的日志 | $1 |
master_binlog_dir | 设置master 保存binlog的位置,以便MHA可以找到master的日志 | $1 |
master_ip_failover_script | 设置自动failover时候的切换脚本 | $1 |
master_ip_online_change_script | 设置手动切换时候的切换脚本 | $1 |
password | 设置mysql中root用户的密码 | $1 |
user | 设置监控用户root | $1 |
ping_interval | 设置监控主库,发送ping包的时间间隔,默认是3秒,尝试三次没有回应的时候自动进行railover | $1 |
remote_workdir | 设置远端mysql在发生切换时binlog的保存位置 | $1 |
repl_password | 设置复制用户的密码 | $1 |
repl_user | 设置复制环境中的复制用户名 | $1 |
report_script | 设置发生切换后发送的报警的脚本 | $1 |
shutdown_script | 设置故障发生后关闭故障主机脚本(该脚本的主要作用是关闭主机防止发生脑裂,这里没有使用) | $1 |
ssh_user | 设置ssh的登录用户名 | $1 |
hostname | 主机名/IP | $1 |
port | 端口号 | $1 |
candidate_master | 设置为候选master,如果设置该参数以后,发生主从切换以后将会将此从库提升为主库,即使这个主库不是集群中事件最新的slave | $1 |
check_repl_delay | 默认情况下如果一个slave落后master 100M的relay logs的话,MHA将不会选择该slave作为一个新的master,因为对于这个slave的恢复需要花费很长时间,通过设置check_repl_delay=0,MHA触发切换在选择一个新的master的时候将会忽略复制延时,这个参数对于设置了candidate_master=1的主机非常有用,因为这个候选主在切换的过程中一定是新的master |
4测试高可用集群配置
在数据库服务器上添加访问数据时连接用户 webuser 123qqq...A
mysql> create database db13;
Query OK, 1 row affected (0.04 sec)
mysql> grant all on db13.* to webuser@"%"
-> identified by "123qqq...A";
4.1 客户端连接VIP地址访问数据库
]# mysql -h192.168.4.100 -uwebuser -p123qqq...A
4.2 测试高用集群
把主机51上的数据库服务停止
把宕机的数据库服务器 在添加到当前集群里
mysql> change master to master_host="192.168.4.52",
-> master_user="repluser",
-> master_password="123qqq...A",
-> master_log_file="master52.000001",
-> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.30 sec)
mysql> start slave;
56:
]# vim /etc/mha_manager/app1.cnf
[server1]
candidate_master=1
hostname=192.168.4.51
:wq
]# masterha_check_repl --conf=/etc/mha_manager/app1.cnf
MySQL Replication Health is OK.
]#nohup masterha_manager --conf=/etc/mha_manager/app1.cnf --remove_dead_master_conf --ignore_last_failover 2>&1 &
[root@mgm56 ~]# masterha_check_status --conf=/etc/mha_manager/app1.cnf
app1 (pid:8710) is running(0:PING_OK), master:192.168.4.52
[root@mgm56 ~]#
删除账户及权限:>drop user 用户名@'%';
>drop user 用户名@ localhost;
6、配置读写分离调度器
rpm -ivh maxscale-2.1.2-1.rhel.7.x86_64.rpm
[root@host56 ~]# rpm -qa | grep maxscale
maxscale-2.1.2-1.x86_64
修改配置文件
[root@host56 ~]# cp /etc/maxscale.cnf /etc/maxscale.cnf.bak
[root@host56 ~]# vim /etc/maxscale.cnf
[maxscale]
threads=1
[server1]
type=server
address=192.168.4.51
port=3306
protocol=MySQLBackend
[server2]
type=server
address=192.168.4.52
port=3306
protocol=MySQLBackend
[MySQL Monitor]
type=monitor
module=mysqlmon
servers=server1, server2
user=scalemon
passwd=123456
monitor_interval=10000
[Read-Write Service]
type=service
router=readwritesplit
servers=server1, server2
user=maxscale
passwd=123456
max_slave_connections=100%
[MaxAdmin Service]
type=service
router=cli
[Read-Write Listener]
type=listener
service=Read-Write Service
protocol=MySQLClient
port=4006
[MaxAdmin Listener]
type=listener
service=MaxAdmin Service
protocol=maxscaled
socket=default
port=4018
[root@host56 ~]#
根据配置文件做相应的设置(在2台数据库服务器上添加用户)
监控数据库服务器时,连接数据库服务器的用户
mysql> grant replication slave, replication client on *.* to scalemon@'%' identified by "123456";
验证 访问数据时,连接数据库服务器使用的用户,是否在数据库服务器上存在的,连接用户
mysql> grant select on mysql.* to maxscale@'%' identified by "123456";
查看授权用户
mysql> select user,host from mysql.user where user in ("scalemon","maxscale");
+----------+------+
| user | host |
+----------+------+
| maxscale | % |
| scalemon | % |
+----------+------+
启动服务
[root@host56 ~]# maxscale -f /etc/maxscale.cnf
查看服务进程和端口
[root@host56 ~]# netstat -utnlp | grep :4006
tcp6 0 0 :::4006 :::* LISTEN 29688/maxscale
[root@host56 ~]# netstat -utnlp | grep :4018
tcp6 0 0 :::4018 :::* LISTEN 29688/maxscale
[root@host56 ~]#
查看进程
[root@host56 ~]# ps -C maxscale
PID TTY TIME CMD
29688 ? 00:00:00 maxscale
停止服务
[root@host56 ~]# ps -C maxscale
PID TTY TIME CMD
29688 ? 00:00:00 maxscale
[root@host56 ~]# kill -9 29688
2.2.3 测试配置
A 在本机访问管理管端口查看监控状态
[root@host56 ~]# maxadmin -P4018 -uadmin -pmariadb
MaxScale> list servers
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server | Address | Port | Connections | Status
-------------------+-----------------+-------+-------------+--------------------
server1 | 192.168.4.51 | 3306 | 0 | Master, Running
server2 | 192.168.4.52 | 3306 | 0 | Slave, Running
-------------------+-----------------+-------+-------------+--------------------
MaxScale> exit
[root@host56 ~]#
b 客户端访问数据读写分离服务器
]# mysql -h192.168.4.56 -P4006 -uwebuser -p123456
mysql> select @@hostname;
mysql> 执行插入或查询 ( 在51 和 52 本机查看记录)