码迷,mamicode.com
首页 > 数据库 > 详细

Mysql 5.7 集群部署,keepalived

时间:2019-10-09 12:39:35      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:ipad   unlock   网卡   备份   压缩   out   rom   open   socket   

参考文章:

https://blog.csdn.net/f18770366447/article/details/80703347

https://www.cnblogs.com/benjamin77/p/8682360.html

https://www.xiaocoder.com/2017/03/17/mysql-installation-guide/

https://blog.csdn.net/hongguo_cheng/article/details/80293588

? ?

? ?

1,配置环境

两个节点,vcpu8个,内存8G,磁盘16G

操作系统:Centos7.5最小化安装

A节点IP地址:172.16.103.14 node1

B节点IP地址:172.16.103.15 node2

VIP:172.16.103.16

关闭selinux和防火墙

2,查看是否已经存在数据库,如果已经存在则卸载

rpm -qa |grep mysql //查看是否mysql数据库

rpm -qa |grep mari //查看是否又mariadb数据库

rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64 //卸载mariadb数据库,包的名字一定要是grep出来的全名

3,安装依赖包

yum install -y net-tools perl

4,下载mysql包,上传解压,安装。

下载5.7的mysql 社区版包

https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.25-1.el7.x86_64.rpm-bundle.tar

上传到/software目录下,解压

tar -xvf mysql-5.7.25-1.el7.x86_64.rpm-bundle.tar

安装

rpm -ivh mysql-community-common-5.7.25-1.el7.x86_64.rpm

rpm -ivh mysql-community-libs-5.7.25-1.el7.x86_64.rpm

rpm -ivh mysql-community-libs-compat-5.7.25-1.el7.x86_64.rpm

rpm -ivh mysql-community-client-5.7.25-1.el7.x86_64.rpm

rpm -ivh mysql-community-server-5.7.25-1.el7.x86_64.rpm

5,创建目录,添加用户,设置所属

mkdir -p /data/mysql/{data,tmp,log}

groupadd mysql

useradd -g mysql mysql

chown -R mysql:mysql /data/mysql/{data,tmp,log}

6,修改配置文件

vi /etc/my.cnf

#配置文件内容开始

[client]

port = 3306

socket = /data/mysql/tmp/mysql.sock

default-character-set = utf8mb4

? ?

[mysqld]

port = 3306

datadir = /data/mysql/data

pid-file = /data/mysql/tmp/mysqld.pid

socket = /data/mysql/tmp/mysql.sock

log-error = /data/mysql/log/error.log

character_set_server = utf8mb4

user = mysql

bind-address = *

server-id = 1 #节点2修改为2

symbolic-links=1

connect_timeout = 3600

wait_timeout = 3600

interactive_timeout = 3600

explicit_defaults_for_timestamp = true

log-bin = mysql-bin

sync_binlog = 1

binlog_checksum = none

binlog_format = mixed

auto-increment-increment = 2

auto-increment-offset = 1 #节点2修改为2

slave-skip-errors = all

#配置文件内容结束

启动服务

systemctl start mysqld

8,登陆数据库并做相关配置获取mysql安装时的默认密码

MySQL_PASS=$(cat /data/mysql/log/error.log | grep "A temporary password" | awk ‘{print $NF}‘)

使用改密码登陆数据库

mysql -u root -p"${MySQL_PASS}"

设置新密码为:Admin123,.

mysql> SET PASSWORD=‘Admin123,.‘;

开启任意主机可使用root用户访问所有库,密码为ADmin123,.

mysql> GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘%‘ IDENTIFIED BY ‘ADmin123,.‘ WITH GRANT OPTION;

9,节点2执行上面1-8步骤,然后添加同步账户

10,添加同步账户,账户名为repl,密码为ADMin123,.

节点1mysql> grant replication slave on *.* to ‘repl‘@‘172.16.103.15‘ identified by ‘ADMin123,.‘;

节点2mysql> grant replication slave on *.* to ‘repl‘@‘172.16.103.14‘ identified by ‘ADMin123,.‘;

使配置立即生效

mysql> flush privileges;

11,查看的主库状态

节点1

show master status;

技术图片

节点2

show master status;

技术图片

12,开始同步

节点1操作,change master 语句中填写节点2的信息

mysql> unlock tables;

mysql> stop slave;

mysql> change master to master_host=‘172.16.103.15‘,master_user=‘repl‘,master_password=‘ADMin123,.‘,master_log_file=‘mysql-bin.000003‘,master_log_pos=150;

mysql> start slave;

mysql> show slave status \G

具体过程

技术图片

执行show slave status \G之后看到以下内容为正常!

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

技术图片

节点2操作

节点2操作,change master 语句中填写节点1的信息

mysql> unlock tables;

mysql> stop slave;

mysql> change master to master_host=‘172.16.103.14‘,master_user=‘repl‘,master_password=‘ADMin123,.‘,master_log_file=‘mysql-bin.000002‘,master_log_pos=2848;

mysql> start slave;

mysql> show slave status \G

技术图片

执行show slave status \G之后看到以下内容为正常!

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

技术图片

13,主主同步测试

在节点1登陆数据库,创建数据库,创建表,插入数据,然后在节点2上查看。

解锁表

unlock tables;

创建ceshiku数据库

create database ceshiku;

使用ceshik数据库

use ceshiku;

创建表,名字为哈哈,后面是列是数据类型

create table if not exists haha ( id int(10) PRIMARY KEY AUTO_INCREMENT, name varchar(50) NOT NULL);

插入数据

insert into haha values(2,‘guojing‘);

插入数据

insert into haha values(1,"huangrong");

查询表haha的所有数据

select * from haha;

以下是图形操作的过程

技术图片

? ?

在节点2上查看同步过来的信息

select * from ceshiku.haha;

技术图片

14,安装keepalived软件包,并集成成系统服务(两个节点都执行)

keepalived包的下载地址

http://www.keepalived.org/software/keepalived-1.3.5.tar.gz

所有版本

https://www.keepalived.org/download.html

上传keepalive包到/usr/local/src/下,安装依赖包,解压缩软件包,编译,安装

安装依赖包

yum install -y openssl-devel gcc

进入目录解压缩

cd /usr/local/src/

tar -zxvf keepalived-1.3.5.tar.gz

进入目录,编译,安装

cd keepalived-1.3.5

./configure --prefix=/usr/local/keepalived

make && make install

拷贝相关目录和文件的到系统目录

cp /usr/local/src/keepalived-1.3.5/keepalived/etc/init.d/keepalived /etc/rc.d/init.d/

cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

mkdir -p /etc/keepalived/

cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

cp /usr/local/keepalived/sbin/keepalived /usr/local/sbin/

echo "/etc/init.d/keepalived start" >> /etc/rc.local

15,配置keepaived服务

节点1配置

备份原有的conf文件

cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak

vi /etc/keepalived/keepalived.conf #删除配置文件中的所有内容,加入以下内容

#配置文件内容开始

! Configuration File for keepalived

? ?

global_defs {

notification_email {

ops@wangshibo.cn

tech@wangshibo.cn

}

? ?

notification_email_from ops@wangshibo.cn

smtp_server 127.0.0.1

smtp_connect_timeout 30

router_id MASTER-HA

}

? ?

vrrp_script chk_mysql_port { #检测mysql服务是否在运行。有很多方式,比如进程,用脚本检测等等

script "/opt/chk_mysql.sh" #这里通过脚本监测

interval 2 #脚本执行间隔,每2s检测一次

weight -5 #脚本结果导致的优先级变更,检测失败(脚本返回非0)则优先级 -5

fall 2 #检测连续2次失败才算确定是真失败。会用weight减少优先级(1-255之间)

rise 1 #检测1次成功就算成功。但不修改优先级

}

? ?

vrrp_instance VI_1 {

state MASTER

interface ens192 #指定自己的网卡接口和IP

mcast_src_ip 172.16.103.14

virtual_router_id 51 #路由器标识,MASTERBACKUP必须是一致的

priority 101 #定义优先级,数字越大,优先级越高,在同一个vrrp_instance下,MASTER的优先级必须大于BACKUP的优先级。这样MASTER故障恢复后,就可以将VIP资源再次抢回来

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

172.16.103.16

}

? ?

track_script {

chk_mysql_port

}

}

#配置文件内容结束

编写检测脚本,KeepAlived做心跳检测,如果Master的MySQL服务挂了(3306端口挂了),那么它就会选择自杀。Slave的KeepAlived通过心跳检测发现这个情况,就会将VIP的请求接管

vi /opt/chk_mysql.sh

#脚本内容开始

#!/bin/bash

counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)

if [ "${counter}" -eq 0 ]; then

/etc/init.d/keepalived stop

fi

#脚本内容结束

给脚本添加执行权限

chmod 755 /opt/chk_mysql.sh

启动keepalived服务

systemctl start keepalived

? ?

如果启动延迟最终导致启动失败,最后keepalived的状态是下图所示。

技术图片

然后查看日志:

tail -f /var/log/messages

Apr 16 18:35:18 node1 systemd: PID file /usr/local/keepalived/var/run/keepalived.pid not readable (yet?) after start.

Apr 16 18:36:48 node1 systemd[1]: keepalived.service start operation timed out. Terminating.

Apr 16 18:36:48 node1 systemd[1]: Failed to start LVS and VRRP High Availability Monitor.

Apr 16 18:36:48 node1 systemd[1]: Unit keepalived.service entered failed state.

Apr 16 18:36:48 node1 systemd[1]: keepalived.service failed.

这种情况下vip确实已经生效了,但是keepalived服务也没有正常开启,解决办法是编辑keepalived服务的pid文件指向的位置

vi /usr/lib/systemd/system/keepalived.service

PIDFile=/var/run/keepalived.pid

然后重载服务,重启服务

systemctl daemon-reload

systemctl start keepalived

systemctl status keepalived

技术图片

? ?

节点2配置参照节点1配置,只是keepalived.conf配置文件的内容不通,具体内容如下

#配置文件内容开始

! Configuration File for keepalived

? ?

global_defs {

notification_email {

ops@wangshibo.cn

tech@wangshibo.cn

}

? ?

notification_email_from ops@wangshibo.cn

smtp_server 127.0.0.1

smtp_connect_timeout 30

router_id MASTER-HA

}

? ?

vrrp_script chk_mysql_port { #检测mysql服务是否在运行。有很多方式,比如进程,用脚本检测等等

script "/opt/chk_mysql.sh" #这里通过脚本监测

interval 2 #脚本执行间隔,每2s检测一次

weight -5 #脚本结果导致的优先级变更,检测失败(脚本返回非0)则优先级 -5

fall 2 #检测连续2次失败才算确定是真失败。会用weight减少优先级(1-255之间)

rise 1 #检测1次成功就算成功。但不修改优先级

}

? ?

vrrp_instance VI_1 {

state BACKUP

interface ens192 #指定自己的网卡接口和IP

mcast_src_ip 172.16.103.15

virtual_router_id 51 #路由器标识,MASTERBACKUP必须是一致的

priority 100 #定义优先级,数字越大,优先级越高,在同一个vrrp_instance下,MASTER的优先级必须大于BACKUP的优先级。这样MASTER故障恢复后,就可以将VIP资源再次抢回来

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

172.16.103.16

}

? ?

track_script {

chk_mysql_port

}

}

#配置文件内容结束

同样参照节点1的配置,节点2同样需要配置检测脚本

vi /opt/chk_mysql.sh

#脚本内容开始

#!/bin/bash

counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)

if [ "${counter}" -eq 0 ]; then

/etc/init.d/keepalived stop

fi

#脚本内容结束

设置权限

chmod 755 /opt/chk_mysql.sh

同样要修改keepalived服务的pid文件指向的位置

vi /usr/lib/systemd/system/keepalived.service

PIDFile=/var/run/keepalived.pid

启动服务

systemctl start keepalived

检查keepalived的状态

systemctl status keepalived

技术图片

可以看到该节点被定义为从节点了。

至此,使用navicat客户端连接vip即可,vip可以对外提供服务了。

? ?

? ?

? ?

? ?

? ?

? ?

? ?

? ?

? ?

Mysql 5.7 集群部署,keepalived

标签:ipad   unlock   网卡   备份   压缩   out   rom   open   socket   

原文地址:https://www.cnblogs.com/withfeel/p/11640728.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!