ip | 角色 |
---|---|
8.131.87.12 | master |
106.75.32.128 | slave1 |
106.75.47.125 | slave2 |
106.75.75.142 | mha |
步骤参考这个笔记,不再赘述
mysql集群之主从同步
步骤参考这个笔记,不再赘述
mysql集群之半同步复制
注意,需要确认的项,直接回车采用默认值即可
生成的秘钥查看:cat /root/.ssh/id_rsa.pub
ssh-keygen -t rsa
ssh-copy-id 106.75.75.142
备注:
以在MHA Manager服务器上检查下,看.ssh/authorized_keys文件是否包含3个公钥
cat /root/.ssh/authorized_keys
cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
scp /root/.ssh/authorized_keys root@8.131.87.12:/root/.ssh/authorized_keys
scp /root/.ssh/authorized_keys root@106.75.32.128:/root/.ssh/authorized_keys
scp /root/.ssh/authorized_keys root@106.75.47.125:/root/.ssh/authorized_keys
提示【authorized_keys】表示成功,有错误的话会提示具体的报错信息
可以MHA Manager执行下面命令,检测下与三台MySQL是否实现ssh互通。
ssh 8.131.87.12
exit
ssh 106.75.32.128
exit
ssh 106.75.47.125
exit
可以正常登陆就表示ssh通了
上传到相应的服务器
MHA的Node依赖于perl-DBD-MySQL,所以要先安装perl-DBD-MySQL。
yum install perl-DBD-MySQL -y
rpm -ivh mha4mysql-node-0.58-0.el7.centos.noarch.rpm
MHA的manager又依赖了perl-Config-Tiny、perl-Log-Dispatch、perl-Parallel-ForkManager,也分别
进行安装。
首先安装依赖
yum install perl-Config-Tiny
yum install perl-Log-Dispatch
yum install perl-Parallel-ForkManager
安装manager
rpm -ivh mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
MHA Manager服务器需要为每个监控的 Master/Slave 集群供一个专用的配置文件,而所有的Master/Slave 集群也可共享全局配置。
#目录说明
#/var/log (CentOS目录)
# /mha (MHA监控根目录)
# /app1 (MHA监控实例根目录)
# /manager.log (MHA监控实例日志文件)
mkdir -p /var/log/mha/app1
touch /var/log/mha/app1/manager.log
create user 'mha'@'%' identified by 'mha123';
grant all on *.* to mha@'%' identified by 'mha123';
flush privileges;
在master mysql的主库建一个新用户
create user ‘mha’@’%’ identified by ‘mha123’;
grant all on . to mha@’%’ identified by ‘mha123’;
flush privileges;
vim /etc/masterha_default.cnf
[server default]
user=mha
password=mha123
port=3306
#ssh登录账号
ssh_user=root
#从库复制账号和密码
repl_user=root
repl_password=root
port=3306
#ping次数
ping_interval=1
#二次检查的主机
secondary_check_script=masterha_secondary_check -s 8.131.87.12 -s 106.75.32.128 -s 106.75.47.125 # 一主两从三个库的ip
etc下创建mha文件夹,文件加内创建app1.cnf文件
mkdir -p /etc/mha
vim /etc/mha/app1.cnf
app1.cnf配置如下:
[server default]
#MHA监控实例根目录
manager_workdir=/var/log/mha/app1
#MHA监控实例日志文件
manager_log=/var/log/mha/app1/manager.log
#[serverx] 服务器编号
#hostname 主机名
#candidate_master 可以做主库
#master_binlog_dir binlog日志文件目录
[server1]
hostname=8.131.87.12
candidate_master=1
master_binlog_dir="/var/lib/mysql"
[server2]
hostname=106.75.32.128
candidate_master=1
master_binlog_dir="/var/lib/mysql"
[server3]
hostname=106.75.47.125
candidate_master=1
master_binlog_dir="/var/lib/mysql"
在MHA Manager服务器上执行:
masterha_check_ssh --conf=/etc/mha/app1.cnf
在MHA Manager服务器上执行:
masterha_check_repl --conf=/etc/mha/app1.cnf
出现“MySQL Replication Health is OK.”证明MySQL复制集群没有问题
遇到的问题:
[error][/usr/share/perl5/vendor_perl/MHA/ServerManager.pm, ln443] Binlog filtering check failed on 106.75.32.128(106.75.32.128:3306)! All log-bin enabled servers must have same binlog filtering rules (same binlog-do-db and binlog-ignore-db). Check SHOW MASTER STATUS output and set my.cnf correctly.
Sat Jan 30 03:27:19 2021 - [warning] Bad Binlog/Replication filtering rules:
8.131.87.12 (current_master)
Binlog_Do_DB: lagou
Binlog_Ignore_DB: information_schema,performance_schema,sys
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
106.75.32.128
Binlog_Do_DB:
Binlog_Ignore_DB:
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
106.75.47.125
Binlog_Do_DB:
Binlog_Ignore_DB:
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Sat Jan 30 03:27:19 2021 - [error][/usr/share/perl5/vendor_perl/MHA/MasterMonitor.pm, ln359] Slave configurations is not valid.
Sat Jan 30 03:27:19 2021 - [error][/usr/share/perl5/vendor_perl/MHA/MasterMonitor.pm, ln427] Error happened on checking configurations. at /usr/bin/masterha_check_repl line 48.
Sat Jan 30 03:27:19 2021 - [error][/usr/share/perl5/vendor_perl/MHA/MasterMonitor.pm, ln525] Error happened on monitoring servers.
Sat Jan 30 03:27:19 2021 - [info] Got exit code 1 (Not master dead).
MySQL Replication Health is NOT OK!
问题思考:
意思就是主库和从库的my.cnf中配置的binlog-ignore-db和binlog-do-db要保持一致
解决方案:
把两台从库中的my.cnf配置进行调整
binlog-ignore-db=performance_schema
binlog-ignore-db=information_schema
binlog-ignore-db=sys
binlog-do-db=lagou
nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --
ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &
查看监控状态命令如下:
masterha_check_status --conf=/etc/mha/app1.cnf
查看监控日志命令如下:
tail -f /var/log/mha/app1/manager.log
MySQL主从复制结构下,如何判定是异步复制还是半同步复制?
show global status like '%Rpl_semi%';
ssh 8.131.87.12
ssh 106.75.32.128
ssh 106.75.47.125
在MHA Manager服务器上执行:
masterha_check_ssh --conf=/etc/mha/app1.cnf
在MHA Manager服务器上执行:
masterha_check_repl --conf=/etc/mha/app1.cnf
启动mha
nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --
ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &
查看监控状态命令如下:
masterha_check_status --conf=/etc/mha/app1.cnf
查看监控日志命令如下:
tail -f /var/log/mha/app1/manager.log
systemctl stop mysqld
masterha_master_switch --conf=/etc/mha/app1.cnf --master_state=alive --orig_master_is_new_slave --new_master_host=8.131.87.12 --new_master_port=3306
因为每一台都有可能在mha的处理指标变成主库。如,执行以下命令检测mysql主从复制的时候,如果主库和从库的my.cnf中配置的binlog-ignore-db和binlog-do-db不一致,会检测不通过
masterha_check_repl --conf=/etc/mha/app1.cnf