大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
一、背景介绍
网站建设公司,为您提供网站建设,网站制作,网页设计及定制网站建设服务,专注于企业网站制作,高端网页制作,对垃圾桶等多个行业拥有丰富的网站建设经验的网站建设公司。专业网站设计,网站优化推广哪家好,专业seo优化优化,H5建站,响应式网站。
在传统的MySQL一主一从或一主多从结构中,主节点存在单点故障隐患,为了解决该故障,进而引入了MHA高可用的方式。MHA由manager和node两部分组成,manager可以安装在一个独立的服务器上或非master角色的MySQL服务器上,1个manager可以管多个node集合,每一个master/node集群称为一个application,所有集群可以使用一个配置文件或者为每个集群创建单独的配置文件,目前MHA最少由3个主机组成
二、实现原理
MHA是在MySQL主从复制基础上实现的高可用,其实现原理是:
在正常情况下,MHA的manager节点对所有node节点周期性进行健康状态检测,当MySQL的master节点出现故障时,MHA的manager试图从宕机的MySQL的master上将二进制日志保存到本地,并与所有node节点的中继日志进行对比,将拥有最新同步状态的slave主机提升为master主机,所有slave主机指向新的master主机。
三、实验环境
本次实验采用CentOS7.4系统,数据库版本5.5.56-MariaDB,MHA版本mha4mysql-manager-0.58。因为所有服务器之间要传输中继日志,并且还需要指定master,所有所有主机间要建立密钥认证,master和candidate节点需要创建用于从服务器复制的二进制日志的账号,MySQL每个服务器都需要创建可以指定master的账号,并且master和candidate节点不能开启relay_log_purge选项,为了最大程度的保证数据的不丢失,建议将candidate配置成半同步形式。
MHA角色 | MySQL角色 | IP地址 | 开启MySQL功能 |
manager | — | 172.16.10.30/24 | — |
node | master | 172.16.10.40/24 | 1.二进制日志,中继日志 2.复制账号,管理账号 3.中继日志不能裁剪 4.半同步master插件 |
node/candidate | slave | 172.16.10.50/24 | 1.二进制日志,中继日志 2.复制账号,管理账号 3.中继日志不能裁剪 4.半同步slave插件 5.只读模式 |
node | slave | 172.16.10.60/24 | 1.中继日志 2.只读模式 |
四、准备操作
既然是集群时间同步和名称解析就需要提前完成,此处不过多介绍
1.所有主机互信
可以在一个主机生成一对密钥,将这对密钥和know_hosts文件拷贝给其余主机的方式,不需要所有主机都生成密钥对相互拷贝
[root@host3 ~]# ssh-keygen
[root@host3 ~]# ssh-copy-id 172.16.10.30
[root@host3 ~]# ssh-copy-id 172.16.10.40
[root@host3 ~]# ssh-copy-id 172.16.10.50
[root@host3 ~]# ssh-copy-id 172.16.10.60
[root@host3 .ssh]# scp -r id_rsa known_hosts root@172.16.10.40:/root/.ssh/
[root@host3 .ssh]# scp -r id_rsa known_hosts root@172.16.10.50:/root/.ssh/
[root@host3 .ssh]# scp -r id_rsa known_hosts root@172.16.10.60:/root/.ssh/
2.安装Mariadb数据库
[root@host4 ~]# yum install mariadb-server.x86_64 -y
[root@host5 ~]# yum install mariadb-server.x86_64 -y
[root@host6 ~]# yum install mariadb-server.x86_64 -y
3.Master主机操作
(1)修改配置文件
[root@host4 ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip_name_resolve=ON
relay_log=mysql-relaylog
relay_log_index=mysql-relaylog
relay_log_purge=OFF
slow_query_log=ON
server-id=10
innodb_file_per_table=ON
binlog_format=ROW
log_bin=mysql-binlog
log_slave_updates=ON
(2)启动并进入MySQL
[root@host4 ~]# systemctl start mariadb.service
[root@host4 ~]# mysql
(3)创建用于主从复制和管理数据库的账号
MariaDB [(none)]> grant replication slave on *.* to 'bak'@'172.16.10.%' identified by 'bakpass';
MariaDB [(none)]> grant all on *.* to 'man'@'172.16.10.%' identified by 'manpass';
MariaDB [(none)]> flush privileges;
(4)开启并激活半同步插件
MariaDB [(none)]> install plugin rpl_semi_sync_master soname 'semisync_master.so';
MariaDB [(none)]> set global rpl_semi_sync_master_enabled=ON;
(5)确定当前binlog日志及position
MariaDB [(none)]> show master status;
+---------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------------+----------+--------------+------------------+
| mysql-binlog.000003 | 610 | | |
+---------------------+----------+--------------+------------------+
4.Candidate主机操作
(1)修改配置文件
[root@host5 ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip_name_resolve=ON
relay_log=mysql-relaylog
relay_log_index=mysql-relaylog
relay_log_purge=OFF
read_only=ON
slow_query_log=ON
server-id=20
innodb_file_per_table=ON
binlog_format=ROW
log_bin=mysql-binlog
log_slave_updates=ON
(2)启动并进入MySQL
[root@host5 ~]# systemctl start mariadb.service
[root@host5 ~]# mysql
(3)创建用于主从复制和管理数据库的账号
MariaDB [(none)]> set global read_only=0;
MariaDB [(none)]> grant replication slave on *.* to 'bak'@'172.16.10.%' identified by 'bakpass';
MariaDB [(none)]> grant all on *.* to 'man'@'172.16.10.%' identified by 'manpass';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> set global read_only=1;
(4)开启并激活半同步插件
MariaDB [(none)]> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
MariaDB [(none)]> set global rpl_semi_sync_slave_enabled=ON;
(5)指定主服务器及主服务器当前的binlog日志和position
MariaDB [hellodb]> change master to
-> master_host='172.16.10.40',
-> master_user='bak',
-> master_password='bakpass',
-> master_port=3306,
-> master_log_file='mysql-binlog.000003',
-> master_log_pos=610,
-> master_connect_retry=10;
(6)启动从节点
MariaDB [hellodb]> start slave [IO_THREAD | SQL_THREAD];
(7)查看从节点状态(Slave_IO_Running和Slave_SQL_Running显示Yes成功)
MariaDB [(none)]> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.10.40
Master_User: bak
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql-binlog.000003
Read_Master_Log_Pos: 610
Relay_Log_File: mysql-relaylog.000002
Relay_Log_Pos: 532
Relay_Master_Log_File: mysql-binlog.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
5.Slave主机操作
(1)修改配置文件
[root@host6 ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip_name_resolve=ON
relay_log=mysql-relaylog
relay_log_index=mysql-relaylog
relay_log_purge=ON
read_only=ON
slow_query_log=ON
server-id=30
innodb_file_per_table=ON
(2)启动并进入MySQL
[root@host6 ~]# systemctl start mariadb.service
[root@host6 ~]# mysql
(3)创建用于管理数据库的账号
MariaDB [(none)]> set global read_only=0;
MariaDB [(none)]> grant replication slave on *.* to 'bak'@'172.16.10.%' identified by 'bakpass';
MariaDB [(none)]> grant all on *.* to 'man'@'172.16.10.%' identified by 'manpass';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> set global read_only=1;
(4)指定主服务器及主服务器当前的binlog日志和position
MariaDB [hellodb]> change master to
-> master_host='172.16.10.40',
-> master_user='bak',
-> master_password='bakpass',
-> master_port=3306,
-> master_log_file='mysql-binlog.000003',
-> master_log_pos=610,
-> master_connect_retry=10;
(5)启动从节点
MariaDB [hellodb]> start slave [IO_THREAD | SQL_THREAD];
(6)查看从节点状态(Slave_IO_Running和Slave_SQL_Running显示Yes成功)
MariaDB [(none)]> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.10.40
Master_User: bak
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql-binlog.000003
Read_Master_Log_Pos: 610
Relay_Log_File: mysql-relaylog.000002
Relay_Log_Pos: 532
Relay_Master_Log_File: mysql-binlog.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
五、MHA高可用实现
1.安装MHA软件
(1)主节点
[root@host3 mha]# yum install mha4mysql-manager-0.58-0.el7.centos.noarch.rpm mha4mysql-node-0.58-0.el7.centos.noarch.rpm -y
-masterha_check_ssh:MHA依赖的SSH环境检测工具;
-masterha_check_repl:MySQL复制环境检测工具;
-masterha_manager:MHA服务主程序;
-masterha_check_status:MHA运行状态探测工具;
-masterha_master_monitor:MySQL master节点可用性检测工具;
-masterha_master_switch:master节点切换工具;
-masterha_conf_host:添加或删除配置的节点;
-masterha_stop:关闭MHA服务的工具;
(2)从节点
[root@host4 mha]# yum install mha4mysql-node-0.58-0.el7.centos.noarch.rpm -y
-save_binary_logs:保存和复制master的二进制日志;
-apply_diff_relay_logs:识别差异的中继日志事件并用于其他slave;
-fiter_mysqlbinlog:去除不必要的ROLLBACK事件(MHA已不再使用这个工具);
-purge_relay_logs:清除中继日志(不会阻塞SQL线程);
2.在MHA的Manager节点创建配置文件并检测
[root@host3 ~]# mkdir /etc/mha
[root@host3 ~]# vim /etc/mha/app1.conf
[server default]
user=man
password=manpass
manager_workdir=/data/masterha/app1
manager_log=/data/masterha/app1/manager.log
remote_workdir=/data/masterha/app1
ssh_user=root
ping_interval=1
repl_user=bak
repl_password=bakpass
[server1]
hostname=172.16.10.40
candidate_master=1
[server2]
hostname=172.16.10.50
candidate_master=1
[server3]
hostname=172.16.10.60
no_master=1
[root@host3 mha]# masterha_check_ssh --conf=/etc/mha/app1.conf
[root@host3 mha]# masterha_check_repl --conf=/etc/mha/app1.conf
此时的警告忽略,MHA可以使用--global_conf选项为多个application指定相同的配置
3.启动并查看MHA
[root@host3 ~]# masterha_manager --conf=/etc/mha/app1.conf 2>&1 &
[root@host3 ~]# masterha_check_status --conf=/etc/mha/app1.conf
app1 (pid:2633) is running(0:PING_OK), master:172.16.10.40
4.模拟master节点故障
(1)关闭master节点的MySQL服务
[root@host4 ~]# mysqladmin -uroot shutdown
(2)在Manager节点查看此时状态
[root@host3 ~]# masterha_check_status --conf=/etc/mha/app1.conf
app1 is stopped(2:NOT_RUNNING).
当由node节点出现故障时MHA会自动关闭,故障节点恢复后,不会自动启动
(3)查看Candidate节点状态
MariaDB [(none)]> show slave hosts;
+-----------+------+------+-----------+
| Server_id | Host | Port | Master_id |
+-----------+------+------+-----------+
| 30 | | 3306 | 20 |
+-----------+------+------+-----------+
MariaDB [(none)]> show global variables like '%read_only%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| read_only | OFF |
+---------------+-------+
能够发现Candidate节点已经升级为Master节点,并且read_only选项也自动关闭(尽管配置文件中该选项还存在)
(4)slave节点查看状态
MariaDB [(none)]> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.10.50
Master_User: bak
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql-binlog.000004
Read_Master_Log_Pos: 605
Relay_Log_File: mysql-relaylog.000002
Relay_Log_Pos: 532
Relay_Master_Log_File: mysql-binlog.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
5.恢复故障主机
(1)将故障修复后的主机上线
(2)修改my.cnf文件,加入read_only=ON选项
(3)使用mysqldump等备份工具对当前master主机上的数据库进行备份
(4)使用备份在故障主机上恢复,启动MySQL服务,再指定新的Master主机和binlog,如果宕机的是半同步的master主机,还需要调换半同步的master和slave角色,并且重启IO_thread
(5)在MHA的manager节点上重启MHA
补充说明
MHA检查复制状态时出现如下报错:
[root@host3 ~]# masterha_check_repl --conf=/etc/mha/app1.conf
Checking if super_read_only is defined and turned on..DBD::mysql::st execute failed: Unknown system variable'super_read_only' at /usr/share/perl5/vendor_perl/MHA/SlaveUtil.pm line 245.
Fri May 4 11:09:28 2018 - [error][/usr/share/perl5/vendor_perl/MHA/MasterMonitor.pm, ln208] Slaves settings check failed!
Fri May 4 11:09:28 2018 - [error][/usr/share/perl5/vendor_perl/MHA/MasterMonitor.pm, ln416] Slave configuration failed.
Fri May 4 11:09:28 2018 - [error][/usr/share/perl5/vendor_perl/MHA/MasterMonitor.pm, ln427] Error happened on checking configurations. at /usr/bin/masterha_check_repl line 48.
Fri May 4 11:09:28 2018 - [error][/usr/share/perl5/vendor_perl/MHA/MasterMonitor.pm, ln525] Error happened on monitoring servers.
Fri May 4 11:09:28 2018 - [info] Got exit code 1 (Not master dead).
原因是在5.5.56-MariaDB版本中虽然从节点设置了read_only选项,但是对于管理员权限的用户这点不生效,所以在MySQL5.6(Mariadb10.1)后新增了super_read_only选项,但当前版本中没有这个选项,所以报错。解决办法是最后将MHA的版本换成mha4mysql-0.56