▲就业班和全程班的小伙伴看这里:(学习老王视频的作业第29-30节)
1、编写脚本,支持让用户自主选择,使用mysqldump还是xtraback全量备份。
PS3="please in put number is backup database mode: "
select num in mysqldump xtraback quit;do
case $REPLY in
1)
mysqldump -A --master-data=2 -F --single-transaction -q |gzip > /data/mysql-`date +%F`.sql.gz
echo "mysqldump-all-databses successful"
break
;;
2)
mkdir /data/xrabackup-`date +%F`
xtrabackup --backup --target-dir=/data/xrabackup-`date +%F` &> /dev/null
echo "xtrabackup-all-databases successful"
break
;;
3)
break
;;
*)
echo "please choice 1 or 2 or 3"
esac
done
2、配置Mysql主从同步
实验环境:
(1)37.7为数据库服务器;37.17从服务器;
(2)37.7开启二进制日志
一、主服务器-37.7
[root@centos7 ~]#vim /etc/my.cnf
server-id=1
log-bin=/data/logbin/mysql-bin
:wq
#systemctl start mariadb
mysql> grant replication slave on *.* to repluser@'192.168.37.%' identified by 'centos';
mysql> show master logs ;
mysql-bin.000002 | 402 |
二、从服务器-37.17
[root@centos7 ~]#vim /etc/my.cnf
server-id=2
read_only=ON
:wq
#systemctl start mariadb
mysql>CHANGE MASTER TO
MASTER_HOST='192.168.37.7',
MASTER_USER='repluser',
MASTER_PASSWORD='centos',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000002',
MASTER_LOG_POS=402;
mysql>start slave;
mysql>show slave status\G
3、使用MHA实现Mysql高可用。
实验环境:4台机器;主从服务器配置为半同步复制;并且可以基于KEY登录
1、Manager:192.168.37.7
2、master: 192.168.37.17
3、slave1: 192.168.37.27 (准备提升的从服务器)
4、slave2: 192.168.37.37
5、在管理节点上安装两个包
mha4mysql-manager
mha4mysql-node
6、在被管理节点安装
mha4mysql-node
一、主从复制
1.1 master---37.17
#vim /etc/my.cnf
[mysqld]
log-bin
server_id=1
skip_name_resolve=1
:wq
#systemctl restart mariadb
mysql>grant replication slave on *.* to repluser@'192.168.37.%' identified by 'magedu';
mysql>grant all on *.* to mhauser@'192.168.37.%' identified by 'magedu';
1.2 所有slave
#vim /etc/my.cnf
[mysqld]
server_id=XX
log-bin
read_only
relay_log_purge=0
skip_name_resolve=1
:wq
#systemctl restart mariadb
mysql>CHANGE MASTER TO MASTER_HOST='192.168.37.17',
MASTER_USER='repluser', MASTER_PASSWORD='magedu',
MASTER_LOG_FILE='mariadb-bin.000001', MASTER_LOG_POS=245;
二、安装mha 需要启用epel 4台主机都进行
yum install mha*.rpm
三、基于KEY免密码认证---在37.7上生成KEY,然后复制到其他主机上即可实现
#ssh-keygen
#ls -a .ssh
#ssh-copy-id 192.168.37.7
#cat /root/.ssh/authorized_keys
#scp -r .ssh 192.168.37.17:/root/
#scp -r .ssh 192.168.37.27:/root/
#scp -r .ssh 192.168.37.37:/root/
#ssh 192.168.37.17
四、MHA的配置文件---37.7
#mkdir /etc/mha
#vim /etc/mha/app1.cnf
[server default]
user=mhauser
password=magedu
manager_workdir=/data/mastermha/app1/
manager_log=/data/mastermha/app1/manager.log
remote_workdir=/data/mastermha/app1/
ssh_user=root
repl_user=repluser
repl_password=magedu
ping_interval=1
[server1]
hostname=192.168.37.17
candidate_master=1
[server2]
hostname=192.168.37.27
candidate_master=1
[server3]
hostname=192.168.37.37
:wq
五、Mha验证
masterha_check_ssh --conf=/etc/mha/app1.cnf
masterha_check_repl --conf=/etc/mha/app1.cnf
六、启动
masterha_manager --conf=/etc/mha/app1.cnf
七、排错日志
#tail -f /data/mastermha/app1/manager.log
八、破坏性实验
8.1、37.17 主机关机;查看是否会启用37.27
8.2、MHA为前台服务,启用37.27后,服务会自动退出,可以从排错日志中看到
8.3、切换成功后,要将37.27的配置文件中的read-only注释掉
8.4、37.17配置为从服务器继续使用