環(huán)境
- Docker+Cnetos7+Mysql8.0
用Docker拉取centos鏡像并創(chuàng)建容器
docker search centos
docker pull docker.io/centos
docker run -idt --name centos -v /home/dk/tmp:/mnt --privileged=true docker.io/centos
=============此處-v是掛載一個(gè)臨時(shí)目錄, 方便宿主機(jī)與Host共享文件================
初始設(shè)置
- 進(jìn)入交互模式
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
- 二進(jìn)制安裝mysql, 參考https://dev.mysql.com/doc/refman/8.0/en/binary-installation.html
yum -y install libaio
yum -y install numactl
##官網(wǎng)沒提到需要這個(gè),但是后續(xù)會(huì)報(bào)錯(cuò), 所以提前裝好
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
##此處記住初始化密碼,否則后面需要手動(dòng)去改 ##
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
####此處還可以設(shè)為開機(jī)啟動(dòng)
''' 將/usr/local/mysql/bin 加入到.bash_profile里面
- 安裝MHA ,其中manager只需要一臺(tái)slave安裝就可以,我這里全部都安裝了, Node是所有節(jié)點(diǎn)都必須裝的,安裝過程中會(huì)依賴相關(guān)perl模塊,配置EPEL yum倉(cāng)庫(kù)后安裝比較方便
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
##因?yàn)槲姨崆耙呀?jīng)下載好了這兩個(gè)rpm安裝包, 也可以下載源碼編譯安裝
mha manger 和 node下載地址:
https://github.com/yoshinorim/mha4mysql-manager/releases
https://github.com/yoshinorim/mha4mysql-node/releases
rpm -ivh *.rpm
-
驗(yàn)證是否生成相關(guān)MHA管理腳本
ls /bin |grep -E 'master|logs'
-
至此,所有初始設(shè)置完成. 返回宿主機(jī),將之前更改提交為一個(gè)新的鏡像,后續(xù)以這個(gè)鏡像建立容器配置mysql主從復(fù)制及MHA
docker stop centos docker commit centos mysql_mha:base
配置主從復(fù)制
- 創(chuàng)建復(fù)制及監(jiān)控用戶
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 上分別設(shè)置為2和3
log-bin=mysql-bin
gtid_mode = on
#開啟gtid牛曹,必須主從全開
enforce_gtid_consistency = 1
log_slave_updates = 1
- 開啟復(fù)制
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(后臺(tái))
nohup masterha_manager --conf=/etc/app1.cnf < /dev/null > /var/log/masterha/app1/app1.log 2>&1 &
## 或者masterha_manager(單個(gè)集群) masterha_manager --conf=/etc/app1.cnf(多個(gè)MHA集群共用一個(gè)maneger則要指定配置文件)
這樣就可以實(shí)現(xiàn)發(fā)生故障的時(shí)候自動(dòng)啟用另外一個(gè)slave為master 了, 后續(xù)在討論IP切換.