快速启动一个MySQL MHA0.5.8实例(无废话)

Posted 上海一亩地

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了快速启动一个MySQL MHA0.5.8实例(无废话)相关的知识,希望对你有一定的参考价值。

准备4台ubuntu desktop 20.04LTS版本虚拟机

虚拟网络设备

虚拟网络类型:NAT
网段:10.0.0.0/24
网关:10.0.0.1
ip范围:10.0.0.5-10.0.0.254

硬件要求

每台虚拟机的硬件要求(一共需要4台,注意硬件是否足够):
CPU:2核或更高
RAM:4GB或更高
DISK:40GB或更高

Ubuntu要求

iso镜像:ubuntu-20.04.4-desktop-amd64.iso

主机1:
hostname:mysql-manager
ip:10.0.0.101

主机2:
hostname:mysql-master
ip:10.0.0.102

主机3:
hostname:mysql-slave1
ip:10.0.0.103

主机4:
hostname: mysql-slave2
ip:10.0.0.104

设置每台机器的root用户密码

sudo passwd root
password: 123456

关闭每台机器的防火墙

ufw disable

安装一些系统包:

su root
apt -y install ssh vim git cmake
systemctl start ssh && systemctl enable ssh

允许ssh通过密码登录root用户

# 允许ssh通过root登录
vim /etc/ssh/sshd_config
# 修改PermitRootLogin参数
PermitRootLogin yes

systemctl restart ssh

配置ssh的免密登录

首先在全部主机1-4上生成公钥

ssh-keygen -t rsa
#一路回车

然后配置免密登录。在每台主机上执行以下命令

ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.0.101
ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.0.102
ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.0.103
ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.0.104

这一步完成后,任意一台机器都能免密登录到其他机器,做到互通有无。

配置manager的域名解析

以下命令在主机1(10.0.0.101)上执行

echo '10.0.0.102 mysql-master' >> /etc/hosts
echo '10.0.0.103 mysql-slave1' >> /etc/hosts
echo '10.0.0.104 mysql-slave2' >> /etc/hosts

安装mysql主从复制

每台机器上编译安装mysql 5.7,方法见下面这篇博客:

安装mysql并设置mysql root密码

主机2-4,安装mysql。ip是10.0.0.102-104

apt -y install mysql*

# 修改mysql root 密码为123456
mysqladmin -uroot -p password '123456'
Enter password: # 这里输入旧密码,如果原来没有密码,则直接回车

安装MHA

下载和安装MHA manager和MHA node程序

以下操作在10.0.0.101-104上执行。

# 下载MHA manager
wget https://github.com/yoshinorim/mha4mysql-manager/releases/download/v0.58/mha4mysql-manager_0.58-0_all.deb

# 下载MHA node源码
wget https://github.com/yoshinorim/mha4mysql-node/releases/download/v0.58/mha4mysql-node_0.58-0_all.deb


在所有虚拟机上安装MHA node程序包

主机1-4都安装,以下操作在10.0.0.101-104上执行。

# 安装dbd包
apt-get -y install libdbd-mysql-perl
# 安装MHA node
dpkg -i mha4mysql-node_0.58-0_all.deb

在主机1上安装MHA manager

以下操作只在主机1上执行,ip是10.0.0.101

# 安装相关的Perl模块
apt-get -yinstall libdbd-mysql-perl
apt-get -y install libconfig-tiny-perl
apt-get -y install liblog-dispatch-perl
apt-get -y install libparallel-forkmanager-perl

# 安装MHA manager(manager依赖node程序,所以上一步安装node是全部虚拟机安装)
dpkg -i mha4mysql-manager_0.58-0_all.deb

编写配置文件

在主机1(10.0.0.101)上编辑一个配置文件

vim /etc/app1.cnf
---------------------------
[server default]
# mysql user and password
user=root
password=123456
# working directory on the manager
manager_workdir=/var/log/masterha/app1
# manager log file
manager_log=/var/log/masterha/app1/app1.log
# working directory on MySQL servers
remote_workdir=/var/log/masterha/app1

[server1]
hostname=mysql-master

[server2]
hostname=mysql-slave1

[server3]
hostname=mysql-slave2

编写全局配置文件

vim /etc/masterha_default.cnf
--------------------------------------
[server default]
user=root
password=123456 # ssh root password
ssh_user=root
master_binlog_dir= /var/lib/mysql
remote_workdir=/data/log/masterha
secondary_check_script= masterha_secondary_check -s remote_host1 -s remote_host2
ping_interval=3
master_ip_failover_script=/script/masterha/master_ip_failover
shutdown_script= /script/masterha/power_manager
report_script= /script/masterha/send_master_failover_mail

测试manager向各个mysql的连通性

masterha_check_ssh --conf=/etc/app1.cnf
Wed May 11 20:42:24 2022 - [info] Reading default configuration from /etc/masterha_default.cnf..
Wed May 11 20:42:24 2022 - [info] Reading application default configuration from /etc/app1.cnf..
Wed May 11 20:42:24 2022 - [info] Reading server configuration from /etc/app1.cnf..
Wed May 11 20:42:24 2022 - [info] Starting SSH connection tests..
Wed May 11 20:42:25 2022 - [debug] 
Wed May 11 20:42:24 2022 - [debug]  Connecting via SSH from root@mysql-master(10.0.0.102:22) to root@mysql-slave1(10.0.0.103:22)..
Wed May 11 20:42:25 2022 - [debug]   ok.
Wed May 11 20:42:25 2022 - [debug]  Connecting via SSH from root@mysql-master(10.0.0.102:22) to root@mysql-slave2(10.0.0.104:22)..
Wed May 11 20:42:25 2022 - [debug]   ok.
Wed May 11 20:42:26 2022 - [debug] 
Wed May 11 20:42:25 2022 - [debug]  Connecting via SSH from root@mysql-slave1(10.0.0.103:22) to root@mysql-master(10.0.0.102:22)..
Wed May 11 20:42:25 2022 - [debug]   ok.
Wed May 11 20:42:25 2022 - [debug]  Connecting via SSH from root@mysql-slave1(10.0.0.103:22) to root@mysql-slave2(10.0.0.104:22)..
Wed May 11 20:42:26 2022 - [debug]   ok.
Wed May 11 20:42:27 2022 - [debug] 
Wed May 11 20:42:25 2022 - [debug]  Connecting via SSH from root@mysql-slave2(10.0.0.104:22) to root@mysql-master(10.0.0.102:22)..
Wed May 11 20:42:26 2022 - [debug]   ok.
Wed May 11 20:42:26 2022 - [debug]  Connecting via SSH from root@mysql-slave2(10.0.0.104:22) to root@mysql-slave1(10.0.0.103:22)..
Wed May 11 20:42:26 2022 - [debug]   ok.
Wed May 11 20:42:27 2022 - [info] All SSH connection tests passed successfully.
Use of uninitialized value in exit at /usr/bin/masterha_check_ssh line 44.

启动MHA

在主机1(10.0.0.101)上启动manager

# 创建日志文件
mkdir -p /var/log/masterha/app1/
touch /var/log/masterha/app1/app1.log

# 启动
masterha_manager --conf=/etc/app1.cnf

查看manager的状态

masterha_check_status --conf=/etc/app1.cnf

以上是关于快速启动一个MySQL MHA0.5.8实例(无废话)的主要内容,如果未能解决你的问题,请参考以下文章

快速笔记01-MySQL多实例的安装与配置

MySQL单机多实例配置实战

一份详细的WordPress安装使用教程,快速搭建一个属于自己的个人网站,CentOS服务器部署必看

快速将InnoDB表复制到另一个实例

mysql快速启动bat

启动多个mysql实例