虚拟机:Oracle VM VirtualBox 5.0.24
操作系统:CentOS Linux release 7.7.1908 (Core)
MySQL服务器:8.0.11 MySQL Community Server - GPL
软件下载地址
主从方式:基于GTID的搭建
主从同步方式:半同步复制
master_ip:192.168.56.101
slave1_ip:192.168.56.103
slave2_ip:192.168.56.105
master主机名:mysql-101
slave1主机名:mysql-103
slave2主机名:mysql-105
master配置文件:/etc/my.cnf
[mysqld]
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
basedir=/usr/local/mysql/mysql-8.0.11
datadir=/data/mysql/
port = 3308
socket = /tmp/mysql.sock
character-set-server=utf8
log-error = /data/mysql/mysqld.log
pid-file = /data/mysql/mysqld.pid
#GTID:
server_id=135 #服务器id
gtid_mode=on #开启gtid模式
enforce_gtid_consistency=on #强制gtid一致性,开启后对于特定create table不被支持
#binlog
log_bin=master-binlog
log-slave-updates=1
binlog_format=row #强烈建议,其他格式可能造成数据不一致
#relay log
skip_slave_start=1
max_connect_errors=1000
default_authentication_plugin = 'mysql_native_password'
slave1 配置文件:/etc/my.cnf
[mysqld]
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
basedir=/data/mysql/mysql-8.0.11/
datadir=/data/mysql/mysql8.0_data/
port = 3308
socket = /tmp/mysql-8-0-11.sock
character-set-server=utf8
log-error = /data/mysql/mysql8.0_data/mysqld.log
pid-file = /data/mysql/mysql8.0_data/mysqld.pid
max_connect_errors=1000
#GTID:
gtid_mode=on
enforce_gtid_consistency=on
server_id=144
#binlog
log-bin=slave-binlog
log-slave-updates=1
binlog_format=row #强烈建议,其他格式可能造成数据不一致
#relay log
skip_slave_start=1
default_authentication_plugin = 'mysql_native_password'
slave2 配置文件:/etc/my.cnf
[mysqld]
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
basedir=/data/mysql/mysql-8.0.11/
datadir=/data/mysql/mysql8.0_data/
port = 3308
socket = /tmp/mysql-8-0-11.sock
character-set-server=utf8
log-error = /data/mysql/mysql8.0_data/mysqld.log
pid-file = /data/mysql/mysql8.0_data/mysqld.pid
#GTID:
gtid_mode=on
enforce_gtid_consistency=on
server_id=143
#binlog
log-bin=slave-binlog
log-slave-updates=1
binlog_format=row #强烈建议,其他格式可能造成数据不一致
#relay log
skip_slave_start=1
show slave hosts;
slave1_ip:192.168.56.103
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.56.101
Master_User: repl
Master_Port: 3308
Connect_Retry: 60
Master_Log_File: master-binlog.000010
Read_Master_Log_Pos: 195
Relay_Log_File: wangbin3-relay-bin.000016
Relay_Log_Pos: 417
Relay_Master_Log_File: master-binlog.000010
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 195
Relay_Log_Space: 668
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 135
Master_UUID: 791b77ef-4587-11ea-9571-0800278ffd3d
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 791b77ef-4587-11ea-9571-0800278ffd3d:1-11
Executed_Gtid_Set: 791b77ef-4587-11ea-9571-0800278ffd3d:1-11,
ac073db4-45c7-11ea-b18a-080027e75c4d:1
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
1 row in set (0.00 sec)
slave2_ip:192.168.56.105
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.56.101
Master_User: repl
Master_Port: 3308
Connect_Retry: 60
Master_Log_File: master-binlog.000010
Read_Master_Log_Pos: 195
Relay_Log_File: wangbin-relay-bin.000013
Relay_Log_Pos: 417
Relay_Master_Log_File: master-binlog.000010
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 195
Relay_Log_Space: 889
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 135
Master_UUID: 791b77ef-4587-11ea-9571-0800278ffd3d
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 791b77ef-4587-11ea-9571-0800278ffd3d:1-11
Executed_Gtid_Set: 0b1232b5-4658-11ea-9c94-080027e12091:1-2,
791b77ef-4587-11ea-9571-0800278ffd3d:1-11
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
1 row in set (0.00 sec)
执行服务器:所有Linux服务器
systemctl status firewalld.service
注:如下面绿色字体 active (running) 表示防火墙是开启状态
systemctl stop firewalld.service
systemctl status firewalld.service
可以看到,inactive (dead) 的字样,说明防火墙已经关闭
注:前面的方法,一旦重启操作系统,防火墙就自动开启。
systemctl disable firewalld.service
使用vim /etc/sysconfig/selinux,将SELINUX=enforcing改为SELINUX=disabled。
重启计算机 reboot 或 init 6
[root@wangbin ~]# sestatus
SELinux status: disabled
注:SELinux status 的值为 disabled 时表示 selinux 防火墙关闭。
三台Linux服务器都必须进行下面的环境准备
编辑三台Linux服务器 /etc/hosts 文件,添加内容如下:
192.168.56.101 mysql-101
192.168.56.103 mysql-103
192.168.56.105 mysql-105
[root@mysql-101 .ssh]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:t+l7E6FCsoK+IXHbFWDBm9BGW87s72dqUMLWREd7LYQ root@mysql-101
The key's randomart image is:
+---[RSA 2048]----+
| +=.....o.. |
| ..+B ..Eo . |
| o.+=o . o . |
| o.=.+ ... |
|. .. .o*S .. . |
| o.o..o....o. |
|..o .. ...o . |
| ... ...o o |
| .. .o+oo . |
+----[SHA256]-----+
[root@mysql-101 .ssh]# ssh-copy-id -i /root/.ssh/id_rsa [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.56.101 (192.168.56.101)' can't be established.
ECDSA key fingerprint is SHA256:i/7Zu1z5Rfdw0n44IIBtNfH1UDBiZXk/YxfbaNqGgAM.
ECDSA key fingerprint is MD5:ac:7e:6e:81:6d:e0:45:7e:21:7a:e5:f0:4e:f5:29:c5.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
[root@mysql-101 .ssh]# ssh-copy-id -i /root/.ssh/id_rsa [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.56.103 (192.168.56.103)' can't be established.
ECDSA key fingerprint is SHA256:i/7Zu1z5Rfdw0n44IIBtNfH1UDBiZXk/YxfbaNqGgAM.
ECDSA key fingerprint is MD5:ac:7e:6e:81:6d:e0:45:7e:21:7a:e5:f0:4e:f5:29:c5.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
[root@mysql-101 .ssh]# ssh-copy-id -i /root/.ssh/id_rsa [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.56.105 (192.168.56.105)' can't be established.
ECDSA key fingerprint is SHA256:VqbHYBYA3aBetOKngcPuoaHOShoFUx+80adb2SqpZ24.
ECDSA key fingerprint is MD5:16:aa:25:6c:1d:31:c2:e5:99:00:43:b5:83:21:ea:94.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
ssh root@192.168.56.101
ssh root@192.168.56.103
ssh root@192.168.56.105
操作服务器:所有Linux服务器
wget http://mirrors.sohu.com/fedora-epel/epel-release-latest-7.noarch.rpm
yum install -y epel-release-latest-7.noarch.rpm
操作服务器:所有Linux服务器
把 /etc/yum.repos.d/epel.repo 文件中的 gpgcheck 参数值设置成 0 。
操作服务器:所有Linux服务器
yum list perl-DBD-MySQL ncftp perl-DBI
yum install -y perl-DBD-MySQL ncftp perl-DBI
Linux服务器:slave2
yum install -y perl-Config-Tiny.noarch perl-Time-HiRes.x86_64 perl-Parallel-ForkManager perl-Log-Dispatch.noarch
软件包下载地址
rpm -ivh mha4mysql-node-0.57-0.el7.noarch.rpm
生产环境中监控服务要尽量安装在单独的Linux服务器上面
rpm -ivh mha4mysql-manager-0.57-0.el7.noarch.rpm
create user dba_mha@'192.168.56.%' identified by '123456';
grant all privileges on *.* to dba_mha@'192.168.56.%';
mkdir -p /etc/mha
mkdir -p /etc/mha
mkdir -p /home/mha
vim /etc/mha/mysql-mha.conf
[server default]
user=dba_mha
password=123456
manager_workdir=/home/mha
manager_log=/home/mha/manager.log
remote_workdir=/home/mha
ssh_user=root
repl_user=repl
repl_password=repl
ping_interval=1
master_binlog_dir=/data/mysql
ssh_port=22
master_ip_failover_script = /usr/bin/master_ip_failover
secondary_check_script = /usr/bin/masterha_secondary_check -s 192.168.56.101 -s 192.168.56.103 -s 192.168.56.101
[server1]
hostname=192.168.56.101
candidate_master=1
[server2]
hostname=192.168.56.103
candidate_master=1
[server3]
hostname=192.168.56.105
no_master=1
vim /usr/bin/master_ip_failover
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use Getopt::Long;
my (
$command, $orig_master_host, $orig_master_ip,$ssh_user,
$orig_master_port, $new_master_host, $new_master_ip,$new_master_port,
$orig_master_ssh_port,$new_master_ssh_port,$new_master_user,$new_master_password
);
my $vip = '192.168.56.110/24';
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig enp0s3:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig enp0s3:$key down";
my $ssh_Bcast_arp= "/sbin/arping -I enp0s3 -c 3 -A 192.168.56.110";
GetOptions(
'command=s' => \$command,
'ssh_user=s' => \$ssh_user,
'orig_master_host=s' => \$orig_master_host,
'orig_master_ip=s' => \$orig_master_ip,
'orig_master_port=i' => \$orig_master_port,
'orig_master_ssh_port=i' => \$orig_master_ssh_port,
'new_master_host=s' => \$new_master_host,
'new_master_ip=s' => \$new_master_ip,
'new_master_port=i' => \$new_master_port,
'new_master_ssh_port' => \$new_master_ssh_port,
'new_master_user' => \$new_master_user,
'new_master_password' => \$new_master_password
);
exit &main();
sub main {
$ssh_user = defined $ssh_user ? $ssh_user : 'root';
print "\n\nIN SCRIPT TEST====$ssh_user|$ssh_stop_vip==$ssh_user|$ssh_start_vip===\n\n";
if ( $command eq "stop" || $command eq "stopssh" ) {
my $exit_code = 1;
eval {
print "Disabling the VIP on old master: $orig_master_host \n";
&stop_vip();
$exit_code = 0;
};
if ($@) {
warn "Got Error: $@\n";
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "start" ) {
my $exit_code = 10;
eval {
print "Enabling the VIP - $vip on the new master - $new_master_host \n";
&start_vip();
&start_arp();
$exit_code = 0;
};
if ($@) {
warn $@;
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "status" ) {
print "Checking the Status of the script.. OK \n";
exit 0;
}
else {
&usage();
exit 1;
}
}
sub start_vip() {
`ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
sub stop_vip() {
`ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}
sub start_arp() {
`ssh $ssh_user\@$new_master_host \" $ssh_Bcast_arp \"`;
}
sub usage {
print
"Usage: master_ip_failover --command=start|stop|stopssh|status --ssh_user=user --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
执行权限设置:chmod +x /usr/bin/master_ip_failover
[root@mysql-105 mha]# masterha_check_ssh --conf=/etc/mha/mysql-mha.conf
Tue Feb 4 22:10:02 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Feb 4 22:10:02 2020 - [info] Reading application default configuration from /etc/mha/mysql-mha.conf..
Tue Feb 4 22:10:02 2020 - [info] Reading server configuration from /etc/mha/mysql-mha.conf..
Tue Feb 4 22:10:02 2020 - [info] Starting SSH connection tests..
Tue Feb 4 22:10:04 2020 - [debug]
Tue Feb 4 22:10:03 2020 - [debug] Connecting via SSH from root@192.168.56.105(192.168.56.105:22) to root@192.168.56.101(192.168.56.101:22)..
Tue Feb 4 22:10:03 2020 - [debug] ok.
Tue Feb 4 22:10:03 2020 - [debug] Connecting via SSH from root@192.168.56.105(192.168.56.105:22) to root@192.168.56.103(192.168.56.103:22)..
Tue Feb 4 22:10:04 2020 - [debug] ok.
Tue Feb 4 22:10:04 2020 - [debug]
Tue Feb 4 22:10:02 2020 - [debug] Connecting via SSH from root@192.168.56.101(192.168.56.101:22) to root@192.168.56.103(192.168.56.103:22)..
Tue Feb 4 22:10:04 2020 - [debug] ok.
Tue Feb 4 22:10:04 2020 - [debug] Connecting via SSH from root@192.168.56.101(192.168.56.101:22) to root@192.168.56.105(192.168.56.105:22)..
Tue Feb 4 22:10:04 2020 - [debug] ok.
Tue Feb 4 22:10:05 2020 - [debug]
Tue Feb 4 22:10:02 2020 - [debug] Connecting via SSH from root@192.168.56.103(192.168.56.103:22) to root@192.168.56.101(192.168.56.101:22)..
Tue Feb 4 22:10:04 2020 - [debug] ok.
Tue Feb 4 22:10:04 2020 - [debug] Connecting via SSH from root@192.168.56.103(192.168.56.103:22) to root@192.168.56.105(192.168.56.105:22)..
Tue Feb 4 22:10:05 2020 - [debug] ok.
Tue Feb 4 22:10:05 2020 - [info] All SSH connection tests passed successfully.
出现 All SSH connection tests passed successfully 表示配置成功
[root@mysql-105 mha]# masterha_check_repl --conf=/etc/mha/mysql-mha.conf
Tue Feb 4 22:42:22 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Feb 4 22:42:22 2020 - [info] Reading application default configuration from /etc/mha/mysql-mha.conf..
Tue Feb 4 22:42:22 2020 - [info] Reading server configuration from /etc/mha/mysql-mha.conf..
Tue Feb 4 22:42:22 2020 - [info] MHA::MasterMonitor version 0.57.
Tue Feb 4 22:42:24 2020 - [info] GTID failover mode = 1
Tue Feb 4 22:42:24 2020 - [info] Dead Servers:
Tue Feb 4 22:42:24 2020 - [info] Alive Servers:
Tue Feb 4 22:42:24 2020 - [info] 192.168.56.101(192.168.56.101:3308)
Tue Feb 4 22:42:24 2020 - [info] 192.168.56.103(192.168.56.103:3308)
Tue Feb 4 22:42:24 2020 - [info] 192.168.56.105(192.168.56.105:3308)
Tue Feb 4 22:42:24 2020 - [info] Alive Slaves:
Tue Feb 4 22:42:24 2020 - [info] 192.168.56.103(192.168.56.103:3308) Version=8.0.11 (oldest major version between slaves) log-bin:enabled
Tue Feb 4 22:42:24 2020 - [info] GTID ON
Tue Feb 4 22:42:24 2020 - [info] Replicating from 192.168.56.101(192.168.56.101:3308)
Tue Feb 4 22:42:24 2020 - [info] Primary candidate for the new Master (candidate_master is set)
Tue Feb 4 22:42:24 2020 - [info] 192.168.56.105(192.168.56.105:3308) Version=8.0.11 (oldest major version between slaves) log-bin:enabled
Tue Feb 4 22:42:24 2020 - [info] GTID ON
Tue Feb 4 22:42:24 2020 - [info] Replicating from 192.168.56.101(192.168.56.101:3308)
Tue Feb 4 22:42:24 2020 - [info] Not candidate for the new Master (no_master is set)
Tue Feb 4 22:42:24 2020 - [info] Current Alive Master: 192.168.56.101(192.168.56.101:3308)
Tue Feb 4 22:42:24 2020 - [info] Checking slave configurations..
Tue Feb 4 22:42:24 2020 - [info] read_only=1 is not set on slave 192.168.56.103(192.168.56.103:3308).
Tue Feb 4 22:42:24 2020 - [info] read_only=1 is not set on slave 192.168.56.105(192.168.56.105:3308).
Tue Feb 4 22:42:24 2020 - [info] Checking replication filtering settings..
Tue Feb 4 22:42:24 2020 - [info] binlog_do_db= , binlog_ignore_db=
Tue Feb 4 22:42:24 2020 - [info] Replication filtering check ok.
Tue Feb 4 22:42:24 2020 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.
Tue Feb 4 22:42:24 2020 - [info] Checking SSH publickey authentication settings on the current master..
Tue Feb 4 22:42:25 2020 - [info] HealthCheck: SSH to 192.168.56.101 is reachable.
Tue Feb 4 22:42:25 2020 - [info]
192.168.56.101(192.168.56.101:3308) (current master)
+--192.168.56.103(192.168.56.103:3308)
+--192.168.56.105(192.168.56.105:3308)
Tue Feb 4 22:42:25 2020 - [info] Checking replication health on 192.168.56.103..
Tue Feb 4 22:42:25 2020 - [info] ok.
Tue Feb 4 22:42:25 2020 - [info] Checking replication health on 192.168.56.105..
Tue Feb 4 22:42:25 2020 - [info] ok.
Tue Feb 4 22:42:25 2020 - [info] Checking master_ip_failover_script status:
Tue Feb 4 22:42:25 2020 - [info] /usr/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.56.101 --orig_master_ip=192.168.56.101 --orig_master_port=3308
IN SCRIPT TEST====root|/sbin/ifconfig enp0s3:1 down==root|/sbin/ifconfig enp0s3:1 192.168.56.110/24===
Checking the Status of the script.. OK
Tue Feb 4 22:42:25 2020 - [info] OK.
Tue Feb 4 22:42:25 2020 - [warning] shutdown_script is not defined.
Tue Feb 4 22:42:25 2020 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
/sbin/ifconfig enp0s3:1 192.168.56.110/24
masterha_manager --conf=/etc/mha/mysql-mha.conf &