Lvs+Keepalived+Mysql半同步主主复制高可用方案.docx
- 文档编号:28061332
- 上传时间:2023-07-08
- 格式:DOCX
- 页数:16
- 大小:49.21KB
Lvs+Keepalived+Mysql半同步主主复制高可用方案.docx
《Lvs+Keepalived+Mysql半同步主主复制高可用方案.docx》由会员分享,可在线阅读,更多相关《Lvs+Keepalived+Mysql半同步主主复制高可用方案.docx(16页珍藏版)》请在冰豆网上搜索。
Lvs+Keepalived+Mysql半同步主主复制高可用方案
Lvs+Keepalived+Mysql半同步主主复制高可用方案
1.1 方案简介
Lvs+keepalived作为目前比较流行的高可用解决方案,lvs提供负载均衡,keepalived作为故障转移,提高系统的可用性。
但是一般的mysql高可用为了实现mysql数据的一致性,一般都是采用单点写入,本方案采用lvs+keepalived结合mysql5.6的半同步主主复制解决mysql单点问题。
本方案实现的功能是当网络有问题、mysql有问题、服务器宕机、keepalived服务停止后,服务器能自动切换到备用机,当主服务器服务启动起来后会自动切换回来。
1.2 方案架构与说明
APP应用连接数据库URL统一为10.103.124.224,在微信这个系统连接数据库的只有一个app,同一时间只有一个数据库在写并同步到另一个数据库,并且设置keepalived策略为persistence_timeout=2小时,也就是应用一旦连接会保持该连接两个小时,除非该数据库宕机会切换到另一台数据库。
两个小时后会重新选择服务器,其中两台master权重不相等,权重大的相对的被连接机率也大。
1.3 方案优缺点
优点:
✓安装配置简单,实现方便,高可用效率好,可以根据服务与系统的可用性多方面进行切换。
✓可以将写VIP和读VIP分别进行设置,为读写分离做准备。
✓可以在后面添加多个从服务器,并做到负载均衡。
缺点:
✓在当前所用数据库发生宕机而切换数据库时可能会导致当前事务丢失。
1.4 方案实战
1.4.1适用场景
这个方案适用于只有两台数据库服务器并且还没有实现数据库的读写分离的情况,读和写都配置VIP。
这个方案能够便于单台数据库的管理维护以及切换工作。
比如进行大表的表结构更改、数据库的升级等都是非常方便的。
1.4.2实战环境介绍
服务器
IP
VIP
系统
Mysql
Keepalived
Master1
10.103.124.221
10.103.124.224
Redhat664bit
5.6.10
1.2.7
Master2
10.103.124.223
10.103.124.224
Redhat664bit
5.6.10
1.2.7
1.4.3Mysql的安装和配置
在master1、master2服务器都进行安装,先卸载旧版本再安装:
rpm-emysql-server-5.1.52-1.el6_0.1.x86_64--nodeps
rpm-emysql-5.1.52-1.el6_0.1.x86_64
rpm-emysql-libs-5.1.52-1.el6_0.1.x86_64--nodeps
rpm-ivhMySQL-server-5.6.10-1.el6.x86_64.rpm
rpm-ivhMySQL-client-5.6.10-1.el6.x86_64.rpm
rpm-ivhMySQL-shared-5.6.10-1.el6.x86_64.rpm
ln–s/usr/f/etc/f
1.4.4Mysql的主主同步配置
10.103.124.223:
/etc/f配置:
[mysqld]
datadir=/var/lib/mysql
port=3306
user=root
server_id=1
join_buffer_size=1G
skip-external-locking
key_buffer_size=2G
max_allowed_packet=10M
table_open_cache=512
sort_buffer_size=20M
read_buffer_size=20M
read_rnd_buffer_size=32M
myisam_sort_buffer_size=256M
thread_cache_size=512
query_cache_size=1024M
thread_concurrency=128
slow_query_log=ON
slow_launch_time=2
innodb_flush_log_at_timeout=2
max_connections=1024
tmp_table_size=1G
max_heap_table_size=1G
innodb_buffer_pool_size=1G
innodb_additional_mem_pool_size=20M
innodb_log_buffer_size=64M
innodb_flush_log_at_trx_commit=0
innodb_lock_wait_timeout=30
innodb_thread_concurrency=8
default-storage-engine=myisam
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
rpl_semi_sync_master_enabled=1
rpl_semi_sync_master_timeout=3000
rpl_semi_sync_slave_enabled=1
log_bin=/var/lib/mysql/binlog
binlog-do-db=masterslave
binlog-ignore-db=mysql
replicate-do-db=masterslave
binlog_format=ROW
slave-parallel-workers=2
auto_increment_increment=2
auto_increment_offset=1
#skip_slave_start
log_slave_updates
#read_only
expire_logs_days=1
max_binlog_size=1G
gtid_mode=ON
enforce-gtid-consistency=true
relay_log_recovery=1
max_relay_log_size=1G
relay_log_purge=1
slave-skip-errors=all
skip-name-resolve
character_set_server=utf8
[client]
character_set_client=utf8
rpl_semi_sync_master_enabled=1
rpl_semi_sync_master_timeout=3000
rpl_semi_sync_slave_enabled=1
character_set_server=utf8
[client]
character_set_client=utf8
先注释上述5个参数
然后登入mysql
SETPASSWORD=PASSWORD('123456');
INSTALLPLUGINrpl_semi_sync_masterSONAME'semisync_master.so';
连接10.103.124.223数据库后执行:
installpluginrpl_semi_sync_masterSONAME'semisync_master.so';
installpluginrpl_semi_sync_slaveSONAME'semisync_slave.so';
grantreplicationslaveon*.*to'repl'@'10.103.124.221'identifiedby'repl';
FLUSHPRIVILEGES;
changemastertomaster_host='10.103.124.221',master_user='repl',master_password='repl',master_port=3306,master_log_file='binlog.000001',master_log_pos=120;
startslave;
10.103.124.221:
/etc/f配置:
[mysqld]
datadir=/var/lib/mysql
port=3306
user=root
server_id=2
join_buffer_size=1G
skip-external-locking
key_buffer_size=2G
max_allowed_packet=10M
table_open_cache=512
sort_buffer_size=20M
read_buffer_size=20M
read_rnd_buffer_size=32M
myisam_sort_buffer_size=256M
thread_cache_size=512
query_cache_size=1024M
thread_concurrency=128
slow_query_log=ON
slow_launch_time=2
innodb_flush_log_at_timeout=2
max_connections=1024
tmp_table_size=1G
max_heap_table_size=1G
innodb_buffer_pool_size=1G
innodb_additional_mem_pool_size=20M
innodb_log_buffer_size=64M
innodb_flush_log_at_trx_commit=0
innodb_lock_wait_timeout=30
innodb_thread_concurrency=8
default-storage-engine=myisam
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
rpl_semi_sync_master_enabled=1
rpl_semi_sync_master_timeout=3000
rpl_semi_sync_slave_enabled=1
log_bin=/var/lib/mysql/binlog
binlog-do-db=masterslave
binlog-ignore-db=mysql
replicate-do-db=masterslave
binlog_format=ROW
slave-parallel-workers=2
auto_increment_increment=2
auto_increment_offset=2
#skip_slave_start
log_slave_updates
#read_only
expire_logs_days=1
max_binlog_size=1G
gtid_mode=ON
enforce-gtid-consistency=true
relay_log_recovery=1
max_relay_log_size=1G
relay_log_purge=1
slave-skip-errors=all
skip-name-resolve
character_set_server=utf8
[client]
character_set_client=utf8
连接10.103.124.221数据库后执行:
installpluginrpl_semi_sync_masterSONAME'semisync_master.so';
installpluginrpl_semi_sync_slaveSONAME'semisync_slave.so';
grantreplicationslaveon*.*to'repl'@'10.103.124.223'identifiedby'repl';
FLUSHPRIVILEGES;
changemastertomaster_host='10.109.149.7',master_user='repl',master_password='repl',master_port=3306,master_log_file='binlog.000001',master_log_pos=465;
startslave;
serviceiptablesstatus
serviceiptablesstop
chkconfigiptablesoff
清除linux防火墙,让3306能让其他机器连接
1.4.5Lvs的安装和配置
在master1、master2服务器都进行安装:
rpm-ivh ipvsadm-1.25-9.el6.x86_64.rpm
rpm-ivhpopt-devel-1.13-7.el6.x86_64.rpm
chkconfigipvsadmon
vi/usr/local/bin/lvs_real
#!
/bin/bash
#description:
startrealserver
VIP=10.103.124.224
/etc/rc.d/init.d/functions
case"$1"in
start)
echo"startLVSofREALServer"
/sbin/ifconfiglo:
0$VIPbroadcast$VIPnetmask255.255.255.255up
echo"1">/proc/sys/net/ipv4/conf/lo/arp_ignore
echo"2">/proc/sys/net/ipv4/conf/lo/arp_announce
echo"1">/proc/sys/net/ipv4/conf/all/arp_ignore
echo"2">/proc/sys/net/ipv4/conf/all/arp_announce
;;
stop)
/sbin/ifconfiglo:
0down
echo"closeLVSDirectorserver"
echo"0">/proc/sys/net/ipv4/conf/lo/arp_ignore
echo"0">/proc/sys/net/ipv4/conf/lo/arp_announce
echo"0">/proc/sys/net/ipv4/conf/all/arp_ignore
echo"0">/proc/sys/net/ipv4/conf/all/arp_announce
;;
*)
echo"Usage:
$0{start|stop}"
exit1
esac
保存退出,执行以下命令:
chmod+x/etc/rc.d/init.d/functions
chmod+xlvs_real
lvs_realstart
echo`whichlvs_real`>>/etc/rc.local
1.4.6Keepalived的安装
在master1、master2服务器都进行安装:
./configure -prefix=/usr/local/keepalived-1.2.7
make&&makeinstall
ln-s/usr/local/keepalived/etc/rc.d/init.d/keepalived/etc/rc.d/init.d/keepalived
ln-s/usr/local/keepalived/etc/sysconfig/keepalived/etc/sysconfig/keepalived
mkdir/etc/keepalived
ln-s/usr/local/keepalived/sbin/keepalived/usr/sbin/keepalived
1.4.7Keepalived的配置
master1的/etc/keepalived/keepalived.conf配置:
global_defs{
router_idLVS_MASTER
}
vrrp_instanceVI_1{
stateMASTER
interfaceeth4#改成自己网卡的名字
virtual_router_id51
priority150
advert_int1
authentication{
auth_typePASS
auth_pass1111
}
virtual_ipaddress{
10.103.124.224
}
}
virtual_server10.103.124.2243306{
delay_loop6
lb_algorr
lb_kindDR
nat_mask255.255.255.0
persistence_timeout7200
protocolTCP
real_server10.103.124.2233306{
weight1
TCP_CHECK{
connect_timeout3
nb_get_retry3
delay_before_retry3
connect_port3306
}
}
real_server10.103.124.2213306{
weight1
TCP_CHECK{
connect_timeout3
nb_get_retry3
delay_before_retry3
connect_port3306
}
}
}
Master2的/etc/keepalived/keepalived.conf配置:
global_defs{
router_idLVS_MASTER
}
vrrp_instanceVI_1{
stateBACKUP
interfaceeth4#改成自己网卡的名字
virtual_router_id51
priority100
advert_int1
authentication{
auth_typePASS
auth_pass1111
}
virtual_ipaddress{
10.103.124.224
}
}
virtual_server10.103.124.2243306{
delay_loop6
lb_algorr
lb_kindDR
nat_mask255.255.255.0
persistence_timeout7200
protocolTCP
real_server10.103.124.2233306{
weight1
notify_down/root/when_db_down.sh #检测到服务down后执行的脚本
TCP_CHECK{
connect_timeout3
nb_get_retry3
delay_before_retry3
connect_port3306
}
}
real_server10.103.124.2213306{
weight1
notify_down/root/when_db_down.sh #检测到服务down后执行的脚本
TCP_CHECK{
connect_timeout3
nb_get_retry3
delay_before_retry3
connect_port3306
}
}
}
1.4.8 高可用方案测试
方案搭建好以后就要进行全方位的可靠性测试了,看看是否达到了我们的预期效果,大致测试步骤如下:
✓∙停掉master1上的mysql,看看能否自动切换到master2。
✓∙插入master1的数据看能否同步到master2数据库。
✓∙启动master上的mysql,看是否能切换回master。
✓∙启动master上的keepalived,看VIP是否会迁移回master上。
✓∙重启master的系统,看看切换过程是否OK
1.5监控与备份
1.5.1 mysql宕机:
Keepalived在检测到数据库宕机后可以执行指定脚本,上面配置的是/root/when_db_down.sh,其中脚本内容如下:
Sshroot@10.103.124.221‘servicemysqlrestart;’
1.5.2 每5分钟清理mysqlsleep或lock进程:
echo"`date`killingmysqlsleepprocess...">>/app/crontab.log
foridin`mysql-uroot-ppassword,-e"showprocesslist"|grep-i-E'sleep|locked'|awk'{if($6>100){print$1}}'`
do
echo"killingpid$id">>/app/crontab.log
`mysql-uroot-ppassword,-e"kill$id"`
done
crontab添加每5分钟执行:
*/50000/root/kill_sleep_process.sh
1.5.3磁盘每小时检测用量,如超80%则发邮件通知:
a=`df-l|awk-F'''{print$5}'|xargsecho""|awk-F'''{print$2}'`;
a=${a%\%};
if[$a-gt80];then
sendEmail-fmail@-tchembo@-s-u"[Emergency]ServerDiskIsFull"-xumail@-xpPassword-m"Diskspaceisfull!
chembodosth."
fi
crontab添加每1小时执行:
0*/1000/root/check_disk_use.sh
1.5.4数据库大表每天凌晨4点进行归档,备份并删除三个月前的数据:
微信系统中需要进行归档有数据库有:
✓微信用户消息分词日志表:
WX_USER_KEY_WORD;
✓微信用户APP待推送消息表;WX_APP_SEND_POOL;
✓微信用户后端待推送消息表:
WX_SEND_POOL;
✓微信用户推送消息日志表:
WX_SEND_LOG;
✓微信用户消息日志表:
WX_USER_MESSAGE_LOG;
✓微信附件表:
WX_ATTACH_INFO;
归档脚本:
threeMonthBeforeDate=`date-
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Lvs Keepalived Mysql 同步 复制 可用 方案