mysql + keepalived 双主高可用

Mysql + keepalived双主高可用集群

 

环境:centos6.4 两台

      服务器1192.168.92.128

      服务器2192.168.92.130

Mysql5.5

关闭防火墙

service iptables stop

Chkconfig iptables off

Setenforce 0

修改/etc/selinux/config

SELINUX=enforcing改为SELINUX=disabled

 两台同时创建mysql用户

groupadd mysql

groupadd mysqluseradd -s /sbin/nologin -g mysql -M mysql

 

 在这里采用的是mysql二进制源码包

直接解压就行,不用编译安装


 

替换/etc/my.cnf文件

建立mysql数据文件目录

chown -R mysql.mysql /usr/local/mysql

/bin/cp support-files/my-small.cnf /etc/my.cnf

mkdir -p /usr/local/mysql/data

 

 安装mysql依赖包libaio-devel

yum -y install libaio-devel

 

初始化

mysql/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql

 

 拷贝mysql启动脚本

cp support-files/mysql.server /etc/init.d/mysqld

chmod +x /etc/init.d/mysqld

 

 设置mysql开机启动

/etc/init.d/mysqld start 命令放到/etc/rc.local文件里

设置mysql环境变量

vim /etc/profile

添加export PATH=$PATH:/usr/local/mysql/bin

运行source /etc/profile

mysql + keepalived 双主高可用_第1张图片 

 

设置mysql数据库root的登录密码

mysqladmin -u root password 'admin'

 mysql + keepalived 双主高可用_第2张图片

 

修改两台的mysql配置文件/etc/my.cnf

两台的mysql均要开启binlog日志功能

把其中一台配置文件里的server-id  = 1改为server-id = 2

Master1配置

log-bin = mysql-bin

binlog_format = mixed

server-id = 1

relay-log = relay-bin

relay-log-index = slave-relay-bin.index

auto-increment-increment = 2

auto-increment-offset = 1

 

重启mysql

Service mysqld restart

Master2配置

log-bin = mysql-bin

binlog_format = mixed

server-id = 2

relay-log = relay-bin

relay-log-index = slave-relay-bin.index

auto-increment-increment = 2

auto-increment-offset = 2

 

重启mysql

Service mysqld restart

 mysql + keepalived 双主高可用_第3张图片

 192.168.92.128服务器新建一个用户赋予“REPLICATION SLAVE”的权限

GRANT  REPLICATION SLAVE ON *.* TO 'replication'@'192.168.92.130' IDENTIFIED  BY 'replication'; 

刷新权限

flush  privileges; 

查看下log bin日志和pos值位置

Show master status;

 

 mysql> change  master to

    -> master_host='192.168.92.130',

    -> master_user='replication',

    -> master_password='replication',

    -> master_log_file='mysql-bin.000003',

-> master_log_pos=107;对端状态显示值

 

如果这里执行失败可以退出mysql

/usr/local/mysql/data/下有两个文件可能被占用

relay-log.info

master.info

把这两个文件删除

重新进入mysql执行

 

执行完后启动

start slave;

show slave status\G

查看IOSQL

出现两个YES成功

mysql + keepalived 双主高可用_第4张图片 

 做下测试

master1创建表用户

 mysql + keepalived 双主高可用_第5张图片

 

master2查看数据同步过来没有

 mysql + keepalived 双主高可用_第6张图片

 

然后两台安装keepalived

Yum -y install keeplived

添加系统服务

chkconfig keepalived --add

chkconfig keepalived on

 

然后编辑两台keepalived配置

vim /etc/keepalived/keepalived.conf

修改IP smtp_server   127.0.0.1

标识,双主相同  mysql_ha

两台都是BACKUP

两个服务器id相同

id主的100,从的低于100就行

VIP两台相同

检测本地mysql,backup也要写检测

mysq服down时,执行此脚本,杀死keepalived


 

 mysql + keepalived 双主高可用_第7张图片

 mysql + keepalived 双主高可用_第8张图片

 vim /etc/keepalived/mysql.sh

编辑

#!/bin/bash 
pkill keepalived

 

提升权限

chmod +x /etc/keepalived/mysql.sh

做下测试

停掉主服务192.168.92.128这台mysql服务

Service mysqld stop

VIP还在这台服务器上没

 mysql + keepalived 双主高可用_第9张图片

然后查看下192.168.92.130这台服务的IP

 

你可能感兴趣的:(mysql + keepalived 双主高可用)