mysql高可用方案之MHA

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介:

环境规划:

节点说明    主机名   IP地址

管理节点   tong3   192.168.1.249

主节点     tong2   192.168.1.248

主节点    tong1   192.168.1.247



1.网络和主机名配置

设置每个主机的IP地址和/etc/hosts文件互相解析

[root@tong1 ~]# cat /etc/hosts
192.168.1.247 tong1
192.168.1.248 tong2
192.168.1.249 tong3
[root@tong1 ~]# ping tong1 -c1         --网络必须ping通
PING tong1 (192.168.1.247) 56(84) bytes of data.
64 bytes from localhost (192.168.1.247): icmp_seq=1 ttl=64 time=0.021 ms

--- tong1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.021/0.021/0.021/0.000 ms
[root@tong1 ~]# ping tong2 -c1
PING tong2 (192.168.1.248) 56(84) bytes of data.
64 bytes from tong2 (192.168.1.248): icmp_seq=1 ttl=64 time=0.109 ms

--- tong2 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.109/0.109/0.109/0.000 ms
[root@tong1 ~]# ping tong3 -c1
PING tong3 (192.168.1.249) 56(84) bytes of data.
64 bytes from tong3 (192.168.1.249): icmp_seq=1 ttl=64 time=0.124 ms

--- tong3 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.124/0.124/0.124/0.000 ms
[root@tong1 ~]# 


2.安装mha管理软件mha manager

tong3管理节点:

[root@tong3 ~]# yum install  perl-DBD-MySQL cpan  --安装perl工具

[root@tong3 ~]# tar xvf mha4mysql-manager-0.53.tar.gz  -C /usr/local/

[root@tong3 ~]# cd /usr/local/mha4mysql-manager-0.53/

[root@tong3 mha4mysql-manager-0.53]# perl Makefile.PL 

[root@tong3 mha4mysql-manager-0.53]# echo $?
0
[root@tong3 mha4mysql-manager-0.53]# make && make install

[root@tong3 mha4mysql-manager-0.53]# echo $?
0
[root@tong3 mha4mysql-manager-0.53]#


3.在各数据节点安装mha node

[root@tong2 ~]# tar xvf mha4mysql-node-0.53.tar.gz 
[root@tong2 ~]# cd mha4mysql-node-0.53
[root@tong2 mha4mysql-node-0.53]# yum install perl-DBD-mysql cpan -y

[root@tong2 mha4mysql-node-0.53]# perl Makefile.PL 
[root@tong2 mha4mysql-node-0.53]# make && make install

[root@tong2 mha4mysql-node-0.53]#  echo $?
0
[root@tong2 mha4mysql-node-0.53]# 


4.各节点ssh互相信任

[root@tong3 ~]# ssh-keygen  -t dsa

[root@tong3 ~]# cd .ssh

[root@tong3 .ssh]# cat id_dsa.pub  > authorized_keys

[root@tong3 .ssh]# scp 192.168.1.247:/root/.ssh/id_dsa.pub 247

[root@tong3 .ssh]# scp 192.168.1.248:/root/.ssh/id_dsa.pub 248

[root@tong3 .ssh]# cat 248  247 >> authorized_keys 
[root@tong3 .ssh]# scp authorized_keys 192.168.1.248:/root/.ssh/
authorized_keys                                                                                                                                    100% 1800     1.8KB/s   00:00    
[root@tong3 .ssh]# scp authorized_keys 192.168.1.247:/root/.ssh/
root@192.168.1.247's password: 
authorized_keys                                                                                                                                    100% 1800     1.8KB/s   00:00    
[root@tong3 .ssh]# ssh tong1 date
Tue Apr 28 12:57:02 CST 2015
[root@tong3 .ssh]# ssh tong2 date
Tue Apr 28 12:59:57 CST 2015
[root@tong3 .ssh]# ssh tong3 date
Tue Apr 28 12:57:25 CST 2015
[root@tong3 .ssh]# 


5.在管理节点编辑配置文件

[root@tong3 .ssh]# mkdir /etc/mysqlmha
[root@tong3 .ssh]# cd /etc/mysqlmha/
[root@tong3 mysqlmha]# cp -a /usr/local/mha4mysql-manager-0.53/samples/* .
[root@tong3 mysqlmha]# vim conf/app1.cnf 
[server default]
manager_workdir=/var/log/masterha/app1
manager_log=/var/log/masterha/app1/manager.log
user=root1                --远程登陆用户
password=system
ssh_user=root
repl_user=repl_user       --复制用户
repl_password=system!#%246
ping_interval=1       --心跳检测

[server1]
hostname=192.168.1.249
master_binlog_dir="/usr/local/mysql-5.6.23/data/"
# candidate_master=1
no_master=1          --不能切换成主数据库

[server2]
hostname=192.168.1.248
master_binlog_dir="/usr/local/mysql-5.6.23/data/"     --二进制日志文件存放
candidate_master=1     --可以切换成主数据库

[server4]
hostname=192.168.1.247
master_binlog_dir="/usr/local/mysql-5.6.23/data/"
candidate_master=1    -可以切换成主数据库

[root@tong3 ~]# masterha_check_ssh --conf=/etc/mysqlmha/conf/app1.cnf 
Tue Apr 28 15:39:21 2015 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Apr 28 15:39:21 2015 - [info] Reading application default configurations from /etc/mysqlmha/conf/app1.cnf..
Tue Apr 28 15:39:21 2015 - [info] Reading server configurations from /etc/mysqlmha/conf/app1.cnf..
Tue Apr 28 15:39:21 2015 - [info] Starting SSH connection tests..
Tue Apr 28 15:39:22 2015 - [debug] 
Tue Apr 28 15:39:21 2015 - [debug]  Connecting via SSH from root@192.168.1.249(192.168.1.249:22) to root@192.168.1.248(192.168.1.248:22)..
Tue Apr 28 15:39:22 2015 - [debug]   ok.
Tue Apr 28 15:39:22 2015 - [debug]  Connecting via SSH from root@192.168.1.249(192.168.1.249:22) to root@192.168.1.247(192.168.1.247:22)..
Tue Apr 28 15:39:22 2015 - [debug]   ok.
Tue Apr 28 15:39:23 2015 - [debug] 
Tue Apr 28 15:39:22 2015 - [debug]  Connecting via SSH from root@192.168.1.248(192.168.1.248:22) to root@192.168.1.249(192.168.1.249:22)..
Tue Apr 28 15:39:22 2015 - [debug]   ok.
Tue Apr 28 15:39:22 2015 - [debug]  Connecting via SSH from root@192.168.1.248(192.168.1.248:22) to root@192.168.1.247(192.168.1.247:22)..
Tue Apr 28 15:39:23 2015 - [debug]   ok.
Tue Apr 28 15:39:24 2015 - [debug] 
Tue Apr 28 15:39:22 2015 - [debug]  Connecting via SSH from root@192.168.1.247(192.168.1.247:22) to root@192.168.1.249(192.168.1.249:22)..
Tue Apr 28 15:39:23 2015 - [debug]   ok.
Tue Apr 28 15:39:23 2015 - [debug]  Connecting via SSH from root@192.168.1.247(192.168.1.247:22) to root@192.168.1.248(192.168.1.248:22)..
Tue Apr 28 15:39:23 2015 - [debug]   ok.
Tue Apr 28 15:39:24 2015 - [info] All SSH connection tests passed successfully.
[root@tong3 ~]# 


6.修改数据库配置文件和创建用户

在所有节点创建相同的用户:

mysql> grant all privileges on *.* to root1@'192.168.1.%' identified by 'system';

mysql> grant replication slave,replication client on *.* to repl_user@'192.168.1.%' identified by 'system!#%246';


tong1节点:

[root@tong1 ~]# vim /etc/my.cnf

basedir = /usr/local/mysql-5.6.23
datadir = /usr/local/mysql-5.6.23/data
port = 3306
server_id = 20
socket = /tmp/mysql.sock

replicate-do-db=tong
replicate-ignore-db=mysql

log-bin=mysql-bin
log-bin-index=mysql-bin-index

auto_increment_offset=1
auto_increment_increment=2
relay_log_purge=0
read-only=1

[root@tong1 ~]#


tong2节点:

[root@tong2 ~]# vim /etc/my.cnf

basedir = /usr/local/mysql-5.6.23
datadir = /usr/local/mysql-5.6.23/data
port = 3306
server_id = 10
socket = /tmp/mysql.sock

replicate-do-db=tong
replicate-ignore-db=mysql

log-bin=mysql-bin
log-bin-index=mysql-bin-index

auto_increment_offset=2
auto_increment_increment=2

read-only=1
relay_log_purge=0

[root@tong2 ~]#


tong3节点:

[root@tong3 ~]# vim /etc/my.cnf

basedir = /usr/local/mysql-5.6.23
datadir = /usr/local/mysql-5.6.23/data
port = 3306
server_id = 30
socket = /tmp/mysql.sock

replicate-do-db=tong
replicate-ignore-db=mysql

[root@tong3 ~]#


7.将tong1和tong2搭建为主主模式

tong1主机:

[root@tong1 .ssh]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 60
Server version: 5.6.23-log MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>  change master to master_host='192.168.1.248',master_port=3306,master_user='repl_user',master_password='system!#%246',master_log_file='mysql-bin.000010',master_log_pos=120;

Query OK, 0 rows affected, 2 warnings (0.45 sec)

mysql> start slave;
Query OK, 0 rows affected (0.05 sec)

mysql>


tong1主机:

[root@tong2 .ssh]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 60
Server version: 5.6.23-log MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>  change master to master_host='192.168.1.247',master_port=3306,master_user='repl_user',master_password='system!#%246',master_log_file='mysql-bin.000010',master_log_pos=120;

Query OK, 0 rows affected, 2 warnings (0.45 sec)

mysql> start slave;
Query OK, 0 rows affected (0.05 sec)

mysql>


8.检查复制是否有错

[root@tong3 ~]# masterha_check_repl --conf=/etc/mysqlmha/conf/app1.cnf

Tue Apr 28 15:53:23 2015 - [info] Checking replication health on 192.168.1.249..
Tue Apr 28 15:53:23 2015 - [info]  ok.
Tue Apr 28 15:53:23 2015 - [info] Checking replication health on 192.168.1.248..
Tue Apr 28 15:53:23 2015 - [info]  ok.
Tue Apr 28 15:53:23 2015 - [warning] master_ip_failover_script is not defined.
Tue Apr 28 15:53:23 2015 - [warning] shutdown_script is not defined.
Tue Apr 28 15:53:23 2015 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

[root@tong3 mysqlmha]# nohup masterha_manager --conf=/etc/mysqlmha/conf/app1.cnf  >> /tmp/mha_manager 2>&1 &

[root@tong3 mysqlmha]# jobs 
[1]+  Running                 nohup masterha_manager --conf=/etc/mysqlmha/conf/app1.cnf >> /tmp/mha_manager 2>&1 &
[root@tong3 mysqlmha]# masterha_check_status --conf=/etc/mysqlmha/conf/app1.cnf 
app1 (pid:24330) is running(0:PING_OK), master:192.168.1.247  --此时主节点在247服务器上
[root@tong3 mysqlmha]# 


9.故障测试

tong1节点:

[root@tong1 ~]# /etc/init.d/mysqld stop       --停掉tong1节点的数据库
Shutting down MySQL............ SUCCESS! 
[root@tong2 ~]# /etc/init.d/mysqld start   --节点必须启动才能做切换

Starting MySQL. SUCCESS! 
[root@tong2 ~]# 


查看tong2主机的日志状态,将tong1节点必须要同步到tong2节点上才可以切换(change master to master_host='192.168.1.248,master_ ....................)


tong3节点:

[root@tong3 mysqlmha]# rm -rf /var/log/masterha/app1/app1.failover.complete 
[1]+  Done                    nohup masterha_manager --conf=/etc/mysqlmha/conf/app1.cnf >> /tmp/mha_manager 2>&1          --必须删除管理节点的监控文件

[root@tong3 mysqlmha]# nohup masterha_manager --conf=/etc/mysqlmha/conf/app1.cnf  >> /tmp/mha_manager 2>&1 &

[root@tong3 ~]# masterha_check_status --conf=/etc/mysqlmha/conf/app1.cnf 
app1 (pid:27870) is running(0:PING_OK), master:192.168.1.248
[root@tong3 ~]# 











本文转自 z597011036 51CTO博客,原文链接:http://blog.51cto.com/tongcheng/1639693,如需转载请自行联系原作者
相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
3月前
|
存储 SQL 关系型数据库
Mysql高可用架构方案
本文阐述了Mysql高可用架构方案,介绍了 主从模式,MHA模式,MMM模式,MGR模式 方案的实现方式,没有哪个方案是完美的,开发人员在选择何种方案应用到项目中也没有标准答案,合适的才是最好的。
393 3
Mysql高可用架构方案
|
1月前
|
监控 关系型数据库 MySQL
Aurora MySQL负载突增应对策略与优化方案
通过以上策略,企业可以有效应对 Aurora MySQL 的负载突增,确保数据库在高负载情况下依然保持高性能和稳定性。这些优化方案涵盖了从架构设计到具体配置和监控的各个方面,能够全面提升数据库的响应速度和处理能力。在实际应用中,应根据具体的业务需求和负载特征,灵活调整和应用这些优化策略。
53 22
|
1月前
|
Java 关系型数据库 MySQL
MySQL 分库分表方案
本文总结了数据库分库分表的相关概念和实践,针对单张表数据量过大及增长迅速的问题,介绍了垂直和水平切分的方式及其适用场景。文章分析了分库分表后可能面临的事务支持、多库结果集合并、跨库join等问题,并列举了几种常见的开源分库分表中间件。最后强调了不建议水平分库分表的原因,帮助读者在规划时规避潜在问题。
184 20
|
2月前
|
存储 缓存 关系型数据库
【MySQL进阶篇】存储引擎(MySQL体系结构、InnoDB、MyISAM、Memory区别及特点、存储引擎的选择方案)
MySQL的存储引擎是其核心组件之一,负责数据的存储、索引和检索。不同的存储引擎具有不同的功能和特性,可以根据业务需求 选择合适的引擎。本文详细介绍了MySQL体系结构、InnoDB、MyISAM、Memory区别及特点、存储引擎的选择方案。
【MySQL进阶篇】存储引擎(MySQL体系结构、InnoDB、MyISAM、Memory区别及特点、存储引擎的选择方案)
|
2月前
|
SQL 关系型数据库 MySQL
数据库数据恢复—Mysql数据库表记录丢失的数据恢复方案
Mysql数据库故障: Mysql数据库表记录丢失。 Mysql数据库故障表现: 1、Mysql数据库表中无任何数据或只有部分数据。 2、客户端无法查询到完整的信息。
|
3月前
|
关系型数据库 MySQL
mysql 5.7.x版本查看某张表、库的大小 思路方案说明
mysql 5.7.x版本查看某张表、库的大小 思路方案说明
117 5
|
3月前
|
关系型数据库 MySQL
mysql 5.7.x版本查看某张表、库的大小 思路方案说明
mysql 5.7.x版本查看某张表、库的大小 思路方案说明
84 1
|
5月前
|
存储 SQL 关系型数据库
【MySQL调优】如何进行MySQL调优?从参数、数据建模、索引、SQL语句等方向,三万字详细解读MySQL的性能优化方案(2024版)
MySQL调优主要分为三个步骤:监控报警、排查慢SQL、MySQL调优。 排查慢SQL:开启慢查询日志 、找出最慢的几条SQL、分析查询计划 。 MySQL调优: 基础优化:缓存优化、硬件优化、参数优化、定期清理垃圾、使用合适的存储引擎、读写分离、分库分表; 表设计优化:数据类型优化、冷热数据分表等。 索引优化:考虑索引失效的11个场景、遵循索引设计原则、连接查询优化、排序优化、深分页查询优化、覆盖索引、索引下推、用普通索引等。 SQL优化。
885 15
【MySQL调优】如何进行MySQL调优?从参数、数据建模、索引、SQL语句等方向,三万字详细解读MySQL的性能优化方案(2024版)
|
5月前
|
存储 SQL 关系型数据库
一篇文章搞懂MySQL的分库分表,从拆分场景、目标评估、拆分方案、不停机迁移、一致性补偿等方面详细阐述MySQL数据库的分库分表方案
MySQL如何进行分库分表、数据迁移?从相关概念、使用场景、拆分方式、分表字段选择、数据一致性校验等角度阐述MySQL数据库的分库分表方案。
746 15
一篇文章搞懂MySQL的分库分表,从拆分场景、目标评估、拆分方案、不停机迁移、一致性补偿等方面详细阐述MySQL数据库的分库分表方案
|
4月前
|
SQL 关系型数据库 MySQL
mysql集群方案
mysql集群方案
71 0