实现无人工干预下mysqlz中mm自动切换,s自动寻找存活的m同步数据,图示如下: 1.正常情况下,写只有一个服务器在提供服务,另外
实现无人工干预下mysqlz中mm自动切换,s自动寻找存活的m同步数据,,
图示如下:
1.正常情况下,写只有一个服务器在提供服务,另外一个备份,中间通过keepalived实现
2.只读服务器,可以多台服务器同时提供服务,也可以只有一台提供服务,keepalived都可以实现,我这边目前写的主要是一台提供服务。
环境搭建步骤:
1.给四台服务器安装mysql,keepalived。(版本一致)
2.master1中mysql,keepalived配置:
cnfport=3306socketsockport=3306socketsockdatadirskip_name_resolveskiplockingkey_buffer_size =384mmax_allowed_packet =1mtable_open_cache =512sort_buffer_size =2mread_buffer_size =2mread_rnd_buffer_size =8mmyisam_sort_buffer_size =64mthread_cache_size =8query_cache_size =32mthread_concurrency =8max_connections =1000log_bin_trust_function_creators=1transaction_isolationcommittedslaveallreplimydbreplicatemysqlreplicatems_statelogbinserverbinlog_format=rowinnodb_buffer_pool_size quickmax_allowed_packet rehashkey_buffer_size =256msort_buffer_size =256mread_buffer =2mwrite_buffer interactive-timeoutconf keepalivedglobal_defs {notification_email {acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from locsmtp_server xxxxsmtp_connect_timeout 30router_id lvs_devel}vrrp_script check_run {script /opt/keepalived_check_mysql.shinterval 5}vrrp_sync_group vg1 vi_1}}vrrp_instance vi_1 {state masterinterface eth0virtual_router_id 51priority 100advert_int 1authentication {auth_type passauth_pass 1111}track_script {check_run}virtual_ipaddress sh#!/bin/bashmysqlmysqlmysql_hostmysql_user=rootmysql_password=123456check_timemysql_ok$mysql mysql_ok=1echo mysql is goodelsemysql_ok=0echo $mysql_okcheck_mysql_helthcheck_timekeepalived stopsleep 1done
3.上面三个配置文件my.cnf文件每个机器都要server-id = 11此参数修改不一样即可
keepalived.conf 其中slave1服务器跟master1一样,其他两台去掉script /opt/keepalived_check_mysql.sh
vip也需要修改
4.mysql建立同步账号:
在master1服务器上:
grant all privileges on grant all privileges on flush privileges;嫌麻烦就直接给整个网段授权:
在master2上同样运行
5.slave1,slave2上建立同步
stop slave;change master to master_hoststart slave;注意: master_log_file,master_log_pos参数是在master1上show master status;查到的。
6.slave1,slave2上检测脚本
checkmysql.sh :m1,m2状态检测
changenode.sh:slave对应远程master服务器修改以及数据同步。
checkstatus.sh :修改master后,停止再次修改,避免数据丢失。
sh#!/bin/bashcdr=/opt/shellcd $cdrecho echo logfireturn $node01_mysql_okecho echo logfireturn $node02_mysql_ok}check_timeecho logcheck_node01_mysql_helthcheck_timeecho logsh $cdrsh $nodeip02#exit 1fisleep 1donecheck_timecheck_node02_mysql_helthcheck_timeecho logsh $cdrsh $nodeip01sleep 1done修改master脚本
sh#!/bin/bash#change another master mysqlcdr=/opt/shellcd $cdr