004.MySQL双主+Keepalived高可用
一 基础环境
主机名
|
系统版本
|
MySQL版本
|
主机IP
|
Master01
|
CentOS 6.8
|
MySQL 5.6
|
172.24.8.10
|
Master02
|
CentOS 6.8
|
MySQL 5.6
|
172.24.8.11
|
VIP
|
172.24.8.12
|
二 实际部署
2.1 安装MySQL
[root@Master01 ~]# yum list installed | grep mysql #查看是否存在其他MySQL组件
[root@Master01 ~]# yum -y remove mysql-libs.x86_64 #为避免冲突引发报错,卸载已存在的组件
[root@Master01 study]# yum localinstall mysql-commu* -y
[root@Master01 ~]# chkconfig --list | grep mysqld #查看MySQL是否加入启动项
[root@Master01 ~]# chkconfig mysqld on
2.2 初始化MySQL
[root@Master01 ~]# service mysqld start
[root@Master01 ~]# mysql_secure_installation
[root@Master01 ~]# service iptables stop
[root@Master01 ~]# chkconfig iptables off
[root@Master01 ~]# vi /etc/sysconfig/selinux
SELINUX=disabled
2.3 master01 my.cf配置
[root@Master01 ~]# vi /etc/my.cnf
[mysqld]
……
server-id=1 #设置主服务器master的id
log-bin=mysql-bin #配置二进制变更日志命名格式
replicate-wild-ignore-table=mysql.%
replicate-wild-ignore-table=test.%
replicate-wild-ignore-table=information_schema.%
# 不同步哪些数据库
binlog-ignore-db = mysql
binlog-ignore-db = test
binlog-ignore-db = information_schema
# 只同步哪些数据库,除此之外,其他不同步
binlog-do-db = mysqltest
replicate-wild-ignore-table=mysql.% #从库配置不同步表
replicate-wild-do-table=test.% #从库配置需要同步的表
提示:不要在主库使用binlog-ignore-db和binlog-do-db,也不要在从库使用replicate-wild-ignore和replicate-wild-do-table。
2.4 创建账号
[root@Master01 ~]# mysql -uroot -p
Enter password:
mysql> grant replication slave on *.* to 'repl_user'@'172.24.8.11' identified by 'x12345678';
mysql> grant all privileges on *.* to 'root'@'172.24.8.%' identified by 'x120952576' with grant option;
mysql> flush privileges;
[root@Master01 ~]# service mysqld restart
[root@Master01 ~]# mysql -uroot -p
Enter password:
mysql> show master status;
2.5 smaster02 my.cf配置配置
[root@Master02 ~]# vi /etc/my.cnf
[mysqld]
server-id=2 #设置主服务器master的id
log-bin=mysql-bin #配置二进制变更日志命名格式
replicate-wild-ignore-table=mysql.%
replicate-wild-ignore-table=test.%
replicate-wild-ignore-table=information_schema.%
read_only=1
2.6 创建账号
[root@Master02 ~]# mysql -uroot -p
Enter password:
mysql> grant replication slave on *.* to 'repl_user'@'172.24.8.10' identified by 'x12345678';
mysql> grant all privileges on *.* to 'root'@'172.24.8.%' identified by 'x120952576' with grant option;
mysql> flush privileges;
[root@Master02 ~]# service mysqld restart
[root@Master02 ~]# mysql -uroot -p
Enter password:
mysql> show master status;
master02:
三 启动主从
3.1 手动同步
3.2 启动Master01的slave功能
[root@Master01 ~]# service mysqld restart
[root@Master01 ~]# mysql -uroot -p
Enter password:
mysql> change master to master_host='172.24.8.11',
master_user='repl_user',
master_password='x12345678',
master_log_file='mysql-bin.000001',
master_port=3306,
master_log_pos=120;
mysql> start slave;
mysql> show slave status\G #查看slave状态
3.3 启动Master02的slave功能
[root@Master02 ~]# service mysqld restart
[root@Master02 ~]# mysql -uroot -p
Enter password:
mysql> change master to master_host='172.24.8.10',
master_user='repl_user',
master_password='x12345678',
master_log_file='mysql-bin.000001',
master_log_pos=120;
mysql> start slave;
mysql> show slave status\G #查看slave状态
四 安装Keepalived
4.1 下载
[root@Master01 ~]# wget http://www.keepalived.org/software/keepalived-1.3.6.tar.gz
[root@Master01 ~]# tar -zvxf keepalived-1.3.6.tar.gz -C /tmp/
[root@Master01 ~]# cd /tmp/keepalived-1.3.6
[root@Master01 keepalived-1.3.6]# ./configure --prefix=/usr/local/keepalived/ --sysconf=/etc --with-init=SYSV
#注:(upstart|systemd|SYSV|SUSE|openrc) #根据你的系统选择对应的启动方式
[root@Master01 keepalived-1.3.6]# make && make install
[root@Master01 ~]# ln -s /usr/local/keepalived/sbin/keepalived /sbin
[root@Master01 ~]# chmod u+x /etc/init.d/keepalived
[root@Master01 ~]# chkconfig --add keepalived
[root@Master01 ~]# chkconfig --level 35 keepalived on
4.2 Master01配置Keepalived
[root@Master01 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
x120952576@126.com
#表示keepalived在发生诸如切换操作时发送Email给哪些地址,邮件地址可以多个,每行一个。
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 172.24.8.10
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
#这里设置VRID,这里非常重要,相同的VRID为一个组,他将决定多播的MAC地址
priority 100
advert_int 1
nopreempt
#不抢占,只在优先级高的机器上设置即可,优先级低的机器不设置
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_mysqld #执行定义的监控脚本
}
virtual_ipaddress {
172.24.8.12
}
}
vrrp_script check_mysqld {
script"/etc/keepalived/mysqlcheck/keepalived_check_mysql.sh "
interval 2
}
4.3 创建检测脚本
[root@Master01 ~]# mkdir -p /etc/keepalived/mysqlcheck/
[root@Master01 ~]# vi /etc/keepalived/mysqlcheck/keepalived_check_mysql.sh
#!/bin/bash
MYSQL=/usr/bin/mysql
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=x120952576
CHECK_TIME=3
#mysql is workingMYSQL_OK is 1 , mysql down MYSQL_OK is 0
MYSQL_OK=1
function check_mysql_helth (){
$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} -e "show status;" >/dev/null 2>&1
if [ $? = 0 ] ;then
MYSQL_OK=1
else
MYSQL_OK=0
fi
return $MYSQL_OK
}
while [ $CHECK_TIME -ne 0 ]
do
let "CHECK_TIME -= 1"
check_mysql_helth
if [ $MYSQL_OK = 1 ] ; then
CHECK_TIME=0
exit 0
fi
if [ $MYSQL_OK -eq 0 ] && [ $CHECK_TIME -eq 0 ]
then
/etc/init.d/keepalived stop
exit 1
fi
sleep 1
done
[root@Master01 ~]# chmod u+x /etc/keepalived/mysqlcheck/keepalived_check_mysql.sh
[root@Master01 ~]# service keepalived start
4.4 Master02配置Keepalived
[root@Master01 ~]# scp /etc/keepalived/keepalived.conf root@127.24.8.11:/etc/keepalived/
4.5 创建检测脚本
[root@Master02 ~]# mkdir -p /etc/keepalived/mysqlcheck/
[root@Master01 ~]# scp /etc/keepalived/mysqlcheck/keepalived_check_mysql.sh root@172.24.8.11:/etc/keepalived/mysqlcheck/
[root@Master02 ~]# chmod u+x /etc/keepalived/mysqlcheck/keepalived_check_mysql.sh
[root@Master02 ~]# service keepalived start
五 功能测试
5.1 测试vip
[root@test ~]# mysql -uroot -h172.24.8.12 -p
mysql> show variables like "%hostname%";
mysql> show variables like "%server_id%";
5.2 同步测试
[root@test ~]# mysql -uroot -p
Enter password:
mysql> create database mysqltest;
mysql> use mysqltest;
mysql> create table user(id int(5),name char(10));
mysql> insert into user values (00001,'zhangsan');
在Slave从服务器上进行验证:
[root@Master02 ~]# mysql -uroot -p
Enter password:
mysql> show databases;
mysql> select * from mysqltest.user;
5.3 测试Keepalived切换
[root@Master01 ~]# service mysqld stop #停止Master01的MySQL
[root@Master01 ~]# tail -f /var/log/messages #观察Master01的日志
[root@Master02 ~]# tail -f /var/log/messages #观察Master02的日志
[root@Client ~]# mysql -uroot -h172.24.8.12 -px120952576 #客户端连接VIP
004.MySQL双主+Keepalived高可用的更多相关文章
- MySQL双主+Keepalived高可用
原文转自:https://www.cnblogs.com/itzgr/p/10233932.html作者:木二 目录 一 基础环境 二 实际部署 2.1 安装MySQL 2.2 初始化MySQL 2. ...
- keepalived+mysql双主复制高可用方案
MySQL双主复制,即互为Master-Slave(只有一个Master提供写操作),可以实现数据库服务器的热备,但是一个Master宕机后不能实现动态切换.而Keepalived通过虚拟IP,实现了 ...
- MySQL集群搭建(6)-双主+keepalived高可用
双主 + keepalived 是一个比较简单的 MySQL 高可用架构,适用于中小 MySQL 集群,今天就说说怎么用 keepalived 做 MySQL 的高可用. 1 概述 1.1 keepa ...
- 基于Mysql 5.7 GTID 搭建双主Keepalived 高可用
实验环境 CentOS 6.9 MySQL 5.7.18 Keepalived v1.2.13 拓扑图 10.180.2.161 M1 10.180.2.162 M2 10.180.2.200 VIP ...
- 利用LVS+Keepalived搭建Mysql双主复制高可用负载均衡环境
应用背景: MySQL复制(主主,主从...)能在保证数据的备份的同时也能够做读写分离分摊系统压力,但是发生单点故障时,需要手动 切换到另外一台主机.LVS和Keppalived可以设定一个VIP来实 ...
- mysql双主+keepalived【转】
简单原理 1.在两台服务器上分别部署双主keepalived,主keepalived会在当前服务器配置虚拟IP用于mysql对外提供服务 2.在两台服务器上分别部署主主mysql,用于故障切换 3.当 ...
- mysql双主+keepalived
环境 OS: centos7Mysql 版本: mysql 5.7Keepalived: keepalived-1.2.20Mysql-vip:192.168.41.100Mysql-master1: ...
- centos7下mysql双主+keepalived
一.keepalived简介 keepalived是vrrp协议的实现,原生设计目的是为了高可用ipvs服务,keepalived能够配置文件中的定义生成ipvs规则,并能够对各RS的健康状态进行检测 ...
- MySQL 双主+keepalived 详细文档 M-M+keepalived
1. 操作系统,系统环境,目结结构,用户,权限,日志路径,脚本2. 配置规范化 1.1 操作系统准备 操作系统 Kylin Linux release 3.3.1707 (Core)数据库版本 mys ...
随机推荐
- Java.ftp上传下载
1:jar的maven的引用: 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht ...
- Confluence 6 PostgreSQL 输入你的数据库细节
在 Confluence 的设置安装向导中,将会指导你 Confluence 如何连接到你的数据库.请确定选择 "My own database". 使用 JDBC 连接(默认) ...
- Confluence 6 自定义 Decorator 模板的宏和针对高级用户
宏 页面的某些部分使用的是 Velocity 宏进行创建的,包括导航栏.有关宏的创建,你可以参考页面 Working With Decorator Macros 页面中的内容. 针对高级用户 vel ...
- vue指令问题
挂载点:最外层标签就是vue实例的挂载点,即id或者类对应的 dom节点 模板:指挂载点内部的内容,在实例里使用template标签来构 建 h1标签放在body里面不使用 “template”是一样 ...
- 使用tensorflow构造隐语义模型的推荐系统
先创建一个reader.py,后面的程序将用到其中的函数. from __future__ import absolute_import, division, print_function impor ...
- INFO JobScheduler: Added jobs for time 1524468752000 ms/INFO MemoryStore: Block input-0-1524469143000 stored as bytes in memory/完全分布式 ./bin/run-example streaming.NetworkWordCount localhost 9999无法正常运行
1.完全分布式 ./bin/run-example streaming.NetworkWordCount localhost 9999无法正常运行: 1 [hadoop@slaver1 spark- ...
- openpose pytorch代码分析
github: https://github.com/tensorboy/pytorch_Realtime_Multi-Person_Pose_Estimation # -*- coding: utf ...
- CentOS6— Redis安装(转和延续)
Part I. Redis安装(转载部分) 一.安装(官网地址 http://redis.io/download) wget http://download.redis.io/redis-stable ...
- 【bzoj4811】[Ynoi2017]由乃的OJ 树链剖分+线段树区间合并
题解: 好像和noi那题并没有什么区别 只是加上了修改和变成树上 比较显然我们可以用树链剖分来维护
- [转]MyEclipse 2015优化技巧
http://www.chinahadoop.cn/group/16/thread/1660 http://www.bkjia.com/Javabc/1077158.html 只有不断的学习才能使人充 ...