导读
本内容摘自知数堂第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
[[email protected] ~]# chkconfig —del iptables
[[email protected] ~]# /etc/init.d/iptables stop
并且关闭 selinux
[[email protected] ~]# setenforce 0
并且将配置文件 /etc/sysconfig/selinux 中的下面这行
SELINUX=permissive
更改为
SELINUX=disabled
下载MySQL
[[email protected] ~]# mkdir /data/Soft
[[email protected] ~]# cd /data/Soft
[[email protected] ~]# 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上分别执行。
[[email protected] ~]# mkdir /opt/mysql
[[email protected] ~]# cd /opt/mysql
[[email protected] ~]# tar zxvf /data/Soft/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
[[email protected] ~]# ln -s /opt/mysql/mysql-5.7.17-linux-glibc2.5-x86_64 /usr/local/mysql
[[email protected] ~]# mkdir /data/mysql/mysql3309/{data,logs,tmp} -p
[[email protected] ~]# groupadd mysql
[[email protected] ~]# useradd -g mysql -s /sbin/nologin -d /usr/local/mysql -M mysql
[[email protected] ~]# chown -R mysql:mysql /data/mysql/
[[email protected] ~]# chown -R mysql:mysql /usr/local/mysql
[[email protected] ~]# cd /usr/local/mysql/
[[email protected] ~]# ./bin/mysqld —defaults-file=/data/mysql/mysql3309/my3309.cnf —initialize
[[email protected] ~]# cat /data/mysql/mysql3309/data/error.log |grep password
[[email protected] ~]# /usr/local/mysql/bin/mysqld —defaults-file=/data/mysql/mysql3309/my3309.cnf &
[[email protected] ~]# echo “export PATH=$PATH:/usr/local/mysql/bin” >>/etc/profile
[[email protected] ~]# source /etc/profile
[[email protected] ~]# 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_