MHA初探(主从复制,GID,MHA高可用)

材料:一台mysql母鸡,glibc安装方式。yum源配置epel。

安装mysql请参考

https://blog.csdn.net/VegetandBird_s/article/details/102555932

1.利用母机克隆4台机器

利用母机克隆4台机器,一主二从一MHA服务器。

以本文环境为例 IP地址分别为:

MHA(10.1.1.10)

主(10.1.1.13)

从1(10.1.1.14)

从2(10.1.1.15)

先修改hostname,分别叫MHA,master,slave1,slave2

# hostnamectl set-hostname MHA
# su
# 关闭NetworkManager
# systemctl stop NetworkManager
# systemctl disable NetworkManager

另外3台操作略。

2.先搭建mysql主从复制(2种方法)

模型是一主二从,二从同主

a.方法1

先区分三台mysql机器

# vi /etc/sysconfig/network-scripts/ifcfg-ens33
# 修改uuid值,让三台克隆机uuid不同即可
# 保存退出

# 修改mysql配置文件,my.cnf,本文路径与安装mysql文章路径一样。
# vi /usr/local/mysql/my.cnf

# 主机内容如下
# [mysqld]
# basedir = /usr/local/mysql
# datadir = /usr/local/mysql/data
# port = 3306
# server_id = 1			#此处为区分点 三台机器要不同#	
# socket = /tmp/mysql.sock
# character_set_server=utf8mb4
# log-bin = /usr/local/mysql/data/binlog		
# log-error = /usr/local/mysql/data/mysql.err

# 从机
# [mysqld]
# basedir = /usr/local/mysql
# datadir = /usr/local/mysql/data
# port = 3306
# server_id = 2        #另一台写3#
# socket = /tmp/mysql.sock
# character_set_server=utf8mb4
# relay-log=/usr/local/mysql/data/relaylog
# log-error = /usr/local/mysql/data/mysql.err
# 保存退出

# 三台配置可根据需求填写更多参数
# 查看是否有mysqld进程残留
# ss -nltp
# 若有
# pkill mysqld


两台从机删掉自己数据
# rm -rf /usr/local/mysql/data/*
# rsync -av [email protected]:/usr/local/mysql/data/* /usr/local/mysql/data/
# rm -rf /usr/local/mysql/data/auto.cnf  #此处为区分点,需要删掉重新生成#

# 重启服务
# service network restart
# service mysql restart

三台mysql机都需要创建一个用于从机访问的账号slave,密码123,给予他复制权限,并且刷新

mysql> grant replication slave on *.* to 'slave'@'10.1.1.%' identified by '123';flush privileges;

添加完切换至主机侧

查看二进制文件写入位置
mysql > show master status;
本机案例

+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000001 |      405 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)

重点是binlog.000001文件名,405 位置。

注:若有可能在部署时有数据修改,则可以考虑先进行主机锁表
mysql> flush tables with read lock;
解锁
mysql> unlock tables;
本文暂不需要

两台从机分别需要执行从机命令

mysql> CHANGE MASTER TO
 MASTER_HOST='10.1.1.13',
 MASTER_USER='slave',
 MASTER_PASSWORD='123',
 MASTER_PORT=3306,
 MASTER_LOG_FILE='binlog.000001',
 MASTER_LOG_POS=405,
 MASTER_CONNECT_RETRY=10;

开启从机
mysql> start slave;
mysql> show slave status\G

若显示  
    Slave_IO_Running: Yes
    Slave_SQL_Running: Yes
则成功。

b.方法2

GTIDs主从复制,是mysql 5.6新加入的一项技术。

GTIDs的优势

① 当使用GTIDs时,每一个事务都可以被==识别并且跟踪== 

② 在配置主从复制时,无需在指定二进制文件名称与位置。大大简化了主从配置的复杂度。

非常简单,只需要在AB复制的基础上,添加(MASTER=>3行/SLAVE=>4)行配置即可实现。

主机配置

[mysqld]
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
server_id = 1
socket = /tmp/mysql.sock
character_set_server=utf8mb4
log-bin = /usr/local/mysql/data/binlog
log-error = /usr/local/mysql/data/mysql.err
# 添加3行配置
gtid-mode=on
log-slave-updates=1
enforce-gtid-consistency

从机配置

[mysqld]
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
server_id = 2    #所有mysql机id要相互不同#
socket = /tmp/mysql.sock
character_set_server=utf8mb4
# 从服务器也需要开启二进制日志
log-bin = /usr/local/mysql/data/binlog
relay-log=/usr/local/mysql/data/relaylog
log-error = /usr/local/mysql/data/mysql.err
# 添加4行配置
gtid-mode=on
log-slave-updates=1
enforce-gtid-consistency=1
skip-slave-start


设置完成后,重启mysql
# service mysql restart

创建同步账户slave密码123
mysql> grant replication slave on *.* to 'slave'@'10.1.1.%' identified by '123';
mysql> flush privileges;

mysql> change master to master_host='10.1.1.13',master_user='slave',master_password='123',master_port=3306,master_auto_position=1;
mysql> start slave;
mysql> show slave status\G

AB复制与GTIDs复制最主要区别:
master_auto_position=1

主从配置到此结束。可自行在主机中添加数据进行同步测试

3.MHA服务搭建 

先下载组件

作者git:

https://github.com/yoshinorim/mha4mysql-manager/releases

https://github.com/yoshinorim/mha4mysql-node/releases

或者博主实验版本

https://codeload.github.com/13331033844/MHA/zip/master

这次我选择先下载,再用ftp传上服务器。

# 以本次实验为例,软件目录为 /software
# ll /software
-rw-r--r--  1 root root      1744 Oct 23 23:46 app1.conf
-rw-r--r--  1 root root      4016 Oct 23 23:46 master_ip_failover.sh
-rw-r--r--  1 root root     11458 Oct 23 23:46 master_ip_online_change.sh
-rw-r--r--  1 root root     81080 Oct 23 23:46 mha4mysql-manager-0.57-0.el7.noarch.rpm
-rw-r--r--  1 root root     35360 Oct 23 23:46 mha4mysql-node-0.57-0.el7.noarch.rpm
-rw-r--r--  1 root root     10146 Oct 23 23:46 power_manager
# 这些文件为实验需要

# 将mha4mysql-node-0.57-0.el7.noarch.rpm 分发给各mysql服务器,路径自行定义
# rsync -av /software/mha4mysql-node-0.57-0.el7.noarch.rpm [email protected]:/software/
# rsync -av /software/mha4mysql-node-0.57-0.el7.noarch.rpm [email protected]:/software/
# rsync -av /software/mha4mysql-node-0.57-0.el7.noarch.rpm [email protected]:/software/

# 所有机器都需要安装mha4mysql-node
# yum -y install /software/mha4mysql-node-0.57-0.el7.noarch.rpm
# rm -rf /etc/my.cnf
# 同时删除这个文件

# 所有机器同时创建用户admin(自定义),让mysql服务器对mha可以免密登录
# useradd admin;echo 123|passwd --stdin admin

MHA服务器单独安装 mha4mysql-manager

# yum -y install /software/mha4mysql-manager-0.57-0.el7.noarch.rpm

# MHA服务器分发密钥
# su - admin
# ssh-keygen -P "" -f ~/.ssh/id_rsa
# cd .ssh/
# mv id_rsa.pub authorized_keys
# 密钥发送分发循环
# for i in 13 14 15;do scp -r ../.ssh/ 10.1.1.$i:~/;done

注:此处本人尝试过使用ssh-copy-id,发现有可能有异常,如想尝试验证,请注意此处。


# 创建MHA配置文件目录
# mkdir /etc/mha/
# mkdir -p /data/mha/masterha/app1
# chown -R admin. /data/
# 把software另外4个文件传到配置文件夹
# cp /software/app1.conf /etc/mha
# cp /software/master_ip_failover.sh /etc/mha
# cp /software/master_ip_online_change.sh /etc/mha
# cp /software/power_manager /etc/mha
# chmod +x /etc/mha/master_ip_*
# vim /etc/mha/app1.conf

# 需要修改内容
# master_pid_file=/usr/local/mysql/data/master.pid
# secondary_check_script=/usr/bin/masterha_secondary_check -s 10.1.1.14 -s 10.1.1.15 --user=admin --port=22 --master_host=10.1.1.11 --master_port=3306
# [server1]
# hostname=10.1.1.13
# port= 3306
# candidate_master=1
# [server2]
# hostname=10.1.1.14
# port= 3306
# candidate_master=1
# [server3]
# hostname=10.1.1.15
# port= 3306
# candidate_master=1
# 保存退出

如果改了网卡名(默认ens33),自定义了用户名(本文admin)则需要修改
# vim master_ip_failover.sh
38行
110行
# vim master_ip_online_change.sh
43行
47行
否则不动

给三台mysql机的admin用户赋予root权限

# vim /etc/sudoers.d/admin
# 内容
# User_Alias MYSQL_USERS = admin
# Runas_Alias MYSQL_RUNAS = root
# Cmnd_Alias MYSQL_CMNDS = /sbin/ifconfig,/sbin/arping
# MYSQL_USERS ALL = (MYSQL_RUNAS) NOPASSWD: MYSQL_CMNDS
# 保存退出


# 把上面的admin文件推送到SLAVE1与SLAVE2
# for i in 14 15;do scp /etc/sudoers.d/admin 10.1.1.$i:/etc/sudoers.d/; done

在MASTER主机上挂载虚拟的IP(VIP),以10.1.1.100为例

# su - admin
# sudo /sbin/ifconfig ens33:1 10.1.1.100 broadcast 10.1.1.255 netmask 255.255.255.0
# sudo /sbin/arping -f -q -c 5 -w 5 -I ens33 -s 10.1.1.100 -U 10.1.1.13


# 配置了app1.conf,要在MASTER数据库主机中,创建一个mha账号,用于检测主从状态。
mysql> grant all privileges on *.* to mha@'10.1.1.%' identified by '123';
mysql> flush privileges;

4.环境测试

在MHA服务器上执行

# su - admin
# masterha_check_ssh --conf=/etc/mha/app1.conf

Thu Oct 24 00:31:57 2019 - [info] All SSH connection tests passed successfully.
成功

# masterha_check_repl --conf=/etc/mha/app1.conf

若

Thu Oct 24 00:32:27 2019 - [info] MHA::MasterMonitor version 0.57.
Thu Oct 24 00:32:27 2019 - [error][/usr/share/perl5/vendor_perl/MHA/ServerManager.pm, ln301] Got MySQL error when connecting 10.1.1.15(10.1.1.15:3306) :1130:Host '10.1.1.10' is not allowed to connect to this MySQL server, but this is not a MySQL crash. Check MySQL server settings.

证明master中未创建mha用户,前面有。

Thu Oct 24 00:35:15 2019 - [info]  Replication filtering check ok.
Thu Oct 24 00:35:15 2019 - [error][/usr/share/perl5/vendor_perl/MHA/MasterMonitor.pm, ln364] None of slaves can be master. Check failover configuration file or log-bin settings in my.cnf

要求从机也要开启binlog,这是AB模式下主从复制不需要进行的。


Thu Oct 24 00:46:03 2019 - [info]  Replication filtering check ok.
Thu Oct 24 00:46:03 2019 - [error][/usr/share/perl5/vendor_perl/MHA/Server.pm, ln398] 10.1.1.15(10.1.1.15:3306): User slave does not exist or does not have REPLICATION SLAVE privilege! Other slaves can not start replication from this host.

从机没添加slave用户,或者权限不足,前面有


Thu Oct 24 00:46:26 2019 - [info]   Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/usr/local/mysql/data --output_file=/data/mysql/mha/save_binary_logs_test --manager_version=0.57 --start_file=binlog.000001 
Thu Oct 24 00:46:26 2019 - [info]   Connecting to [email protected](10.1.1.13:22).. 
Failed to save binary log: Permission denied:/usr/local/mysql/data/binlog.000001
 at /usr/bin/save_binary_logs line 126.

主机方面的admin访问mysql文件夹权限不足。需要执行
# chown mysql.admin -R /usr/local/mysql

Thu Oct 24 00:48:32 2019 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=10.1.1.14 --slave_ip=10.1.1.14 --slave_port=3306 --workdir=/data/mysql/mha --target_version=5.6.35-log --manager_version=0.57 --relay_log_info=/usr/local/mysql/data/relay-log.info  --relay_dir=/usr/local/mysql/data/  --slave_pass=xxx
Thu Oct 24 00:48:32 2019 - [info]   Connecting to [email protected](10.1.1.14:22).. 
Can't exec "mysqlbinlog": No such file or directory at /usr/share/perl5/vendor_perl/MHA/BinlogManager.pm line 106.
mysqlbinlog version command failed with rc 1:0, please verify PATH, LD_LIBRARY_PATH, and client options

从机都需执行
# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/local/bin/mysqlbinlog;ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql


Thu Oct 24 00:50:15 2019 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=10.1.1.14 --slave_ip=10.1.1.14 --slave_port=3306 --workdir=/data/mysql/mha --target_version=5.6.35-log --manager_version=0.57 --relay_log_info=/usr/local/mysql/data/relay-log.info  --relay_dir=/usr/local/mysql/data/  --slave_pass=xxx
Thu Oct 24 00:50:15 2019 - [info]   Connecting to [email protected](10.1.1.14:22).. 
Creating directory /data/mysql/mha.. failed to create dir:/data/mysql/mha:mkdir /data: Permission denied at /usr/share/perl5/vendor_perl/MHA/NodeUtil.pm line 35.
 at /usr/bin/apply_diff_relay_logs line 495.

从机需都执行
# mkdir /data/mysql/mha -p
# chown admin. -R /data

Thu Oct 24 00:52:46 2019 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=10.1.1.14 --slave_ip=10.1.1.14 --slave_port=3306 --workdir=/data/mysql/mha --target_version=5.6.35-log --manager_version=0.57 --relay_log_info=/usr/local/mysql/data/relay-log.info  --relay_dir=/usr/local/mysql/data/  --slave_pass=xxx
Thu Oct 24 00:52:46 2019 - [info]   Connecting to [email protected](10.1.1.14:22).. 
  Checking slave recovery environment settings..
    Opening /usr/local/mysql/data/relay-log.info ...Could not open relay-log-info file /usr/local/mysql/data/relay-log.info.
 at /usr/bin/apply_diff_relay_logs line 332.

从机需都执行
# chown mysql.admin -R /usr/local/mysql

Thu Oct 24 00:54:27 2019 - [info]   /etc/mha/master_ip_failover.sh 10.1.1.100 1 --command=status --ssh_user=admin --orig_master_host=10.1.1.13 --orig_master_ip=10.1.1.13 --orig_master_port=3306 
Thu Oct 24 00:54:27 2019 - [error][/usr/share/perl5/vendor_perl/MHA/MasterMonitor.pm, ln427] Error happened on checking configurations. Can't exec "/etc/mha/master_ip_failover.sh": No such file or directory at /usr/share/perl5/vendor_perl/MHA/ManagerUtil.pm line 68.
Thu Oct 24 00:54:27 2019 - [error][/usr/share/perl5/vendor_perl/MHA/MasterMonitor.pm, ln525] Error happened on monitoring servers.

MHA服务器配置文件没有移入/etc/mha文件夹,前面有

Thu Oct 24 01:00:27 2019 - [info] MHA::MasterMonitor version 0.57.
Thu Oct 24 01:00:34 2019 - [error][/usr/share/perl5/vendor_perl/MHA/ServerManager.pm, ln188] There is no alive server. We can't do failover

配置文件app1中端口号有问题



终于
MySQL Replication Health is OK.

要成功了,再执行

# nohup masterha_manager --conf=/etc/mha/app1.conf --remove_dead_master_conf --ignore_last_failover &
# 这句执行会卡住,ctrl+c退出来就行
# masterha_check_status --conf=/etc/mha/app1.conf

# 显示
# app1 (pid:16454) is running(0:PING_OK), master:10.1.1.13
# 成功

停止mha语句是
# masterha_stop --conf=/etc/mha/app1.conf

如果MHA启动异常,可以通过cat命令,查看nohup.out(所有错误信息都会写入此文件)
# cat nohup.out

 

5.验证MHA效果

MHA服务器执行
# su - root
# tail -f /data/mha/masterha/app1/app1-3306.log

主数据库执行
# service mysql stop

Master failover to 10.1.1.14(10.1.1.14:3306) completed successfully.
这句话证明了实验的圆满成功

 

你可能感兴趣的:(学习与自我学习,学习)