Mha-Atlas-MySQL高可用方案实践。

Posted L1n

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mha-Atlas-MySQL高可用方案实践。相关的知识,希望对你有一定的参考价值。

Mha-Atlas-mysql高可用方案实践(一)

Mha-Atlas-MySQL高可用方案实践

 

一,mysql-mha环境准备

 

1.1 实验环境:

技术分享图片

技术分享图片

技术分享图片

 

1.2 软件包

 

用到得所有包

链接:https://pan.baidu.com/s/19tiKXNEW4C6oWi9OFmcDYA 
提取码:be07

 

1) mha管理节点安装包:

mha4mysql-manager-0.56-0.el6.noarch.rpm 
mha4mysql-manager-0.56.tar.gz

 

2) mha node数据节点安装包:

mha4mysql-node-0.56-0.el6.noarch.rpm 
mha4mysql-node-0.56.tar.gz

 

3) mysql中间件:

Atlas-2.2.1.el6.x86_64.rpm

 

4) mysql源码安装包

mysql-5.6.17-linux-glibc2.5-x86_64.tar

 

1.3 主机名映射

echo "192.168.200.69 sl-Mysql-Mater" >> /etc/hosts

echo "192.168.200.79 sl-Mysql-Slave1" >> /etc/hosts

echo "192.168.200.80 sl-Mysql-Slave2" >> /etc/hosts

scp /etc/hosts [email protected]:/etc/

scp /etc/hosts [email protected]:/etc/

技术分享图片

 

1.4 关闭selinux和iptables(三台都需要关闭)

 
  1. [[email protected]-db01 ~]# vim /etc/sysconfig/selinux
  2. [[email protected]-db01 ~]# cat /etc/sysconfig/selinux | grep -v "#"
  3. SELINUX=disabled
  4. SELINUXTYPE=targeted
  5. [[email protected]-db01 ~]# setenforce 0
  6. [[email protected]-db01 ~]# service iptables stop
  7. iptables: Setting chains to policy ACCEPT: filter [ OK ]
  8. iptables: Flushing firewall rules: [ OK ]
  9. iptables: Unloading modules: [ OK ]
  10. [[email protected]-db01 ~]# chkconfig iptables off

技术分享图片

 

二,简介

 

2.1 作者简介

技术分享图片

姓名:松信嘉范 
MySQL/Linux专家 
2001年索尼公司入职 
2001年开始使用oracle 
2004年开始使用MySQL 
2006年9月-2010年8月MySQL从事顾问 
2010年-2012年DeNA 
2012年至今Facebook

 

2.2 软件简介

  • MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,是一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件。在MySQL故障切换过程中,MHA能做到0~30秒之内自动完成数据库的故障切换操作,并且在进行故障切换过程中,MHA能最大程度上保证数据库的一致性,以达到真正意义上的高可用。
  • MHA由两部分组成:MHA Manager(管理节点)和MHA Node(数据节点)。MHA Manager可以独立部署在一台独立的机器上管理多个Master-Slave集群,也可以部署在一台Slave上。当Master出现故障时,它可以自动将最新数据的Slave提升为新的Master,然后将所有其他的Slave重新指向新的Master。整个故障转移过程对应程序是完全透明的。
 

2.3 工作流程

  • 从宕机崩溃的master保存二进制日志事件(binlog events);
  • 识别含有最新更新的slave;
  • 应用差异的中继日志(relay log)到其他的slave;
  • 应用从master保存的二进制日志事件(binlog events);
  • 提升一个slave为新的master;
  • 使其他的slave连接新的master进行复制;

1,复制主库binlog日志出来(因为还有没来的及复制到从库的二进制日志呢) 
2,找出relaylog日志最全的从库 (每个从库复制的速度是有差异的) 
3,将最全的relaylog日志在所有从库中同步(第一次数据同步) 
4,将之前最全的那个从库提升为主库 
5,将复制出来的binlog日志,放到新提升的主库里 
6,其他所有从库重新指向新提升的主库,继续主从复制。

 

 

 

2.5 MHA工具介绍

MHA软件由两部分组成,Manager工具包和Node工具包,具体的说明如下

 
  1. #Manager工具包主要包括以下几个工具
  2. masterha_check_ssh #检查MHA的SSH配置状况
  3. masterha_check_repl #检查MySQL复制状况
  4. masterha_check_status #检测当前MHA运行状态
  5. masterha_master_monitor #检测master是否宕机
  6. masterha_manger #启动MHA
  7. masterha_master_switch #控制故障转移(自动或者手动)
  8. masterha_conf_host #添加或删除配置的server信息
  9. masterha_secondary_check #试图建立TCP连接从远程服务器
  10. masterha_stop #停止MHA
  11. #Node工具包主要包括以下几个工具
  12. save_binary_logs #保存和复制master的二进制日志
  13. apply_diff_relay_logs #识别差异的中继日志事件
  14. filter_mysqlbinlog #去除不必要的ROLLBACK事件
  15. purge_relay_logs #清除中继日志
 

三,mysql环境准备

 

3.1 环境检查

技术分享图片

 

3.2 安装mysql

 

3.2.1 安装包准备

上方有所有软件包得下载链接

 

3.2.2 安装(3台都装)

yum -y install ncurses-devel libaio

tar xf mysql-5.6.17-linux-glibc2.5-x86_64.tar.gz -C /usr/local/

ln -s /usr/local/mysql-5.6.17-linux-glibc2.5-x86_64 /usr/local/mysql

useradd mysql -s /sbin/nologin -M

/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/

/bin/cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf

/bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

ln -s /usr/local/mysql/bin/* /usr/local/bin/

which mysqladmin

技术分享图片


技术分享图片


技术分享图片

 

3.2.3 加入开机自启动并启动mysql

chkconfig mysqld on

chkconfig mysqld --list

/etc/init.d/mysqld start

技术分享图片

 

3.2.4 配置密码

mysqladmin -uroot password ‘linyaonie‘

技术分享图片

 

四,配置基于GTID的主从复制

 

4.1 先决条件

  • 主库和从库都要开启binlog
  • 主库和从库server-id不同
  • 要有主从复制用户
 

4.2 主库操作(mysql-db01)

 

4.2.1 修改配置文件

 
  1. #修改主库配置文件/etc/my.cnf
  2. [[email protected]-db01 mysql]# cat /etc/my.cnf
  3. [client]
  4. socket = /usr/local/mysql/data/mysql.sock
  5. [mysqld]
  6. lower_case_table_names = 1
  7. default-storage-engine = InnoDB
  8. port = 3306
  9. datadir = /usr/local/mysql/data
  10. character-set-server = utf8
  11. socket = /usr/local/mysql/data/mysql.sock
  12. log_bin = mysql-bin #开启binlog日志
  13. server_id = 1 #设置server_id
  14. innodb_buffer_pool_size = 200M
  15. slave-parallel-workers = 8
  16. thread_cache_size = 600
  17. back_log = 600
  18. slave_net_timeout = 60
  19. max_binlog_size = 512M
  20. key_buffer_size = 8M
  21. query_cache_size = 64M
  22. join_buffer_size = 2M
  23. sort_buffer_size = 2M
  24. query_cache_type = 1
  25. thread_stack = 192K
  26. #重启动MySQL服务
  27. [[email protected]-db01 mysql]# /etc/init.d/mysqld restart
  28. Shutting down MySQL.. SUCCESS!
  29. Starting MySQL. SUCCESS!
 

4.2.2 登陆MySQL删除不必要的用户并创建主从复制用户

1)删除不必要的用户

 
  1. mysql> select user,host from mysql.user;
  2. +------+------------+
  3. | user | host |
  4. +------+------------+
  5. | root | 127.0.0.1 |
  6. | root | ::1 |
  7. | | localhost |
  8. | root | localhost |
  9. | | mysql-db01 |
  10. | root | mysql-db01 |
  11. +------+------------+
  12. 6 rows in set (0.00 sec)
  13. mysql> drop user [email protected]‘127.0.0.1‘;
  14. Query OK, 0 rows affected (0.00 sec)
  15. mysql> drop user [email protected]‘::1‘;
  16. Query OK, 0 rows affected (0.00 sec)
  17. mysql> drop user ‘ ‘@‘localhost‘;
  18. Query OK, 0 rows affected (0.00 sec)
  19. mysql> drop user ‘ ‘@‘mysql-db01‘;
  20. Query OK, 0 rows affected (0.00 sec)
  21. mysql> select user,host from mysql.user;
  22. +------+------------+
  23. | user | host |
  24. +------+------------+
  25. | root | localhost |
  26. | root | mysql-db01 |
  27. +------+------------+
  28. 2 rows in set (0.00 sec)

2)创建主从复制用户

 
  1. mysql> grant replication slave on *.* to [email protected]‘192.168.0.%‘ identified by ‘123123‘;
  2. Query OK, 0 rows affected (0.00 sec)
  3. mysql> select user,host from mysql.user;
  4. +------+-------------+
  5. | user | host |
  6. +------+-------------+
  7. | rep | 192.168.0.% |
  8. | root | localhost |
  9. | root | mysql-db01 |
  10. +------+-------------+
  11. 3 rows in set (0.00 sec)
  12. mysql> show grants for [email protected]‘192.168.0.%‘;
  13. +--------------------------------------------------------------------------------------------------------------------------+
  14. | Grants for [email protected].168.0.% |
  15. +--------------------------------------------------------------------------------------------------------------------------+
  16. | GRANT REPLICATION SLAVE ON *.* TO ‘rep‘@‘192.168.0.%‘ IDENTIFIED BY PASSWORD ‘*E56A114692FE0DE073F9A1DD68A00EEB9703F3F1‘ |
  17. +--------------------------------------------------------------------------------------------------------------------------+
  18. 1 row in set (0.00 sec)
 

4.3 从库操作(mysql-db02和mysql-db03)

 

4.3.1 修改配置文件

 
  1. #修改mysql-db02配置文件(和mysql-db01配置文件一致)
  2. #只需要修改server-id = 5选项
  3. [[email protected]-db02 ~]# cat /etc/my.cnf
  4. [client]
  5. socket = /usr/local/mysql/data/mysql.sock
  6. [mysqld]
  7. lower_case_table_names = 1
  8. default-storage-engine = InnoDB
  9. port = 3306
  10. datadir = /usr/local/mysql/data
  11. character-set-server = utf8
  12. socket = /usr/local/mysql/data/mysql.sock
  13. log_bin = mysql-bin #从binlog也要打开
  14. server_id = 5 #仅需修改此项
  15. innodb_buffer_pool_size = 200M
  16. slave-parallel-workers = 8
  17. thread_cache_size = 600
  18. back_log = 600
  19. slave_net_timeout = 60
  20. max_binlog_size = 512M
  21. key_buffer_size = 8M
  22. query_cache_size = 64M
  23. join_buffer_size = 2M
  24. sort_buffer_size = 2M
  25. query_cache_type = 1
  26. thread_stack = 192K
  27. [[email protected]-db02 ~]# /etc/init.d/mysqld restart #重启mysql
  28. Shutting down MySQL.. SUCCESS!
  29. Starting MySQL. SUCCESS!
  30. #修改mysql-db03配置文件(和mysql-db01配置文件一致)
  31. #只需要修改server-id = 10选项
  32. [[email protected]-db03 ~]# cat /etc/my.cnf
  33. [client]
  34. socket = /usr/local/mysql/data/mysql.sock
  35. [mysqld]
  36. lower_case_table_names = 1
  37. default-storage-engine = InnoDB
  38. port = 3306
  39. datadir = /usr/local/mysql/data
  40. character-set-server = utf8
  41. socket = /usr/local/mysql/data/mysql.sock
  42. log_bin = mysql-bin #从binlog也要打开
  43. server_id = 10 #只需修改此项
  44. innodb_buffer_pool_size = 200M
  45. slave-parallel-workers = 8
  46. thread_cache_size = 600
  47. back_log = 600
  48. slave_net_timeout = 60
  49. max_binlog_size = 512M
  50. key_buffer_size = 8M
  51. query_cache_size = 64M
  52. join_buffer_size = 2M
  53. sort_buffer_size = 2M
  54. query_cache_type = 1
  55. thread_stack = 192K
  56. [[email protected]-db03 ~]# /etc/init.d/mysqld restart #重启mysql
  57. Shutting down MySQL.. SUCCESS!
  58. Starting MySQL. SUCCESS!

特别提示: 
在以往如果是基于binlog日志的主从复制,则必须要记住主库的master状态信息。 
技术分享图片
但是在MySQL5.6版本里多了一个Gtid的功能,可以自动记录主从复制位置点的信息,并在日志中输出出来。

 

4.4 开启GTID

 
  1. #没开启之前先看一下GTID状态
  2. mysql> show global variables like ‘%gtid%‘;
  3. +--------------------------+-------+
  4. | Variable_name | Value |
  5. +--------------------------+-------+
  6. | enforce_gtid_consistency | OFF |
  7. | gtid_executed | |
  8. | gtid_mode | OFF |
  9. | gtid_owned | |
  10. | gtid_purged | |
  11. +--------------------------+-------+
  12. 5 rows in set (0.00 sec)

编辑mysql配置文件(主库从库都需要修改)

技术分享图片

mysql-db01,mysql-db02,mysql-db03都需要加入上图的上行代码

修改完配置文件以后重启动数据库

 
  1. [[email protected]-db01 ~]# /etc/init.d/mysqld restart
  2. Shutting down MySQL.. SUCCESS!
  3. Starting MySQL. SUCCESS!
  4. [[email protected]-db02 ~]# /etc/init.d/mysqld restart
  5. Shutting down MySQL.. SUCCESS!
  6. Starting MySQL. SUCCESS!
  7. [[email protected]-db03 ~]# /etc/init.d/mysqld restart
  8. Shutting down MySQL.. SUCCESS!
  9. Starting MySQL. SUCCESS!

再次查看GTID状态

 
  1. [[email protected]-db01 ~]# mysql -uroot -p123123
  2. Warning: Using a password on the command line interface can be insecure.
  3. Welcome to the MySQL monitor. Commands end with ; or g.
  4. Your MySQL connection id is 1
  5. Server version: 5.6.17-log MySQL Community Server (GPL)
  6. Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type ‘help;‘ or ‘h‘ for help. Type ‘c‘ to clear the current input statement.
  11. mysql> show global variables like ‘%gtid%‘;
  12. +--------------------------+-------+
  13. | Variable_name | Value |
  14. +--------------------------+-------+
  15. | enforce_gtid_consistency | ON | #执行GTID一致
  16. | gtid_executed | |
  17. | gtid_mode | ON | #开启GTID模块
  18. | gtid_owned | |
  19. | gtid_purged | |
  20. +--------------------------+-------+
  21. 5 rows in set (0.00 sec)
  22. mysql>

再次提示: 
主库从库都必须要开启GTID,否则在做主从复制的时候就会报错

 

4.5 配置主从复制(mysql-db02,mysql-db03)

 
  1. mysql> change master to
  2. -> master_host=‘192.168.0.51‘, #主库IP
  3. -> master_user=‘rep‘, #主库复制用户
  4. -> master_password=‘123123‘, #主库复制用密码
  5. -> master_auto_position=1; #GTID位置点(自动追踪需要同步的position)
  6. Query OK, 0 rows affected, 2 warnings (0.00 sec)
 

4.6 开启从库的主从复制功能(mysql-db02,mysql-db03)

 
  1. mysql> start slave; #开启主从同步功能
  2. Query OK, 0 rows affected, 1 warning (0.01 sec)
  3. mysql> show slave statusG
  4. *************************** 1. row ***************************
  5. Slave_IO_State: Waiting for master to send event
  6. Master_Host: 192.168.0.51
  7. Master_User: rep
  8. Master_Port: 3306
  9. Connect_Retry: 60
  10. Master_Log_File: mysql-bin.000003
  11. Read_Master_Log_Pos: 151
  12. Relay_Log_File: mysql-db02-relay-bin.000002
  13. Relay_Log_Pos: 361
  14. Relay_Master_Log_File: mysql-bin.000003
  15. Slave_IO_Running: Yes #此项yes代表成功
  16. Slave_SQL_Running: Yes #此项yes代表成功
  17. Replicate_Do_DB:
  18. Replicate_Ignore_DB:
  19. Replicate_Do_Table:
  20. Replicate_Ignore_Table:
  21. #....以下省略若干行....

两个从库mysql-db02和mysql-db03都执行以上步骤。

 

4.7 什么是GTID

  • GTID(Global Transaction)全局事务标识符:是一个唯一的标识符,它创建并与源服务器(主)上提交的每个事务相关联。此标识符不仅对其发起的服务器是唯一的,而且在给定复制设置中的所有服务器上都是唯一的。所有交易和所有GTID之间都有1对1的映射。
  • GTID实际上是由UUID+TID组成的。其中UUID是一个MySQL实例的唯一标识。TID代表了该实例上已经提交的事务数量,并且随着事务提交单调递增。
  • 下面是一个GTID的具体形式: 
    3E11FA47-71CA-11E1-9E33-C80AA9429562:23
 

4.8 GTID的新特性

(1)支持多线程复制:事实上是针对每个database开启相应的独立线程,即每个库有一个单独的(sql thread)

(2)支持启用GTID,在配置主从复制,传统的方式里,你需要找到binlog和POS点,然后change master to 指向。在mysql5.6里,无须再知道binlog和POS点,只需要知道master的IP/端口/账号密码即可,因为同步复制是自动的,MySQL通过内部机制GTID自动找点同步。

(3)基于Row复制只保存改变的列,大大节省磁盘空间,网络,内存等

(4)支持把Master和Slave的相关信息记录在Table中;原来是记录在文件里,现在则记录在表里,增强可用性

(5)支持延迟复制

 

4.9 开启方法

 
  1. #mysql配置文件:
  2. [mysqld]
  3. gtid_mode=ON
  4. enforce_gtid_consistency
  5. #查看
  6. show global variables like ‘%gtid%’;
 

4.10 从库设置(mysql-db02,mysql-db03)

 
  1. #登陆从库
  2. [[email protected]-db02 ~]# mysql -uroot -p123123
  3. #临时禁用自动删除relay log功能
  4. mysql> set global relay_log_purge = 0;
  5. Query OK, 0 rows affected (0.00 sec)
  6. #设置只读
  7. mysql> set global read_only=1;
  8. Query OK, 0 rows affected (0.00 sec)

编辑配置文件/etc/my.cnf 
技术分享图片

修改完配置文件,别忘了重启动mysql服务

 
  1. [email protected]-db02 ~]# /etc/init.d/mysqld restart
  2. Shutting down MySQL.. SUCCESS!
  3. Starting MySQL. SUCCESS!
  4. [email protected]-db03 ~]# /etc/init.d/mysqld restart
  5. Shutting down MySQL.. SUCCESS!
  6. Starting MySQL. SUCCESS!
 

五,部署MHA

 

5.1 环境准备(所有节点mysql-db01,mysql-db02,mysql-db03)

 
  1. #光盘安装依赖包
  2. [[email protected]-db01 ~]# yum -y install perl-DBD-MySQL
  3. #安装mha4mysql-node-0.56-0.el6.noarch.rpm
  4. [[email protected]-db01 rpm]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
  5. Preparing... ########################################### [100%]
  6. 1:mha4mysql-node ########################################### [100%]
  7. [[email protected]-db01 ~]# mysql -uroot -p123123
  8. mysql> grant all privileges on *.* to [email protected]‘192.168.0.%‘ identified by ‘123123‘;
  9. Query OK, 0 rows affected (0.00 sec)
  10. mysql> select user,host from mysql.user where user=‘mha‘;
  11. +------+-------------+
  12. | user | host |
  13. +------+-------------+
  14. | mha | 192.168.0.% | #主库上创建从库会自动复制
  15. +------+-------------+
  16. 1 row in set (0.00 sec)
  17. #特别提示:3台MySQL都需要安装mha4mysql-node-0.56-0.el6.noarch.rpm
 

5.2 部署管理节点(mha-manager)

 

5.2.1 在mysql-db03上部署管理节点

 
  1. #使用阿里云源+epel源
  2. wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
  3. wget -O /etc/yum.repos.d/epel-6.repo http://mirrors.aliyun.com/repo/epel-6.repo
  4. #安装manager依赖包(需要公网源)
  5. [[email protected]-db03 ~]# yum -y install perl-Config-Tiny epel-release perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes
  6. #安装manager包
  7. [[email protected]-db03 rpm]# rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm
  8. Preparing... ########################################### [100%]
  9. 1:mha4mysql-manager ########################################### [100%]
 

5.2.2 编辑配置文件

 
  1. #创建配置文件目录
  2. [[email protected]-db03 ~]# mkdir -p /etc/mha
  3. #创建日志目录
  4. [[email protected]-db03 ~]# mkdir -p /var/log/mha/mha1
  5. #创建配置文件(默认没有)
  6. [[email protected]-db03 ~]# cd /etc/mha/
  7. [[email protected]-db03 mha]# ls
  8. [[email protected]-db03 mha]# vim /etc/mha/mha1.cnf
  9. [[email protected]-db03 mha]# cat /etc/mha/mha1.cnf
  10. [server default]
  11. manager_log=/var/log/mha/mha1/manager #manager管理日志存放路径
  12. manager_workdir=/var/log/mha/mha1 #manager管理日志的目录路径
  13. master_binlog_dir=/usr/local/mysql/data #binlog日志的存放路径
  14. user=mha #管理账户
  15. password=123123 #管理账户密码
  16. ping_interval=2 #存活检查的间隔时间
  17. repl_user=rep #主从复制的授权账户
  18. repl_password=123123 #主从复制的授权账户密码
  19. ssh_user=root #用于ssh连接的账户
  20. [server1]
  21. hostname=192.168.0.51
  22. port=3306
  23. [server2]
  24. #candidate_master=1 #此条暂时注释掉(后面解释)
  25. #check_repl_delay=0 #此条暂时注释掉(后面解释)
  26. hostname=192.168.0.52
  27. port=3306
  28. [server3]
  29. hostname=192.168.0.53
  30. port=3306
  31. #**特别提示:**
  32. #以上配置文件内容里每行的最后不要留有空格,因此,不能复制的呦

特别说明: 
参数:candidate_master=1 
解释:设置为候选master,如果设置该参数以后,发生主从切换以后会将此从库提升为主库,即使这个主库不是集群中事件最新的slave 
参数:check_repl_delay=0 
解释:默认情况下如果一个slave落后master 100M的relay logs 的话,MHA将不会选择该slave作为一个新的master,因为对于这个slave的恢复需要花费很长时间,通过设置check_repl_delay=0,MHA触发切换在选择一个新的master的时候将会忽略复制延时,这个参数对于设置了candidate_master=1的主机非常有用,因为这个候选主在切换的过程中一定是新的master

 

5.3 配置ssh信任(所有节点mysql-db01,mysql-db02,mysql-db03)

 
  1. #创建密钥对
  2. [[email protected]-db03 ~]# ssh-keygen -t dsa -P "" -f ~/.ssh/id_dsa >/dev/null 2>&1
  3. #发送mysql-db03公钥,包括自己
  4. [[email protected]-db03 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub [email protected].168.0.51
  5. [[email protected]-db03 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub [email protected].168.0.52
  6. [[email protected]-db03 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub [email protected].168.0.53
  7. #发送mysql-db02公钥,包括自己
  8. [[email protected]-db02 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub [email protected].168.0.51
  9. [[email protected]-db02 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub [email protected].168.0.52
  10. [[email protected]-db02 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub [email protected].168.0.53
  11. #发送mysql-db01公钥,包括自己
  12. [[email protected]-db01 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub [email protected].168.0.51
  13. [[email protected]-db01 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub [email protected].168.0.52
  14. [[email protected]-db01 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub [email protected].168.0.53
 

5.4 启动测试

 

5.4.1 ssh检查检测

 
  1. [[email protected]-db03 ~]# masterha_check_ssh --conf=/etc/mha/mha1.cnf #ssh检查命令
  2. Tue Sep 5 03:01:38 2017 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
  3. Tue Sep 5 03:01:38 2017 - [info] Reading application default configuration from /etc/mha/mha1.cnf..
  4. Tue Sep 5 03:01:38 2017 - [info] Reading server configuration from /etc/mha/mha1.cnf..
  5. Tue Sep 5 03:01:38 2017 - [info] Starting SSH connection tests..
  6. ..中间省略若干行..
  7. Tue Sep 5 03:01:40 2017 - [debug] Connecting via SSH from [email protected].168.0.53(192.168.0.53:22) to [email protected].168.0.52(192.168.0.52:22)..
  8. Tue Sep 5 03:01:40 2017 - [debug] ok.
  9. Tue Sep 5 03:01:40 2017 - [info] All SSH connection tests passed successfully. #出现这个就表示成功
 

5.4.2 主从复制检测

(1)错误的主从复制检测 
[[email protected] ~]# masterha_check_repl --conf=/etc/mha/mha1.cnf

如果不出意外,检测结果会是下面的样子 
技术分享图片

因此在mysql-db02和mysql-db03上添加主从复制的用户即可。 
grant replication slave on *.* to [email protected]‘192.168.0.%‘ identified by ‘123123‘; 
再次检查如下图所示: 
技术分享图片
技术分享图片

 

5.5 启动MHA

 
  1. #启动
  2. [[email protected]-db03 ~]# nohup masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/mha1/manager.log 2>&1 &
  3. [[email protected]-db03 ~]# ps -ef | grep perl | grep -v grep
  4. root 4961 4690 0 06:33 pts/2 00:00:00 perl /usr/bin/masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover
  5. #说明:
  6. nohup:启动命令
  7. --conf:指定配置文件位置
  8. --remove_dead_master_conf:如果有master down了,就去掉配置文件里该master的部分。
 

5.6 进行mha自动切换master的测试

初始状态:

技术分享图片

(1)登陆mysql-db02(192.168.0.53)查看信息状态

 
  1. #登陆数据库mysql-db02(192.168.0.53)
  2. [[email protected]-db03 ~]# mysql -uroot -p123123
  3. mysql> show slave statusG
  4. *************************** 1. row ***************************
  5. Slave_IO_State: Waiting for master to send event
  6. Master_Host: 192.168.0.51 #这是主库IP地址
  7. Master_User: rep
  8. Master_Port: 3306
  9. Connect_Retry: 60
  10. Master_Log_File: mysql-bin.000003
  11. Read_Master_Log_Pos: 1656
  12. Relay_Log_File: mysql-db02-relay-bin.000004
  13. Relay_Log_Pos: 1796
  14. Relay_Master_Log_File: mysql-bin.000003
  15. Slave_IO_Running: Yes
  16. Slave_SQL_Running: Yes
  17. ..以下省略若干内容..

(2)停掉mysql-db01(192.168.0.51)上的MySQL服务

 
  1. [[email protected]-db01 ~]# /etc/init.d/mysqld stop
  2. Shutting down MySQL..... SUCCESS!

(3)查看mysql-db03上的MySQL从库同步状态

 
  1. [[email protected]-db03 ~]# mysql -uroot -p123123 -e ‘show slave statusG‘
  2. Warning: Using a password on the command line interface can be insecure.
  3. *************************** 1. row ***************************
  4. Slave_IO_State: Waiting for master to send event
  5. Master_Host: 192.168.0.52 #现在的主库IP
  6. Master_User: rep
  7. Master_Port: 3306
  8. Connect_Retry: 60
  9. Master_Log_File: mysql-bin.000006 #binlog日志
  10. Read_Master_Log_Pos: 777 #binlog日志位置
  11. Relay_Log_File: mysql-db03-relay-bin.000002
  12. Relay_Log_Pos: 408
  13. Relay_Master_Log_File: mysql-bin.000006
  14. Slave_IO_Running: Yes
  15. Slave_SQL_Running: Yes
  16. ..以下省略若干内容..

(4)查看mysql-db02上的MySQL,主库同步状态。 
技术分享图片

(5)查看mysql-db03上的mha进程状态

 
  1. [[email protected]-db03 ~]# ps -ef | grep perl | grep -v grep #查询发现mha进程已经没了
  2. [[email protected]-db03 ~]#

(6)查看mha配置文件信息 
技术分享图片

说明: 
当作为主库的mysql-db01上的MySQL宕机以后,mha通过检测发现mysql-db01宕机,那么会将binlog日志最全的从库立刻提升为主库,而其他的从库会指向新的主库进行再次同步。

此处需要进行简单的mha日志记录的讲解:/var/log/mha/mha1/manager

 

5.7 进行mha的故障还原测试

由于mysql-db01的MySQL服务宕机,因此mha将mysql-db02提升为了主库。因此,我们需要将宕机的mysql-db01的MySQL服务启动,然后作为主库mysql-db02的从库。

初始状态: 
技术分享图片 
(1)将故障宕机的mysql-db01的MySQL服务启动并授权进行从同步

 
  1. [[email protected]-db01 ~]# /etc/init.d/mysqld start
  2. Starting MySQL. SUCCESS!
  3. [[email protected]-db01 ~]# mysql -uroot -p123123
  4. mysql> CHANGE MASTER TO MASTER_HOST=‘192.168.0.52‘, MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER=‘rep‘, MASTER_PASSWORD=‘123123‘;
  5. mysql> start slave;
  6. mysql> show slave statusG #查看从同步状态

(2)将mha配置文件里缺失的部分补全

 
  1. [[email protected]-db03 ~]# cat /etc/mha/mha1.cnf
  2. [server default]
  3. manager_log=/var/log/mha/mha1/manager
  4. manager_workdir=/var/log/mha/mha1
  5. master_binlog_dir=/usr/local/mysql/data
  6. password=123123
  7. ping_interval=2
  8. repl_password=123123
  9. repl_user=rep
  10. ssh_user=root
  11. user=mha
  12. [server1]
  13. hostname=192.168.0.51
  14. port=3306
  15. [server2]
  16. hostname=12.168.0.52
  17. port=3306
  18. [server3]
  19. hostname=192.168.0.53
  20. port=3306

(3)启动mha进程

 
  1. [[email protected]-db03 ~]# nohup masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/mha1/manager.log 2>&1 &
  2. [[email protected]-db03 ~]# ps -ef | grep perl | grep -v grep
  3. root 5226 4690 0 09:42 pts/2 00:00:00 perl /usr/bin/masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover

(4)停掉mysql-db02上的MySQL服务

 
  1. [[email protected]-db02 ~]# /etc/init.d/mysqld stop
  2. Shutting down MySQL..... SUCCESS!

(5)查看mysql-db03上的主从同步状态:

 
  1. [[email protected]-db03 ~]# mysql -uroot -p123123 -e ‘show slave statusG‘
  2. Warning: Using a password on the command line interface can be insecure.
  3. *************************** 1. row ***************************
  4. Slave_IO_State: Waiting for master to send event
  5. Master_Host: 192.168.0.51 #此时的主库IP切换回了mysql-db01
  6. Master_User: rep
  7. Master_Port: 3306
  8. Connect_Retry: 60
  9. Master_Log_File: mysql-bin.000005
  10. Read_Master_Log_Pos: 231
  11. Relay_Log_File: mysql-db03-relay-bin.000002
  12. Relay_Log_Pos: 361
  13. Relay_Master_Log_File: mysql-bin.000005
  14. Slave_IO_Running: Yes
  15. Slave_SQL_Running: Yes
  16. ..以下省略若干行..

(6)启动mysql-db02上的MySQL服务

 
  1. [[email protected]-db02 ~]# /etc/init.d/mysqld start
  2. Starting MySQL. SUCCESS!
  3. [[email protected]-db02 ~]# mysql -uroot -p123123
  4. mysql> CHANGE MASTER TO MASTER_HOST=‘192.168.0.51‘, MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER=‘rep‘, MASTER_PASSWORD=‘123123‘;
  5. mysql> start slave;
  6. mysql> show slave statusG

(7)再次补全mha配置文件后,启动mha进程

 
  1. [[email protected]-db03 ~]# cat /etc/mha/mha1.cnf
  2. [server default]
  3. manager_log=/var/log/mha/mha1/manager
  4. manager_workdir=/var/log/mha/mha1
  5. master_binlog_dir=/usr/local/mysql/data
  6. password=123123
  7. ping_interval=2
  8. repl_password=123123
  9. repl_user=rep
  10. ssh_user=root
  11. user=mha
  12. [server1]
  13. hostname=192.168.0.51
  14. port=3306
  15. [server2]
  16. hostname=192.168.0.52
  17. port=3306
  18. [server3]
  19. #andidate_master=1
  20. #check_repl_delay=0
  21. hostname=192.168.0.53
  22. port=3306
  23. [[email protected]-db03 ~]# nohup masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/mha1/manager.log 2>&1 &
  24. [[email protected]-db03 ~]# ps -ef | grep perl | grep -v grep
  25. root 5226 4690 0 09:42 pts/2 00:00:01 perl /usr/bin/masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover

此时的初始状态还原为下图:

技术分享图片

 


mha配置文件内容如下: 
技术分享图片

 

附录:源码安装mha的方法

node节点的源码安装方法:

 
  1. [[email protected]-db01 ~]# yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Params-Validate perl-CPAN perl-devel perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker
  2. [[email protected]-db01 ~]# tar xf mha4mysql-node-0.56.tar.gz -C /usr/src/
  3. [[email protected]-db01 ~]# cd /usr/src/mha4mysql-node-0.56/
  4. [[email protected]-db01 mha4mysql-node-0.56]# perl Makefile.PL
  5. [[email protected]-db01 mha4mysql-node-0.56]# make && make install

manager节点的源码安装方法:

 
  1. [[email protected]-db01 ~]# yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Params-Validate perl-CPAN perl-devel perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker
  2. [[email protected]-db01 ~]# tar xf mha4mysql-manager-0.56.tar.gz -C /usr/src/
  3. [[email protected]-db01 ~]# cd /usr/src/mha4mysql-manager-0.56/
  4. [[email protected]-db01 mha4mysql-manager-0.56]# perl Makefile.PL
  5. [[email protected]-db01 mha4mysql-manager-0.56]# make && make install
+

 







































以上是关于Mha-Atlas-MySQL高可用方案实践。的主要内容,如果未能解决你的问题,请参考以下文章

Mha-Atlas-MySQL高可用方案实践

项目课---Mha-Atlas-MySQL高可用方案实践

(项目六)Mha-Atlas-MySQL高可用方案实践

Mha-Atlas-MySQL高可用

MHA-Atlas-MySQL高可用 上

Linux系统——MHA-Atlas-MySQL高可用集群