暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

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

原创 小知_知数堂 2019-12-16
598

导读

本内容摘自知数堂第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 版本

机器名 操作系统 Ip
node1 centos-6.8 192.168.11.100
node2 centos-6.8 192.168.11.101
node3 centos-6.8 192.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高可用切换,完成一次高可用自动切换。


有想要讨论问题的朋友们可以加我哦,这里有一个乐于交友的人鸭!

微信:lvqingshan_

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论