基于MySQL 5.7多源复制及Keepalived搭建三节点高可用架构

xiaoxiao2021-02-28  14

导读

本内容摘自知数堂第35期公开课《MySQL 5.7 高可用新玩法》

本次公开课视频请访问  http://pan.baidu.com/s/1mia6MZu

知数堂公开课相关视频请访问  https://ke.qq.com/course/172600

课中涉及到的脚本及配置文件请点击文末 “阅读原文”

基本环境准备

使用Centos 6.X 64位系统 MySQL 使用 MySQL-5.7.17-x86_64 版本,去官方下载mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz 版本

机器名操作系统Ipnode1centos-6.8192.168.11.100node2centos-6.8192.168.11.101node3centos-6.8192.168.11.102

三节点集群设置VIP为 192.168.11.110

一般我们建议关闭iptables

[wubx@zhishuedu.com ~]# chkconfig —del iptables [wubx@zhishuedu.com ~]# /etc/init.d/iptables stop

并且关闭 selinux

[wubx@zhishuedu.com ~]# setenforce 0 并且将配置文件 /etc/sysconfig/selinux 中的下面这行 SELINUX=permissive 更改为 SELINUX=disabled

下载MySQL

[wubx@zhishuedu.com ~]# mkdir /data/Soft [wubx@zhishuedu.com ~]# cd /data/Soft [wubx@zhishuedu.com ~]# wget -c https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.17-linux-glibc2.5-x86\_64.tar.gz

MySQL部署约定

二进制文件放置到 /opt/mysql/ 下面对应的目录。数据文件全部放置到 /data/mysql/ 下面对应的目录。原始二进制文件下载到 /data/Soft/ 目录下。

MySQL基本安装

以下安装步骤需要在node1, node2, node3上分别执行。

[wubx@zhishuedu.com ~]# mkdir /opt/mysql [wubx@zhishuedu.com ~]# cd /opt/mysql [wubx@zhishuedu.com ~]# tar zxvf /data/Soft/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz [wubx@zhishuedu.com ~]# ln -s /opt/mysql/mysql-5.7.17-linux-glibc2.5-x86_64 /usr/local/mysql [wubx@zhishuedu.com ~]# mkdir /data/mysql/mysql3309/{data,logs,tmp} -p [wubx@zhishuedu.com ~]# groupadd mysql [wubx@zhishuedu.com ~]# useradd -g mysql -s /sbin/nologin -d /usr/local/mysql -M mysql [wubx@zhishuedu.com ~]# chown -R mysql:mysql /data/mysql/ [wubx@zhishuedu.com ~]# chown -R mysql:mysql /usr/local/mysql [wubx@zhishuedu.com ~]# cd /usr/local/mysql/ [wubx@zhishuedu.com ~]# ./bin/mysqld —defaults-file=/data/mysql/mysql3309/my3309.cnf —initialize [wubx@zhishuedu.com ~]# cat /data/mysql/mysql3309/data/error.log |grep password [wubx@zhishuedu.com ~]# /usr/local/mysql/bin/mysqld —defaults-file=/data/mysql/mysql3309/my3309.cnf & [wubx@zhishuedu.com ~]# echo “export PATH=$PATH:/usr/local/mysql/bin” >>/etc/profile [wubx@zhishuedu.com ~]# source /etc/profile [wubx@zhishuedu.com ~]# mysql -S /tmp/mysql3309.sock -uroot -pXX mysql> grant replication slave,replication client on . to ‘repl’@’%’ identified by ‘repl4slave’; mysql> grant all privilegs on test.* to ‘wubx’@’%’ identified by ‘wubx’; mysql> reset master;

每个节点按上面进行,遇到初始化和启动故障请认真阅读 error log 日志文件。

搭建主从结构

node1上设置master

mysql> change master to master_host=’192.168.11.101’,master_port=3309, master_user=’repl’, master_password=’repl4slave’, master_auto_position=1 for channel ‘192_168_11_101_3309’; mysql> change master to master_host=’192.168.11.102’,master_port=3309, master_user=’repl’, master_password=’repl4slave’, master_auto_position=1 for channel ‘192_168_11_102_3309’; #确认同步OK mysql> start slave; mysql> show slave status\G

node2上设置master

mysql> change master to master_host=’192.168.11.100’,master_port=3309, master_user=’repl’, master_password=’repl4slave’, master_auto_position=1 for channel ‘192_168_11_100_3309’; mysql> change master to master_host=’192.168.11.102’,master_port=3309,master_user=’repl’, master_password=’repl4slave’,master_auto_position=1 for channel ‘192_168_11_102_3309’; #确认同步OK mysql> start slave; mysql> show slave status\G

node3上设置master

mysql> change master to master_host=’192.168.11.100’,master_port=3309, master_user=’repl’, master_password=’repl4slave’, master_auto_position=1 for channel ‘192_168_11_100_3309’; mysql> change master to master_host=’192.168.11.101’,master_port=3309, master_user=’repl’, master_password=’repl4slave’,master_auto_position=1 for channel ‘192_168_11_101_3309’; #确认同步OK mysql> start slave; mysql> show slave status\G

安装keepalived

node1, node2, node3 上分别安装keepalived。

yum install keepalivled

安装python依赖模块。

yum install MySQL-python.x86_64 yum install python2-filelock.noarch

keepalived配置 配置文件放置在 /etc/keepalived/keepalived.conf,内容如下

vrrp_script vs_mysql_82 {    script "/etc/keepalived/checkMySQL.py -h 127.0.0.1 -P 3309"    interval 15 } vrrp_instance VI_82 {    state backup    nopreempt    interface eth1    virtual_router_id 82    priority 100    advert_int 5    authentication {        auth_type PASS        auth_pass 1111    }    track_script {        vs_mysql_82    }    notify /etc/keepalived/notify.py    virtual_ipaddress {        192.168.11.110    } }

在node1, node2, node3分别执行下面命令,启动keepalived。

/etc/init.d/keepalived start

观察每个系统上的 /var/log/messages 中是否有报错等内容。

在client端机器上测试验证当前连接到哪个实例上。

mysql -h 192.168.11.110 -P 3309 -uwubx -pwubx -e “select @@hostname”

可以尝试关闭实例,自行触发keepalived高可用切换,完成一次高可用自动切换。

扫码关注 知数堂 公众号,第一时间关注干货文章

点击下方“阅读原文”,获取三节点高可用切换脚本。

转载请注明原文地址: https://www.6miu.com/read-1250350.html

最新回复(0)