mysql高可用方案之MHA

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介:

环境规划:

节点说明    主机名   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,如需转载请自行联系原作者
相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
1月前
|
关系型数据库 MySQL 数据库
|
19天前
|
运维 容灾 关系型数据库
MySQL高可用方案--Xenon全解
MySQL高可用方案--Xenon全解
|
1月前
|
存储 SQL 关系型数据库
(二十三)MySQL分表篇:该如何将月增上亿条数据的单表处理方案优雅落地?
前面《分库分表的正确姿势》、《分库分表的后患问题》两篇中,对数据库的分库分表技术进行了全面阐述,但前两篇大多属于方法论,并不存在具体的实战实操,而只有理论没有实践的技术永远都属纸上谈兵,所以接下来会再开几个单章对分库分表各类方案进行落地。
|
16天前
|
SQL 关系型数据库 MySQL
orchestrator搭建mysql高可用
orchestrator搭建mysql高可用
13 0
|
16天前
|
缓存 关系型数据库 MySQL
如何实现mysql高可用集群
如何实现mysql高可用集群
12 0
|
19天前
|
安全 关系型数据库 MySQL
【MySQL】Orchestrator最简单的 mysql 高可用方案最细细细细~
【MySQL】Orchestrator最简单的 mysql 高可用方案最细细细细~
|
2月前
|
关系型数据库 MySQL 数据库
|
3月前
|
运维 容灾 关系型数据库
介绍几种 MySQL 官方高可用方案
MySQL 官方提供了多种高可用部署方案,从最基础的主从复制到组复制再到 InnoDB Cluster 等等。本篇文章以 MySQL 8.0 版本为准,介绍下不同高可用方案架构原理及使用场景。
419 3
介绍几种 MySQL 官方高可用方案
|
1月前
|
缓存 关系型数据库 MySQL
MySQL调优秘籍曝光!从索引到事务,全方位解锁高可用秘诀,让你的数据库性能飞起来!
【8月更文挑战第6天】MySQL是顶级关系型数据库之一,其性能直接影响应用的高可用性与用户体验。本文聚焦MySQL的高性能调优,从索引设计到事务管理,逐一解析。介绍如何构建高效索引,如联合索引`CREATE INDEX idx_order_customer ON orders(order_id, customer_id);`,以及索引覆盖查询等技术。
65 0
|
2月前
|
存储 SQL 关系型数据库
Mysql多表数据需进行联动修改得方案
Mysql多表数据需进行联动修改得方案

热门文章

最新文章

下一篇
DDNS