keepalived+mysql主主配置手册

实验架构图:

一.mysql 5.5双机热备份 master-master

1.系统环境

操作系统:centos6.6

masterA IP:192.168.166.161

masterB ip:192.168.166.162

应用软件

mysql-5.5.42.tar.gz

keepalived-1.2.12.tar.gz

2.安装mysql

wget http://mirrors.sohu.com/mysql/MySQL-5.5/mysql-5.5.42.tar.gz


#useradd  -r mysql -s /sbin/nologin

#mkdir  -p /data/mysql/{data,binlog,relaylog}

#chown  mysql:mysql -R /data/mysql

#tar xf  mysql-5.5.42.tar.gz

#cd  mysql-5.5.42

# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \

-DMYSQL_DATADIR=/data/mysql/data \

-DMYSQL_DATADIR=/etc \

-DWITH_INNOBASE_STORAGE_ENGINE=1 \

-DWITH_ARCHIVE_STORAGE_ENGINE=1 \

-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \

-DDEFAULT_CHARSET=utf8 \

-DDEFAULT_COLLATION=utf8_general_ci \

-DWITH_ZLIB=system \

-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \

-DWITH_LIBWRAP=0 \

-DWITH_READLINE=1 \

-DSYSCONFDIR=/etc \

-DMYSQL_TCP_PORT=3306

# #会编译不过去,提示需要安装ncurses-devel

#yum -y install ncurses-devel  gcc gcc-c++   bison

#rm -f CMakeCache.txt

重新执行cmake

#make  && make install

#cd /usr/local/mysql

#chown -R mysql:mysql *

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

#chmod +x /etc/init.d/mysqld

#chkconfig -add mysqld

#chkconfig mysqld on

# vim /etc/profile 添加PATH=$PATH:/usr/local/mysql/bin

# . /etc/profile(或者export PATH=$PATH:/usr/local/mysql/bin)

# ln -sv /usr/local/mysql/include/ /usr/include/mysql

# echo ‘/usr/local/mysql/lib‘ >  /etc/ld.so.conf.d/mysql.conf 加载库文件

# ldconfig -v |grep mysql

# vim /etc/man.config 添加一行MANPATH /usr/local/mysql/man

# cd /usr/local/mysql

# scripts/mysql_install_db --user=mysql  --datadir=/data/mysql/data

 

 

 

3.配置master A

将如下配置拷贝到/etc/my.cnf

 


[client]

default-character-set = utf8

port=3306

socket = /tmp/mysql.sock

[mysqld]

user = mysql

port=3306

socket = /tmp/mysql.sock

basedir = /usr/local/mysql

datadir=/data/mysql/data

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

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

#max_connections=1000

#log_slave_update =1

log-bin = /data/mysql/binlog/mysql-bin

log-bin-index = /data/mysql/binlog/mysql-bin.index

binlog_format = mixed

binlog_cache_size = 4M

max_binlog_cache_size = 8M

max_binlog_size = 1G

expire_logs_days = 30

#不需要同步的数据,且不记录到binlog中。

binlog-do-db=small

binlog-ignore-db=mysql

replicate-do-db=small

replicate-ignore-db=mysql

#同步参数:

#保证slave挂在任何一台master上都会接收到另一个master的写入信息

log-slave-updates

slave-skip-errors=all

sync_binlog=1

auto_increment_offset=1

auto_increment_increment=2

key_buffer_size = 384M

sort_buffer_size = 2M

read_buffer_size = 2M

read_rnd_buffer_size =16M

join_buffer_size =2M

thread_cache_size = 300

query_cache_limit = 2M

query_cache_min_res_unit =2K

thread_concurrency = 8

table_cache =614

table_open_cache = 512

open_files_limit = 10240

back_log = 600

max_connections = 5000

max_connect_errors = 6000

external-locking = FALSE

max_allowed_packet =16M

default_storage_engine = MyISAM

#default_storage_engine = InnoDB

thread_stack =192K

transaction_isolation = REPEATABLE-READ

tmp_table_size = 256M

max_heap_table_size =512M

bulk_insert_buffer_size = 64M

myisam_sort_buffer_size = 64M

myisam_max_sort_file_size = 10G

myisam_repair_threads = 1

myisam_recover

long_query_time = 2

slow_query_log = on

slow_query_log_file = /data/mysql/slow.log

log-queries-not-using-indexes =on

log-slow-admin-statements

skip-name-resolve

skip-external_locking

log_bin_trust_function_creators=1

#skip-networking

#skip-innodb

innodb_additional_mem_pool_size = 16M

innodb_buffer_pool_size = 512M

innodb_data_file_path = ibdata1:256M:autoextend

innodb_file_io_threads = 4

innodb_thread_concurrency = 8

innodb_flush_log_at_trx_commit =2

innodb_log_file_size =128M

innodb_log_files_in_group = 3

innodb_max_dirty_pages_pct = 90

innodb_lock_wait_timeout =240

innodb_file_per_table = 0

innodb_status_file = 1

interactive_timeout=120

wait_timeout=120

server-id=1

#innodb_flush_logs_at_trx_commit=1

[mysqldump]

quick

max_allowed_packet = 64M

[mysql]

no-auto-rehash

[myisamchk]

key_buffer_size = 256M

sort_buffer_size = 256M

read_buffer = 2M

write_buffer = 2M

 

 

4.配置 master B

将如下配置拷贝到/etc/my.cnf

 


[client]

default-character-set = utf8

port=3306

socket = /tmp/mysql.sock

[mysqld]

user = mysql

port=3306

socket = /tmp/mysql.sock

basedir = /usr/local/mysql

datadir=/data/mysql/data

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

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

#max_connections=1000

#log_slave_update =1

log-bin = /data/mysql/binlog/mysql-bin

log-bin-index = /data/mysql/binlog/mysql-bin.index

binlog_format = mixed

binlog_cache_size = 4M

max_binlog_cache_size = 8M

max_binlog_size = 1G

expire_logs_days = 30

#需要同步的数据库

binlog-do-db=small

binlog-ignore-db=mysql

replicate-do-db=small

replicate-ignore-db=mysql

log-slave-updates

slave-skip-errors=all

sync_binlog=1

auto_increment_increment=2

auto_increment_offset=2

key_buffer_size = 384M

sort_buffer_size = 2M

read_buffer_size = 2M

read_rnd_buffer_size =16M

join_buffer_size =2M

thread_cache_size = 300

query_cache_limit = 2M

query_cache_min_res_unit =2K

thread_concurrency = 8

table_cache =614

table_open_cache = 512

open_files_limit = 10240

back_log = 600

max_connections = 5000

max_connect_errors = 6000

external-locking = FALSE

max_allowed_packet =16M

default_storage_engine = MyISAM

#default_storage_engine = InnoDB

thread_stack =192K

transaction_isolation = REPEATABLE-READ

tmp_table_size = 256M

max_heap_table_size =512M

bulk_insert_buffer_size = 64M

myisam_sort_buffer_size = 64M

myisam_max_sort_file_size = 10G

myisam_repair_threads = 1

myisam_recover

long_query_time = 2

slow_query_log = on

slow_query_log_file = /data/mysql/slow.log

log-queries-not-using-indexes =on

log-slow-admin-statements

skip-name-resolve

skip-external_locking

log_bin_trust_function_creators=1

#skip-networking

#skip-innodb

innodb_additional_mem_pool_size = 16M

innodb_buffer_pool_size = 512M

innodb_data_file_path = ibdata1:256M:autoextend

innodb_file_io_threads = 4

innodb_thread_concurrency = 8

innodb_flush_log_at_trx_commit =2

innodb_log_file_size =128M

innodb_log_files_in_group = 3

innodb_max_dirty_pages_pct = 90

innodb_lock_wait_timeout =240

innodb_file_per_table = 0

innodb_status_file = 1

interactive_timeout=120

wait_timeout=120

server-id=2

[mysqldump]

quick

max_allowed_packet = 64M

[mysql]

no-auto-rehash

[myisamchk]

key_buffer_size = 256M

sort_buffer_size = 256M

read_buffer = 2M

write_buffer = 2M

 

 

5.创建授权用户

 


masterA:

Mysql>grant replication slave on *.* to  [email protected] identified by ‘123456‘;

Mysql> flush privileges;

masterB:

Mysql>grant replication slave on *.* to  [email protected] identified by ‘123456‘;

Mysql> flush privileges;

 

 

 

6.准备复制


Master A:
 mysql> flush tables with read lock\G
 Query OK, 0 rows affected (0.00 sec)

mysql> show master status\G

*************************** 1. row  ***************************

File: mysql-bin.000001

Position: 347

Binlog_Do_DB: small

Binlog_Ignore_DB: mysql

1 row in set (0.00 sec)

master B:
 mysql> flush tables with read lock;
 Query OK, 0 rows affected (0.00 sec)

mysql> show master status\G

*************************** 1. row  ***************************

File: mysql-bin.000001

Position: 347

Binlog_Do_DB: small

Binlog_Ignore_DB: mysql

1 row in set (0.00 sec)

7.配置同步


Master A:

mysql>   change master to

-> master_host=‘192.168.166.162‘,

-> master_user=‘rpuser2‘,

-> master_password=‘123456‘,

-> master_log_file=‘mysql-bin.000001‘,

->  master_log_pos=347;

Master B:

mysql> change master to

-> master_host=‘192.168.166.161‘,

-> master_user=‘rpuser1‘,

-> master_password=‘123456‘,

-> master_log_file=‘mysql-bin.000001‘,

-> master_log_pos=347;

8.查看与验证


Master A:

Mysql>show slave status \G

Slave_IO_Running: Yes 这两个为yes表示正常

Slave_SQL_Running: Yes

Master B:

Mysql>show slave status \G

Slave_IO_Running: Yes 这两个为yes表示正常

Slave_SQL_Running: Yes

Master A:

创建数据库

create databases small;

Master B:

mysql> show databases;

+--------------------+

| Database            |

+--------------------+

| information_schema |

| mysql               |

| performance_schema |

| small               |

| test               |

+--------------------+

5 rows in set (0.05 sec)

可以发现数据库small

Master B:

在small数据库里创建一张表,T1:

mysql> create table T1 select * from mysql.user;

Query OK, 7 rows affected (0.08 sec)

Records: 7   Duplicates: 0  Warnings: 0

Master A:

查看small下面有T1这个表格:

mysql> use small;

No connection. Trying to reconnect...

Connection id:     10

Current database: *** NONE ***

Database changed

mysql> show tables;

+-----------------+

| Tables_in_small |

+-----------------+

| T1               |

+-----------------+

1 row in set (0.00 sec)

二.配置Keepalived实现MySQL双主高可用

1.下载软件

wget   http://www.keepalived.org/software/keepalived-1.2.12.tar.gz

 

需要在两台服务器上安装keepalived,这里拿Master A安装过程举例


#tar xf keepalived-1.2.12.tar.gz

#cd keepalived-1.2.12

#yum –y install openssl-devel kernel-devel

#./configure --sysconf=/etc  --with-kernel-dir=/usr/src/kernels/2.6.32-504.el6.x86_64/

#make

#make install

 

 

2.配置keepalived


Master A

global_defs {

notification_email  {

[email protected]

}

notification_email_from keepalived.example.com

router_id MySQL_HA

}

vrrp_script check_mysqld {

script "/etc/keepalived/check_slave.pl 127.0.0.1"

interval 2

weight 21

}

vrrp_instance HA_1 {

state BACKUP       //master A和master B上均配置为BACKUP

interface eth0

virtual_router_id  80

priority 100

advert_int 2

nopreempt       //不抢占模式,只在优先级高的机器上设置即可,优先级低的机器可以不设置。

authentication {

auth_type PASS

auth_pass 23b14455cd

}

track_script {

check_mysqld

}

virtual_ipaddress {

192.168.166.254

}

}

其中,/etc/keepalived/check_slave.sh 脚本内容为:

#!/bin/bash

#######################################

# this script function is :

#  check_mysql_slave_replication_status

#

# User YYYY-MM-DD - ACTION

# mlx  2013-12-29 - Created

# mail [email protected]

#######################################

HOST_IP=localhost

HOST_PORT=3306

MYUSER=root

MYPASS="123456"

MYSOCK=/tmp/mysql.sock

MYSQL_PATH=/usr/local/mysql/bin

MYSQL_CMD="$MYSQL_PATH/mysql  -u$MYUSER -p$MYPASS -S $MYSOCK"

CHECKNUM=123

MYSQL1=$($MYSQL_CMD -N -s -e  "select ${CHECKNUM}")

if [ $? -ne 0 ] || [  "${MYSQL1}" -ne "${CHECKNUM}" ];then

/etc/init.d/keepalived stop

exit 1

else

SlaveStatusArr=($($MYSQL_CMD -e  "show slave status \G"|egrep "_Behind|_Running"|awk ‘{print  $NF}‘))

if [ "${SlaveStatusArr[0]}"  = "No" ]  || [  "${SlaveStatusArr[1]}" = "No" ];then

/etc/init.d/keepalived stop

fi

fi

说明:此监控脚本可以检测mysql服务是否启动和mysql主从同步是否正常。

Master B:

global_defs {

notification_email  {

[email protected]

}

notification_email_from keepalived.example.com

router_id MySQL_HA

}

vrrp_script check_mysqld {

script "/etc/keepalived/check_slave.pl 127.0.0.1"

interval 2

weight 21

}

vrrp_instance HA_1 {

state BACKUP       //master A和master B上均配置为BACKUP

interface eth0

virtual_router_id  80

priority 90

advert_int 2

authentication {

auth_type PASS

auth_pass 23b14455cd

}

track_script {

check_mysqld

}

virtual_ipaddress {

192.168.166.254

}

}

其中,/etc/keepalived/check_slave.pl   脚本内容为:

#!/bin/bash

#######################################

# this script function is :

# check_mysql_slave_replication_status

#

# User YYYY-MM-DD - ACTION

# mlx  2013-12-29 -  Created

# mail [email protected]

#######################################

HOST_IP=localhost

HOST_PORT=3306

MYUSER=root

MYPASS="123456"

MYSOCK=/tmp/mysql.sock

MYSQL_PATH=/usr/local/mysql/bin

MYSQL_CMD="$MYSQL_PATH/mysql -u$MYUSER -p$MYPASS -S  $MYSOCK"

CHECKNUM=123

MYSQL1=$($MYSQL_CMD -N -s -e "select  ${CHECKNUM}")

if [ $? -ne 0 ] || [ "${MYSQL1}" -ne  "${CHECKNUM}" ];then

/etc/init.d/keepalived stop

exit 1

else

SlaveStatusArr=($($MYSQL_CMD -e "show slave status \G"|egrep  "_Behind|_Running"|awk ‘{print $NF}‘))

if [  "${SlaveStatusArr[0]}" = "No" ]  || [ "${SlaveStatusArr[1]}" =  "No" ];then

/etc/init.d/keepalived stop

fi

fi

 

 

3.启动keepalived

 


Master A:

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

#service keepalived start

#chkconfig –level 2345 keepalived on

Master B:

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

#service keepalived start

#chkconfig –level 2345 keepalived on

 

 

 

3.测试服务的高可用功能

我们在webserver这台主机上用mysql客户端连接vip:192.168.166.254


#mysql  -uroot -p123456 -h 192.168.166.254

说明:此时vip地址在Master B 这台服务器上。

4.测试故障转移

故障模拟,我们这时手动停掉192.168.166.254的MySQL复制线程


mysql>stop  slave;

这里可以看到,当停掉复制线程后,执行查询时连接中断了一次,马上再次连接上完成查询,显示的server_id已经变成1了,表示服务器已经切换了。

查看master A服务器的IP地址验证VIP是否转移过来了

5.模拟网线故障


手动停掉master A  服务武器eth0网卡

#ifdown eth0

说明:vip地址已经转到Master B服务器上,并且mysql服务已经做了故障转移

5.测试完成

时间: 2024-10-16 14:35:42

keepalived+mysql主主配置手册的相关文章

keepalived+mysql双主

       mysql高可用keepalived+mysql双主 MySQL的高可用方案一般有如下几种: keepalived+双主,MHA,PXC,MMM,Heartbeat+DRBD等,比较常用的是keepalived+双主,MHA和PXC. 本节主要介绍了利用 keepalived 实现 MySQL 数据库的高可用. Keepalived+mysql双主来实现MySQL-HA,我们必须保证两台MySQL数据库的数据完全一样,基本思路是两台MySQL互为主从关系,通过Keepalived配

MySQL 高可用性之keepalived+mysql双主

生产环境中一台mysql主机存在单点故障,所以我们要确保mysql的高可用性,即两台MySQL服务器如果其中有一台MySQL服务器挂掉后,另外一台能立马接替其进行工作. MySQL的高可用方案一般有如下几种: keepalived+双主,MHA,PXC,MMM,Heartbeat+DRBD等,比较常用的是keepalived+双主,MHA和PXC. 这里将主要介绍了利用 keepalived 实现 MySQL数据库的高可用. Keepalived+mysql双主来实现MySQL-HA,我们必须保

MySQL 高可用性——keepalived+mysql双主(有详细步骤和全部配置项解释)

博主QQ:819594300 博客地址:http://zpf666.blog.51cto.com/ 有什么疑问的朋友可以联系博主,博主会帮你们解答,谢谢支持! 前言:生产环境中一台mysql主机存在单点故障,所以我们要确保mysql的高可用性,即两台MySQL服务器如果其中有一台MySQL服务器挂掉后,另外一台能立马接替其进行工作. MySQL的高可用方案一般有如下几种: keepalived+双主,MHA,PXC,MMM,Heartbeat+DRBD等,比较常用的是keepalived+双主,

MySQL 高可用之 keepalived+Mysql 双主双活。

环境描述:[[email protected] ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) [[email protected] ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) IP 规划:MySQL-M 192.168.10.10 MySQL-S 192.168.10.20 配置两服务服务器双主: MySQL 安装: [[ema

keepalived+mysql双主复制高可用方案

MySQL双主复制,即互为Master-Slave(只有一个Master提供写操作),可以实现数据库服务器的热备,但是一个Master宕机后不能实现动态切换.而Keepalived通过虚拟IP,实现了双主对外的统一接口以及自动检查.失败切换机制.联合使用,可以实现MySQL数据库的高可用方案. 实验环境:OS:centos 6.x x86_64系统MySQL版本: :mysql 5.6.22   64 位A: master :192.168.79.3 3306B: slave :192.168.

keepalived + mysql 双主多从

keepalived 的热备可以看文档:keepalived的配置使用 mysql 的主从 的文档讲解:mysql 主从复制 keepalived的双主,就是主从的一种, 大家看一眼就懂了,为了省力, 下面的双主配置我直接网上找了一篇,在下面,大家随便看下 keepalived的双子多从的整体结构 如下图: 1.配置两台Mysql主主同步[[email protected] ~]# yum install mysql-server mysql -y [[email protected] ~]#

keepalived+mysql双主高可用配置

具体架构图如下 两个节点一主一从(从库上面也可以再挂一个从库),或者是双主,再利用keepalived在出现容灾时进行高可用切换. Keepalived原理说明: 其实这个原理可以很简单的用一个故事说明. 以前有一个帮派,像其他所有的帮派一样,有老大.这个老大活的也很憋屈,不仅自己做所有的活,还要隔一段时间就要告诉所有小弟我是老大,你们老实点.所有的小弟也是很乖的,只要老大不死,他们肯定不会抢夺老大的地位,但是一旦他们都收不到老大的通信的话,他们就确信老大死了,他们就开始抢夺老大的地位了.不过他

Keepalived+Mysql(2主2从架构)

利用keepalived构建高可用MySQL-HA,保证两台MySQL数据的一致性,然后用keepalived实现虚拟IP,通过keepalived自带的服务监控功能来实现MySQL故障时自动切换. 使用MySQL双master+keepalived是一种非常好的解决方案,在MySQL-HA环境中,MySQL互为主从关系,这样就保证了两台MySQL数据的一致性,然后用keepalived实现虚拟IP,通过keepalived自带的服务监控功能来实现MySQL故障时自动切换. 下面,我把即将上线的

mysql主主复制及keepalived高可用群集

mysql主主复制及keepalived高可用 Keepalived+mysql双主来实现MySQL-HA,我们必须保证两台MySQL数据库的数据完全一样,基本思路是两台MySQL互为主从关系(双主),通过Keepalived配置虚拟IP,实现当其中的一台MySQL数据库宕机后,应用能够自动切换到另外一台MySQL数据库,保证系统的高可用. 实验基本拓扑: 主主同步就是两台机器互为主从的关系,在任何一台机器上写入都会同步. 在master1 mysql主配置文件中添加下面参数(/etc/my.c

MySQL双主+keepalived实现高可用

mysql+keepalived实现高可用+主主复制模式 为了解决mysql的单点故障问题,衍生出很多mysql的高可用方案: keepalived+双主.MHA.PXC.MMM.Hearbeat+DRBD等,比较常用的一般是keepalived+双主,MHA和PXC 在此搭建实验环境,实现keepalived+mysql双主模式. 实验思路: 两台MySQL互为主从关系(双主),通过keepalived配置虚拟vip,实现当其中的一台MySQL数据库宕机后,应用能自动切换到另外一台MySQL数