本篇文章记录博主所负责的一个大型项目最近一次遇到主从失效,从新搭建的整个过程。博主但是干了几个小时才搭建好,可是小伙伴们不要怕,其实搭建主从自己是很是简单的;主要仍是数据量十几个G还原数据库花费太多时间。mysql
首先,由于博主负责的该项目服务器几十台的规模,因此批量操做博主都已经作成了自动化脚本。若是你们要想看脚本请到前面博主的文章查看:大型项目linux自动化版本发布脚本(shell)之tomcat、nginx服务脚本linux
如下是详细步骤:nginx
1、中止web应用,接口服务器,mycatweb
#执行nginx集群下线脚本(主要解决几台nginx转发问题) sh n_downline_ssh.sh #中止全部web服务器tomcat sh t_kill.sh #中止接口服务器tomcat Kill -9 接口服务器tomcat进程号 #中止mycat服务 ./mycat stop
2、备份主数据库sql
#备份命令 nohup mysqldump -uxxx -pxxxxx xxerx_wcc_test > /opt/xxerx/dbbackup/xxerx_20181031.sql 1>myout.out 2>/dev/null & #因为博主主了脚本执行,此处就直接运行脚本备份 sh t_xxmp_database_backup.sh
3、通知项目公司运维部门的人上传sql到几台从数据库所在服务器(人家是内网,速度快)shell
4、把三台数据库主从挂载去掉数据库
#须要先分别登陆几台从库上mysql命令行 stop slave; reset slave all; show slave status;
5、把三台从数据库的xxery_wcc库删掉,从新建立数据库centos
#从库sql命令行执行 drop database if exists `xxerx_wcc`; create database `xxerx_wcc` DEFAULT CHARACTER SET UTF8 COLLATE utf8_general_ci;
6、设置几台从数据库参数tomcat
#设置一下参数来提升sql执行速度 set global net_buffer_length=1000000;(经测试无效) set global max_allowed_packet=1000000000;(经测试无效) //目标数据库(从库),查看参数设置结果 show variables like 'max_allowed_packet'; show variables like 'net_buffer_length'; #上面的设置可能会没生效,可执行下面登陆时设置 mysql -u rxxx -p -e "set global net_buffer_length=1000000; set global max_allowed_packet=1000000000;" #设置关闭一些数据库校验,提升sql执行速度 SET foreign_key_checks = 0; SET UNIQUE_CHECKS = 0; SET AUTOCOMMIT = 0; SET GLOBAL log_bin_trust_function_creators = 1;
7、重启主库,在主库执行grant命令受权从库挂载 mysql命令:show master status; 记录posion和bin块服务器
#将mysql锁住不写数据 flush tables with read lock; #重启mysql service mysql restart; #主数据库配置受权从库的链接权限 grant replication slave on *.* to 'xxx'@'10.x.x.xx7' identified by 'xxxxxxxxxx@xxx~!@'; grant replication slave on *.* to 'xxx'@'10.x.x.xx1' identified by 'xxxxxxxxxx@xxx~!@'; grant replication slave on *.* to 'xxx'@'10.x.x.xx1' identified by 'xxxxxxxxxx@xxx~!@'; #查看mysql pos位置和bin文件块 show master status; #执行结果 +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000094 | 715 | xxery_wcc | mysql,test | | +------------------+----------+--------------+------------------+-------------------+
8、在三台从数据上执行备份的sql文件还原数据库,重启mysql
#mysql> use xxerx_wcc; source /opt/xxerx/xxerx_wcc20181031_214314.sql; --需手动复制函数视图过来(函数、视图同步)
9、将从库校验参数修改回来
SET foreign_key_checks = 1; SET UNIQUE_CHECKS = 1; SET AUTOCOMMIT = 1;
10、在从库上执行命令配置主从,并启动:start slave;
#mysql命令 #关闭从库链接 stop slave; change master to master_host='10.x.x.xxx',master_port=3x06,master_user='xxx',master_password='xxxxxx@xxxx~!@',master_log_file = 'mysql-bin.000094',master_log_pos = 715; #开启从库链接同步 start slave; #在主库中执行主库,检查从库是否挂载成功 show processlist \G; #上一句执行效果 *************************** 1. row *************************** Id: 19 User: root Host: 10.x.x.xxx:52739 db: NULL Command: Binlog Dump Time: 273 State: Master has sent all binlog to slave; waiting for binlog to be updated Info: NULL *************************** 2. row *************************** Id: 20 User: xoox Host: 10.x.x.xxx:xxx06 db: NULL Command: Binlog Dump Time: 158 State: Master has sent all binlog to slave; waiting for binlog to be updated Info: NULL *************************** 3. row *************************** Id: 21 User: xoox Host: 10.x.x.xxx:3x9xx db: NULL Command: Binlog Dump Time: 78 State: Master has sent all binlog to slave; waiting for binlog to be updated Info: NULL *************************** 4. row *************************** Id: 22 User: xoox Host: localhost db: NULL Command: Query Time: 0 State: init Info: show processlist 4 rows in set (0.34 sec)
11、检查从库状态 show slave status. 测试同步是否成功(主库手动加数据看从库是否同步)
#主库执行,解锁主库 unlock tables; #从库执行,查看从库状态 show slave status\G;
12、修改mycat配置,重启Mycat(由于从库坏后,我把链接配置修改回了单库支撑线上业务,此处须要修改回读写分离)
cd /opt/xxxx/mycat/conf/ vi schema.xml cd /opt/xxxx/bin ./mycat start;
#修改前(读写配置同一台主库): <?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://org.opencloudb/"> <schema name="xxerx_wcc" checkSQLschema="false" dataNode="dn1"> </schema> <dataNode name="dn1" dataHost="localhost1" database="xxerx_wcc" /> <dataHost name="localhost1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="2" slaveThreshold="100"> <heartbeat>show slave status</heartbeat> <!-- can have multi master write hosts --> <writeHost host="xilon-centos5" url="10.x.x.xxx:3x06" user="xxx" password="xxxxx@xxx~!@"> <!-- can have multi slave read hosts --> <readHost host="xilon-centos5" url="10.x.x.xxx:3x06" user="xxx" password="xxxxx@xxx~!@" /> </writeHost> </dataHost> </mycat:schema>
#修改后: <?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://org.opencloudb/"> <schema name="xxerx_wcc" checkSQLschema="false" dataNode="dn1"> </schema> <dataNode name="dn1" dataHost="localhost1" database="xxerx_wcc" /> <dataHost name="localhost1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="2" slaveThreshold="100"> <heartbeat>show slave status</heartbeat> <!-- can have multi master write hosts --> <writeHost host="xilon-centos5" url="10.x.x.xxx:3xx6" user="xxx" password="xxxxx@xxx~!@"> <!-- can have multi slave read hosts --> <readHost host="xilon-centos6" url="10.x.x.xxx:3xx6" user="xxx" password="xxxxx@xxx~!@" /> <readHost host="xilon-centos7" url="10.x.x.xxx:3xx6" user="xxx" password="xxxxx@xxx~!@" /> <readHost host="xilon-centos8" url="10.x.x.xxx:3xx6" user="xxx" password="xxxxx@xxx~!@" /> </writeHost> </dataHost> </mycat:schema>
十3、重启web,接口服务器
sh t_restart_delay_new.sh 60s
十4、上线nginx
sh n_go_online_ssh.sh
十5、测试线上业务(完成)
最后总结,mycat主从读写分离特别方便简单。以上是博主本次文章的所有内容,若是你们以为博主的文章还不错,请点赞;若是您对博主其它服务器技术或者博主本人感兴趣,请关注博主博客,而且欢迎随时跟博主沟通交流。