1、mysql主從復制。
服務器信息如下:
| 服務器主機名 | ip | 說明 |
|---|---|---|
| mycat-node | 192.168.1.73 | mycat服務器,連接時使用該服務器 |
| mycat-jessie | 192.168.1.73 | 物理數(shù)據(jù)庫1,作為主庫master |
| mycat-jyp | 192.168.1.52 | 物理數(shù)據(jù)庫2,作為從庫slave |
備注:實驗時,務必關閉關閉機器的iptables防火墻和selinux。
a) 安裝ysql,請自行安裝。
安裝過程此處略...
b) mysql主從復制。
sql1: 登錄用戶名和密碼:root/root
sql2: 登錄用戶名和密碼:root/123456
1.1 主庫master的配置
1)、找到數(shù)據(jù)庫的配置文件
(my.cnf Linux系統(tǒng))或者(my.ini win系統(tǒng))
C:\ProgramData\MySQL\MySQL Server 5.7\my.ini
2)、master配置:
找到mysqld部分,插入如下內容:
# 配置主從:
# 唯一,要與slave的不同。
server-id=1
skip-grant-tables
#(mysql路徑)
basedir = C:\ProgramData\MySQL\MySQL Server 5.7\
#(data路徑,存放日志文件)
datadir = C:\ProgramData\MySQL\MySQL Server 5.7\Data\
# 開啟二進制日志
log-bin = mysql-bin
# 需要同步的數(shù)據(jù)庫。如果是多個同步庫,就以此格式另寫幾行即可。如果不指明對某個具體庫同步,就去掉此行,表示同步所有庫(除了ignore忽略的庫)
binlog-do-db=db_img_0
binlog-do-db=db_img_1
binlog-do-db=db_info_0
binlog-do-db=db_info_1
binlog-do-db=role
binlog-do-db=role_user
binlog-do-db=user
# 表示不需要備份的數(shù)據(jù)庫為mysql
binlog-ignore-db=mysql
3)、配置:
a) 首先重啟mysql服務,
右鍵計算機管理---服務---mysql停止/啟動。
b) 創(chuàng)建用戶:
// CREATE USER ' 使用名 '@' 從庫 IP 地址 ' IDENTIFIED BY ' 密碼 ';
mysql>create user 'ir'@'192.168.1.52' identified by '123456';
c) 分配權限:
// GRANT REPLICATION SLAVE ON . TO ' 使用名 '@' 備機 IP 地址 ';
grant replication slave on *.* to 'ir'@'192.168.1.52';
d) 刷新權限:
mysql> flush privileges;
e) 查看master狀態(tài):
File 的值為: mysql-bin.000013;
Position 的值為: 154;
Binlog_Do_DB 的值為: test;
Binlog_Ignore_DB 的值為: manual,mysql ;
記下 File 、 Position 的值,配置從庫要用到。
1.2 主庫master的配置
1)、master配置:
找到mysqld部分,插入如下內容:
# 配置主從:
# 與主庫不同,
server-id=2
skip-grant-tables
#(mysql文件夾路徑)
basedir = C:\ProgramData\MySQL\MySQL Server 5.7\
#(data路徑,存放日志文件)
datadir = C:\ProgramData\MySQL\MySQL Server 5.7\Data\
# 開啟二進制日志
log-bin = mysql-bin
# 需要同步的數(shù)據(jù)庫。
binlog-do-db=db_img_0
binlog-do-db=db_img_1
binlog-do-db=db_info_0
binlog-do-db=db_info_1
binlog-do-db=role
binlog-do-db=role_user
binlog-do-db=user
# 表示不需要備份的數(shù)據(jù)庫為mysql
binlog-ignore-db=mysql
2)、配置:
a) 首先重啟mysql服務,
右鍵計算機管理---服務---mysql停止/啟動。
b)、登錄上Slave數(shù)據(jù)庫。
用戶名改和密碼:"root 123456"
c)、停止同步。
輸入 "stop slave;"。
d)、重置同步。
輸入"reset slave;"。
e)、更改位置。(與Master查看的日志位置保持一致)
CHANGE MASTER TO
MASTER_HOST='192.168.1.73', # 主機的IP
MASTER_USER='ir', # 之前創(chuàng)建的用戶名
MASTER_PASSWORD='123456', # 之前創(chuàng)建的密碼
MASTER_LOG_FILE='mysql-bin.000013', # 主庫中show master status---File下的那個值
MASTER_LOG_POS=154; # 主庫中show master status---Position下的那個值
f)、啟動同步。
輸入" start slave; "。
g)、查看是否同步成功。
輸入" show slave status\G; "
結果如下表格所示:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
二者均為Yes表示成功啦!
如果slave_SQL_Running提示為NO,請等待10-20分鐘后再次輸入"show slave status\G;"
查看是否同步成功;若仍不成功,請檢查之前操作是否存在問題。
輸出結果如下:
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.73
Master_User: ir
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 154
Relay_Log_File: DESKTOP-6BJDELI-relay-bin.000004
Relay_Log_Pos: 147655155
Relay_Master_Log_File: mysql-bin.000013
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
至此,mysql主從復制,配置完成啦!?。?/strong>
2、mycat的安裝與使用。
2.1.1 安裝jdk
因為 MyCat 是用 Java 開發(fā)的,因此 MyCat 運行需要安裝 JDK(準確來說是 JRE 就夠了),并且需要 JDK1.7 或以上版本。
新建JAVA_HOME
變量值為:C:\Program Files\Java\jdk1.8.0_121
新建classpath
值為:.;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar
編輯path
后追加:%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin
命令行輸入:java -version
C:\Users\sprivacy>java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
安裝成功?。?!
2.1.2 安裝mycat
官網下載地址: http://www.mycat.io/
本文下載版本為1.6
安裝目錄為:
D:\soooooft\mycat\Mycat-server-1.6-RELEASE-20161028204710-win\mycat\
設置環(huán)境變量:
第一步:
新建MYCAT_HOME
變量值為:D:\soooooft\mycat\Mycat-server-1.6-RELEASE-20161028204710-win\mycat(mycat解壓路徑)
第二步:
編輯path
后面追加" %MYCAT_HOME%\bin "
進入mycat---bin目錄下: .\mycat.bat start
PS D:\soooooft\mycat\Mycat-server-1.6-RELEASE-20161028204710-win\mycat\bin> .\mycat.bat start
wrapper | Starting the Mycat-server service...
wrapper | Mycat-server started.
安裝成功。
2.2 配置
2.2.1 修改schema.xml
數(shù)據(jù)庫設置,name與server.xml中schema對應
<schema name="db_im" checkSQLschema="false" sqlMaxLimit="100">
<!-- 拆分寫法 -->
<table name="db_img" dataNode="dn11,dn21,dn31,dn41,dn51,dn61,dn71,dn81,dn91,dn101" rule="mod-long1" />
<table name="db_info" dataNode="dn1,dn2,dn3,dn4,dn5,dn6,dn7,dn8,dn9,dn10" rule="mod-long1" />
<table name="role_user" primaryKey="id" type="gloab" dataNode="dn11"/>
<table name="role" primaryKey="id" type="gloab" dataNode="dn11">
<!-- <childTable name="role_user" joinKey="role_id" parentKey="id"/> -->
</table>
<!-- 不拆分寫法 -->
<table name="user" primaryKey="id" type="gloab" dataNode="dn11"/>
</schema>
分片信息,分庫相關配置
<dataNode name="dn1" dataHost="localhost1" database="db_info_0" />
<dataNode name="dn2" dataHost="localhost1" database="db_info_1" />
<dataNode name="dn3" dataHost="localhost1" database="db_img_0" />
<dataNode name="dn4" dataHost="localhost1" database="db_img_1" />
物理數(shù)據(jù)庫,真正存儲數(shù)據(jù)的數(shù)據(jù)庫,讀寫分離配置:
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<!-- can have multi write hosts -->
<writeHost host="hostM1" url="192.168.1.73:3306" user="root"
password="root">
<!-- can have multi read hosts -->
<readHost host="hostS2" url="192.168.1.52:3306" user="root" password="123456" />
</writeHost>
<!-- <writeHost host="hostS1" url="localhost:3316" user="root"
password="123456" /> -->
<!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
</dataHost>
備注: schema.xml文件中有三點需要注意:balance="1",writeType="0" ,switchType="1"
balance=1:readHost及備用的writeHost參與select的負載均衡。
switchType=2:基于mysql主從同步的狀態(tài)決定是否切換。
hearHbeat:主從切換的心跳語句必須為show slave status;
mycat主從分離只是在讀的時候做了處理,寫入數(shù)據(jù)的時候,只會寫入到writeHost,需要通過mycat的主從復制將數(shù)據(jù)復制到readHost。
如果mysql沒有主從復制配置,mycat在讀寫分離后,數(shù)據(jù)寫入writeHost后,readHost一直沒有數(shù)據(jù)。
2.2.2 修改rule.xml
分庫分表配置還需要配置rule文件:
<tableRule name="mod-long">
<rule>
<columns>id</columns>
<algorithm>mod-long</algorithm>
</rule>
</tableRule>
// 此處需要修改,根據(jù)db_id取mod;如果還有分片的。復制此段代碼繼續(xù)添加。
<tableRule name="mod-long1">
<rule>
<columns>db_id</columns>
<algorithm>mod-long</algorithm>
</rule>
</tableRule>
// 分片的規(guī)則為字符串時:
<tableRule name="mod-long2">
<rule>
<columns>authentication_id</columns>
<algorithm>partition-by-string-hash-function</algorithm>
</rule>
</tableRule>
分片數(shù)量:
<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
<!-- how many data nodes -->
<property name="count">4</property>
</function>
分片規(guī)則為字符串時:
<tableRule name="mod-long3">
<rule>
<columns>client_id</columns>
<algorithm>partition-by-string-hash-function</algorithm>
</rule>
</tableRule>
<tableRule name="mod-long4">
<rule>
<columns>token_id</columns>
<algorithm>partition-by-string-hash-function</algorithm>
</rule>
</tableRule>
規(guī)則為字符串時,需要指定:
<function name="partition-by-string-hash-function" class="io.mycat.route.function.PartitionByString">
<property name="partitionLength">512</property> <!--求?;鶖?shù) zero-based -->
<property name="partitionCount">2</property> <!-- 分片數(shù)量 -->
<property name="hashSlice">0:6</property> <!-- hash預算位 -->
</function>
2.2.3 修改server.xml
server 文件跟讀寫分離關系不大,但是在這里需要該文件配置來連接mycat的用戶及權限。
<user name="root">
<property name="password">root</property>
<property name="schemas">db_im</property>
<!-- 表級 DML 權限設置 -->
<!--
<privileges check="false">
<schema name="db_im" dml="0110" >
<table name="tb01" dml="0000"></table>
<table name="tb02" dml="1111"></table>
</schema>
</privileges>
-->
</user>
<user name="user">
<property name="password">user</property>
<property name="schemas">db_im</property>
<property name="readOnly">true</property>
</user>
備注:
server文件里mycat的登錄用戶名和密碼可以任意設置,該用戶名及密碼是作為登陸mycat時的賬戶信息。我這里配置的是root/root賬戶。
我這里沒有做特殊權限,這些可以自己根據(jù)需求定義。
邏輯庫名(即db_im)也即是登陸mycat后顯示的庫名,登陸后顯示的就是代理的真是的mysql數(shù)據(jù)庫的相關表,要和schema里的相對應。
3、測試
啟動mycat服務
// 進入mycat安裝目錄下的bin文件:輸入 .\mycat.bat start
PS D:\soooooft\mycat\Mycat-server-1.6-RELEASE-20161028204710-win\mycat\bin> .\mycat.bat start
出現(xiàn)以下結果,表示啟動成功:
PS D:\soooooft\mycat\Mycat-server-1.6-RELEASE-20161028204710-win\mycat\bin> .\mycat.bat start
wrapper | Starting the Mycat-server service...
wrapper | Mycat-server started.
navicat 連接mycat:
連接名: test_mycat
ip: 192.168.1.73
端口: 8066 (mycat默認端口)
用戶名:
密碼: root
連接成功,執(zhí)行相關新增、刪除、查詢語句,可以實現(xiàn)讀寫分離相關功能。
停止mycat服務
// 進入bin文件輸入: .\mycat.bat stop
PS D:\soooooft\mycat\Mycat-server-1.6-RELEASE-20161028204710-win\mycat\bin> .\mycat.bat stop
wrapper | Stopping the Mycat-server service...
wrapper | Mycat-server stopped.
停止成功!