大型項目mysql5.6主從讀寫分離搭建(一主三從)

    本篇文章記錄博主所負責的一個大型項目最近一次遇到主從失效,從新搭建的整個過程。博主但是幹了幾個小時才搭建好,可是小夥伴們不要怕,其實搭建主從自己是很是簡單的;主要仍是數據量十幾個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主從讀寫分離特別方便簡單。以上是博主本次文章的所有內容,若是你們以爲博主的文章還不錯,請點贊;若是您對博主其它服務器技術或者博主本人感興趣,請關注博主博客,而且歡迎隨時跟博主溝通交流。

相關文章
相關標籤/搜索