环境
- Docker+Cnetos7+Mysql8.0
用Docker拉取centos镜像并创建容器
docker search centos
docker pull docker.io/centos
docker run -idt --name centos -v /home/dk/tmp:/mnt --privileged=true docker.io/centos
=============此处-v是挂载一个临时目录, 方便宿主机与Host共享文件================
初始设置
- 进入交互模式
docker exec -it centos bash
vi yum_install.sh
#!/bin/bash
package=$(yum provides $1 |grep -B1 Repo |head -1|awk '{print $1}')
yum -y install $package
chmod 700 yum_install.sh
yum_install.sh vi
yum_install.sh ifconfig
yum_install.sh ping
yum_install.sh service
yum -y install libaio
yum -y install numactl
##官网没提到需要这个,但是后续会报错, 所以提前装好
yum -y install openssl
##同上
yum -y install keeepalived
##备用,非必须
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
cd /usr/local/src
cp /mnt/mysql*.gz ./
tar zxvf mysql*.gz
rm -rf *.gz
cd ..
ln -s ./src/mysql* mysql
cd mysql
mkdir mysql-files
chown mysql:mysql mysql-files
chmod 750 mysql-files
bin/mysqld --initialize --user=mysql
##此处记住初始化密码,否则后面需要手动去改 ##
bin/mysql_ssl_rsa_setup
#bin/mysqld_safe --user=mysql & ##可选我是用下面步骤##
cp /usr/local/mysql/support-files/mysqld_safe /etc/init.d/mysqld
service mysqld start
####此处还可以设为开机启动
''' 将/usr/local/mysql/bin 加入到.bash_profile里面
- 安装MHA ,其中manager只需要一台slave安装就可以,我这里全部都安装了, Node是所有节点都必须装的,安装过程中会依赖相关perl模块,配置EPEL yum仓库后安装比较方便
yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum makecache
yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-CPAN
cd /mnt
##因为我提前已经下载好了这两个rpm安装包, 也可以下载源码编译安装
mha manger 和 node下载地址:
https://github.com/yoshinorim/mha4mysql-manager/releases
https://github.com/yoshinorim/mha4mysql-node/releases
rpm -ivh *.rpm
-
验证是否生成相关MHA管理脚本
ls /bin |grep -E 'master|logs'
image.png -
至此,所有初始设置完成. 返回宿主机,将之前更改提交为一个新的镜像,后续以这个镜像建立容器配置mysql主从复制及MHA
docker stop centos docker commit centos mysql_mha:base
配置主从复制
- 创建复制及监控用户
mysql>create user 'repl'@'172.172.0.%' identified by '123456';
mysql> grant replication slave on *.* to 'repl'@'172.172.0.%';
mysql> create user 'root'@'172.172.0.%' identified by '123456';
mysql> grant all privileges on *.* to 'root'@'172.172.0.%';
- 配置文件
server-id=1
##在slave 上分别设置为2和3
log-bin=mysql-bin
gtid_mode = on
#开启gtid,必须主从全开
enforce_gtid_consistency = 1
log_slave_updates = 1
- 开启复制
change master to master_host='172.172.0.10',master_user='repl',master_password='123456',master_auto_position=1;
###,master_log_file='mysql-bin.000012',master_log_pos=979;
set global read_only=1;
start slave
配置MHA
- 配置ssh
[root@135361cd5fad ~]# /sbin/sshd&
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
sshd: no hostkeys available -- exiting.
ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key
ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key
/sbin/sshd&
ssh-keygen -t rsa
cat id_rsa.pub >>authorized_keys
scp 172.172.0.30:/root/.ssh/id_rsa.pub /tmp
cat /tmp/id_rsa.pub >>authorized_keys
scp 172.172.0.20:/root/.ssh/id_rsa.pub /tmp
cat /tmp/id_rsa.pub >>authorized_keys
scp authorized_keys 172.172.0.20:/root/.ssh
scp authorized_keys 172.172.0.30:/root/.ssh
- 编辑manager配置文件
vi /etc/app1.cnf
[server default]
#mysql user and password
user=root
password=123456
ssh_user=root
# working directory on the manager
manager_workdir=/var/log/masterha/app1
# working directory on MySQL servers
remote_workdir=/var/log/masterha/app1
[server1]
hostname=172.172.0.30
[server2]
hostname=172.172.0.20
[server3]
hostname=172.172.0.10
- 检查ssh 和replication配置情况
masterha_check_repl --conf=/etc/app1.cnf
masterha_check_ssh --conf=/etc/app1.cnf
masterha_check_status --conf=/etc/app1.cnf
- 开启manager(后台)
nohup masterha_manager --conf=/etc/app1.cnf < /dev/null > /var/log/masterha/app1/app1.log 2>&1 &
## 或者masterha_manager(单个集群) masterha_manager --conf=/etc/app1.cnf(多个MHA集群共用一个maneger则要指定配置文件)
这样就可以实现发生故障的时候自动启用另外一个slave为master 了, 后续在讨论IP切换.
网友评论