博客分享的第一篇技术文章;项目主要搭建:主主数据库高可用集群搭建。数据库互为主备,应用技术:MM+keepalived使用的是虚拟机搭建的实验向大家展示:数据库1:192.168.4.7数据库2:192.168.4.77VIP:192.168.4.68web1:192.168.4.69web2:192.168.4.70一、安装mysql,部署主主同步结构。直接yum安装配置主主同步:由于主数据库192.168.4.7里面存放着数据,所以需要先导出数据,方法很多,我们采取mysqldump:#mysqldump-uroot-p123456--all-databases>/opt/all.sql然后需要将数据拷贝到数据库192.168.4.77。数据库2:1、修改配置文件:数据库192.168.4.7[mysqld]server_id=7#两台主机不能重复log-bin=master7#开启binlog功能log-bin-index=master7.indexdatadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sockbinlog_format="mixed"数据库192.168.4.77[mysqld]#vim/etc/my.cnf[mysqld]log-bin=master77server_id=77binlog_format="mixed"log_slave_updatesdatadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sock:wq依次启动两台数据库:systemctlstartmariadb2、导入数据库:将数据库192.168.4.7备份出来的数据库导入到数据库192.168.4.77,以此保持两端数据一致mysql-uroot-p123456<all.sql3、配置用户授权并查看master信息:数据库192.168.4.7给用户授权:允许192.168.4.77的数据库用户slave拥有replicationslave权限:mysql>grantreplicationslaveon*.*toslave@"192.168.4.77"identifiedby"123456";mysql>showmasterstatus;//查看状态;数据库192.168.4.77给用户授权:允许192.168.4.7的数据库用户slave拥有replicationslave权限:mysql>grantreplicationslaveon*.*toslave@"192.168.4.7"identifiedby"123456";mysql>showmasterstatus;//查看状态;4、配置相互主从,并开启slave模式:数据库192.168.4.7#mysql-uroot-p123456mysql>changemastertomaster_host="192.168.4.77",master_user="slave",master_password="123456",master_log_file="master77.000001",master_log_pos=357;mysql>startslave;//启动slavemysql>showslavestatus\G;//查看slave状态数据库192.168.4.77mysql>changemastertomaster_host="192.168.4.7",master_user="slave",master_password="123456",master_log_file="master7.000001",master_log_pos=357;mysql>startslave;mysql>showslavestatus\G;//查看slave状态状态都为yes是正常,否则主主同步异常,需要进一步排查Slave_IO_Running:YesSlave_SQL_Running:Yes主主同步配置完成后可以进行一些建库,建表的测试测试,看看两边的数据是否一致二、安装keepalived并设置监控keepalived是安装在两台MySQL服务器上的首先安装keepalived过程不解释就正常解压安装就好1、修改配置文件/etc/keepalived/keepalived.conf!ConfigurationFileforkeepalivedglobal_defs{notification_email{acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_fromAlexandre.Cassen@firewall.locsmtp_server192.168.200.1smtp_connect_timeout30router_idLVS_DEVEL}vrrp_scriptcheck_mysql{script"/opt/mysql.sh"//监控脚本存放位置(最后有参考脚本,也可以自己写)interval2//(检测脚本执行的间隔)weight2}vrrp_instanceVI_1{stateMASTER//备份服务器上将MASTER改为BACKUPinterfaceeth0virtual_router_id51//主、备机的virtual_router_id必须相同priority100//主、备机取不同的优先级,主机值较大,备份机值较小,备份服务器设置为99advert_int1authentication{auth_typePASSauth_pass1111//主、备机一样}track_script{check_mysql//检测脚本}virtual_ipaddress{192.168.4.68//VIP(虚拟IP,对外提供服务的IP)}}数据库192.168.4.77配置文件:vim/etc/keepalived/keepalived.conf!ConfigurationFileforkeepalivedglobal_defs{notification_email{acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_fromAlexandre.Cassen@firewall.locsmtp_server192.168.200.1smtp_connect_timeout30router_idLVS_DEVEL}vrrp_scriptcheck_mysql{script"/opt/mysql.sh"interval2weight2}vrrp_instanceVI_1{stateBACKUPinterfaceeth0virtual_router_id51priority90advert_int1authentication{auth_typePASSauth_pass1111}track_script{check_mysql}virtual_ipaddress{192.168.4.68}}******************************************************************************监控脚本的作用是:实现keepalived主备的切换附:监控脚本vim/opt/mysql.sh#!/bin/basha=`ps-Cmysqld--no-header|wc-l`if[$a-eq0];thensystemctlstartmariadbsleep3b=`ps-Cmysqld--no-header|wc-l`if[$b-eq0];thenkillallkeepalivedfifi