Docker搭建MHA环境

MHA环境搭建

01 | 基础环境准备

# 1. 安装docker(for mac)
# 其他版本国内镜像下载 http://mirrors.ustc.edu.cn/docker-ce/
brew cask install docker
# 2. 获取centos镜像,注:不要使用OFFICIAL版本,会存在大量依赖问题
docker pull ansible/centos7-ansible
# 3. 查看镜像
docker images         
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
ansible/centos7-ansible   latest              688353a31fde        3 years ago         447MB
# 4. 启动容器
docker run -itd --privileged --name centos-mysql 688353a31fde /usr/sbin/init
# 5. 进入容器
docker exec -it centos-mysql /bin/bash 
# 6. 容器中安装基础工具
yum install wget
yum install net-tools
yum install initscripts
yum install openssh-server

02 | 安装mysql

# 1. 下载mysql RPM安装包
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
# 2. 
yum -y install mysql57-community-release-el7-10.noarch.rpm
# 3. 安装
yum -y install mysql-community-server
# 4. 启动
systemctl start mysqld.service

# 5. 重设密码
#  查看初始化密码
cat /var/log/mysqld.log |grep 'temporary password'
#  连接mysql
mysql -u root -p 
#  重设密码
set password=password('new_password')
# 7. 保存容器制作镜像 
docker stop mysql
docker commit mysql centos:mysql-57

03 | 创建Master

# 0. 查看镜像
docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
cenos                     mysql-57            e087a9b7b7bb        3 minutes ago       1.62GB
ansible/centos7-ansible   latest              688353a31fde        3 years ago         447MB
# 1. 使用带有mysql的镜像启动容器并进入容器
docker run -itd --privileged --name mysql-master e087a9b7b7bb /usr/sbin/init
docker exec -it mysql-master /bin/bash 

# 2. 修改my.inf配置文件,增加以下配置
vi /etc/my.inf
log_bin=master-bin
log_bin_index=master-bin.index
server-id=1
sync-binlog=1
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=sys
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates = 1

plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
loose_rpl_semi_sync_master_enabled = 1
loose_rpl_semi_sync_slave_enabled = 1
loose_rpl_semi_sync_master_timeout = 5000
# 3. 重启mysql
systemctl restart mysqld.service
# 4. 登录mysql进行账户授权,注:identified by 后需要填写你root账户对应的密码
mysql -u root -p
mysql> grant replication slave on *.* to 'root'@'%' identified by 'Mysql##06';
mysql> grant all privileges on *.* to 'root'@'%' identified by 'Mysql##06';
mysql> flush privileges;

04 | 创建Slave

# 1. 使用带有mysql的镜像启动容器并进入容器
docker run -itd --privileged --name mysql-slave-1 e087a9b7b7bb /usr/sbin/init
docker exec -it mysql-slave-1 /bin/bash 
# 2. 修改my.inf配置文件,增加以下配置
vi /etc/my.inf
server_id=2
relay_log_index = slave_relay_bin.index
relay_log= slave_relay_bin
innodb_log_file_size= 256M
expire-logs-days = 1 
read_only=1
gtid_mode = on 
enforce_gtid_consistency = 1
log_slave_updates = 1
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=sys
# 候选时需要设置参数
log-bin = mysql-bin 
log-bin-index = mysql-bin.index
plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
loose_rpl_semi_sync_master_enabled = 1
loose_rpl_semi_sync_slave_enabled = 1
loose_rpl_semi_sync_master_timeout = 5000

# 3. 重启mysql
# 启动之前需要先删除auto.cnf文件,不然连接master时会失败
rm /var/lib/mysql/auto.cnf
systemctl restart mysqld.service

# 4. 连接Master
#  4.1 登录mysql
mysql -u root -p
#  4.2 设置master相关信息,以上信息可通过在master mysql中执行show master status; 命令获得
mysql> change master to 
master_host='172.17.0.2',
master_port=3306,
master_user='root',
master_password='Mysql##06',
master_log_file='master-bin.000001',
master_log_pos=1306;

mysql> start slave;
mysql> show slave status\G;

# 5. 使用同样的方式创建salve2,注:需要保证my.inf配置中server_id保持唯一

# 6. 测试主从复制
# master 创建一个数据库test
mysql> create database test;

# slave1 查看书否创建同步成功
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.00 sec)
# slave2 查看书否创建同步成功
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.00 sec)

05 | 配置公钥互信

5.1 节点说明

172.17.0.2 master
172.17.0.3 slave
172.17.0.4 slave
172.17.0.5 mha-manager

5.2 配置公钥互信

# 1. 启动ssh
service sshd restart
# 2. 在分发密钥是需要输入系统密码,由于系统是由docker容器启动,需要重设系统密码(非dokcer忽略)
passwd root 
# 生成密钥对
ssh-keygen -t rsa
# 分发到所有节点包括自己
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]

05 | MHA安装配置

# 所有节点安装以下组件
yum install epel-release
yum install perl-DBD-MySQL
yum install perl-Config-Tiny
yum install perl-Log-Dispatch
yum install perl-Parallel-ForkManager

wget https://qiniu.wsfnk.com/mha4mysql-node-0.58-0.el7.centos.noarch.rpm
rpm -ivh mha4mysql-node-0.58-0.el7.centos.noarch.rpm

# manager上安装
wget https://qiniu.wsfnk.com/mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
rpm -ivh mha4mysql-manager-0.58-0.el7.centos.noarch.rpm

MHA Manager配置

touch /etc/mha-manager.cnf

[server default]
manager_workdir=/var/log/mha/app1
manager_log=/var/log/mha/app1/manager.log
user=root 
password=Mysql##06
ssh_user=root
repl_user=root
repl_password=Mysql##06
ping_interval=1

[server1]
hostname=172.17.0.2
master_binlog_dir=/var/lib/mysql
port=3306

[server2]
hostname=172.17.0.3
port=3306

[server3]
hostname=172.17.0.4
port=3306

运行

# 检查SSH配置
masterha_check_ssh --conf=/etc/mha.cnf 
[info] All SSH connection tests passed successfully.
# 验证配置
masterha_check_repl --conf=/etc/mha.cnf
# 运行
nohup masterha_manager --conf=/etc/app1.cnf  --remove_dead_master_conf --ignore_last_failover < /dev/null >  /var/log/mha/app1/manager.log  2>&1 &

注:mha-manager节点不要和mysql节点放到一起,不然有可能遇到各种莫名其妙的依赖问题,如下:
在这里插入图片描述
遇到如上错误只需把mah-manger节点和mysql节点分开即可

参考:
https://www.cnblogs.com/sdhzdtwhm/p/10008793.html
https://www.cnblogs.com/kevincaptain/p/10333891.html

你可能感兴趣的:(mysql)