环境&软件 | 版本 |
---|---|
虚拟机&VMware Fusion | 12.1 |
服务器&CentOS | 7.8 |
数据库&Mysql | 5.7.28 |
远程连接&SSH Shell | 16.03 |
机器名称 | IP | 角色 |
---|---|---|
Mysql_Master | 192.168.91.134 | 可读写、主库 |
Mysql_Slave1 | 192.168.91.135 | 只读、从库 |
Mysql_Slave2 | 192.168.91.136 | 只读、从库 |
Mysql_MHA | 192.168.91.133 | 高可用监控 |
可以通过wget下载:
wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar
已下载可通过scp传输:
scp -r mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar [email protected]:/usr/local/software
rpm -qa|grep mariadb
rpm -e mariadb-libs-5.5.68-1.el7.x86_64 --nodeps
tar xvf mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar
rpm -ivh mysql-community-common-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-compat-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.28-1.el7.x86_64.rpm --nodeps
rpm -ivh mysql-community-devel-5.7.28-1.el7.x86_64.rpm
mysqld --initialize --user=mysql
cat /var/log/mysqld.log | grep password
systemctl start mysqld
systemctl enable mysqld
mysql -uroot -p{cat查看的初始密码}
set password for root@localhost = password('123456');
grant all privileges on *.* to 'root'@'%' identified by 'root';
flush privileges;
1. 修改配置文件
vim /etc/my.cnf
-----------------------------------追加配置内容:
#bin_log配置
log_bin=mysql-bin
server-id=1
sync-binlog=1
# 忽略database库设置
binlog-ignore-db=information_schema
binlog-ignore-db=mysql
binlog-ignore-db=performance_schema
binlog-ignore-db=sys
#relay_log配置
relay_log=mysql-relay-bin
log_slave_updates=1
relay_log_purge=0
#gtid配置
#开启gtid
#gtid_mode=on
#enforce_gtid_consistency=1
-----------------------------------配置文件完成
2. 重启mysql
systemctl restart mysqld
3. 授权同步
grant replication slave on *.* to 'root'@'%' identified by 'root';
flush privileges;
4. 查看主库状态
show master status;
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| mysql-bin.000001 | 1398 | | information_schema,mysql,performance_schema,sys | |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
1. 修改配置文件:
vim /etc/my.cnf
-----------------------------------追加配置内容:
#bin_log配置
log_bin=mysql-bin
#服务器ID,从库1是2,从库2是3server-id=2
server-id=2
sync-binlog=1
# 忽略database库设置
binlog-ignore-db=information_schema
binlog-ignore-db=mysql
binlog-ignore-db=performance_schema
binlog-ignore-db=sys
#relay_log配置
relay_log=mysql-relay-bin
log_slave_updates=1
relay_log_purge=0
read_only=1
#gtid配置
#开启gtid
#gtid_mode=on
#enforce_gtid_consistency=1
-----------------------------------配置文件完成
2. 重启mysql:systemctl restart mysqld
3. 设置连接的主库信息并开启slave
change master to master_host='192.168.91.134',master_port=3306,master_user='root',master_password='123456'
,master_log_file='mysql-bin.000001',master_log_pos=1398;
start slave;
此时主从复制已完成,主库的增删改可以同步到从库
// 是否有动态加载 yes可以动态安装插件
select @@have_dynamic_loading ;
//查看插件
show plugins;
// 安装半同步插件并起别名为semisync_master.so
install plugin rpl_semi_sync_master soname 'semisync_master.so';
// 查看半同步参数
show variables like'%semi%';
| rpl_semi_sync_master_enabled | OFF |
| rpl_semi_sync_master_timeout | 10000 |
| rpl_semi_sync_master_trace_level | 32 |
| rpl_semi_sync_master_wait_no_slave | ON |
// 设置开启以及超时时间为1s
set global rpl_semi_sync_master_enabled=1;
set global rpl_semi_sync_master_timeout=1000;
install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
show variables like'%semi%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled | OFF |
| rpl_semi_sync_slave_trace_level | 32 |
+---------------------------------+-------+
set global rpl_semi_sync_slave_enabled=1;
// 设置完后重启
stop slave;
start slave;
cat /var/log/mysqld.log
可以看到日志打印:
Start semi-sync binlog_dump to slave (server_id: 2), pos(mysql-bin.000001, 1398)
1. 每台生成公钥和私钥,换行回车采用默认值
ssh-keygen -t rsa
2. Mysql服务器将公钥拷到MHA Manager服务器上
ssh-copy-id 192.168.91.133
3. 在MHAManager服务器向其他服务器分发公钥信息
scp /root/.ssh/authorized_keys 192.168.91.134:$PWD
scp /root/.ssh/authorized_keys 192.168.91.135:$PWD
scp /root/.ssh/authorized_keys 192.168.91.136:$PWD
4. 验证服务器之间是否实现了免密登录,在MHAManager服务器运行
ssh 192.168.91.134
// 在四台服务器上安装mha4mysql-node。
// MHA的Node依赖于perl-DBD-MySQL,所以要先安装perl-DBD-MySQL。
yum install perl-DBD-MySQL -y
// 下载node
wget https://github.com/yoshinorim/mha4mysql-node/releases/download/v0.58/mha4mysql-node-0.58-0.el7.centos.noarch.rpm
// 安装
rpm -ivh mha4mysql-node-0.58-0.el7.centos.noarch.rpm
// MHA的manager又依赖了perl-Config-Tiny、perl-Log-Dispatch、perl-Parallel-ForkManager,也分别进行安装。
wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -ivh epel-release-latest-7.noarch.rpm
yum install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager -y
// 安装MHA-manager
wget https://github.com/yoshinorim/mha4mysql-manager/releases/download/v0.58/mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
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
vim /etc/masterha_default.cnf
[server default]
#用户名
user=root
#密码
password=123456
#ssh登录账号
ssh_user=root
#主从复制账号
repl_user=root
#主从复制密码
repl_password=123456
#ping次数
ping_interval=1
#二次检查的主机
secondary_check_script=masterha_secondary_check -s 192.168.91.134 -s 192.168.91.135 -s 192.168.91.136
vim /etc/mha/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=192.168.91.129
candidate_master=1
master_binlog_dir="/var/lib/mysql"
[server2]
hostname=192.168.91.131
candidate_master=1
master_binlog_dir="/var/lib/mysql"
[server3]
hostname=192.168.91.132
candidate_master=1
master_binlog_dir="/var/lib/mysql"
// 执行ssh通信检测
masterha_check_ssh --conf=/etc/mha/app1.cnf
// 检测MySQL主从复制
masterha_check_repl --conf=/etc/mha/app1.cnf
检查完毕出现“MySQL Replication Health is OK.”证明MySQL复制集群没有问题。
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 &
此时Mysql主从模式以及MHA高可用架构搭建已完成,可以验证部署效果,模拟主库宕机并查看MHA监控日志。
tail -f /var/log/mha/app1/manager.log