mysql的主從複製,前端
參考https://my.oschina.net/u/3647713/blog/1801735node
wget http://dl.mycat.io/1.6-RELEASE/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gzmysql
1,下載linux
首先 cd /usr/local/sql
wget http://dl.mycat.io/1.6-RELEASE/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gzmongodb
2 ,安裝數據庫
tar -zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gzexpress
3 ,啓動mycat apache
/usr/local/mycat/bin/mycat start
session
以前啓動過了 就從新啓動下
[root@zhuangy local]# /usr/local/mycat/bin/mycat
Usage: /usr/local/mycat/bin/mycat { console | start | stop | restart | status | dump }
查看 命令
cat /usr/local/mycat/conf/schema.xml
若是配置成功鏈接mycat就有TESTDB庫和表,這裏給出方便理解
<?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <!-- schema 標籤用於定義 MyCat 實例中的邏輯庫,MyCat 能夠有多個邏輯庫,每一個邏輯庫都有本身的相關配置。可使用 schema 標籤來劃分這些不一樣的邏輯庫。 checkSQLschema False 過濾schema定義。 select * from testdb.company => select * from company;True 不過濾schema定義。有可能報錯大小寫等。 sqlMaxLimit Limit 自動加入limit,會影響最後返回條數。例如:select * from company。--> <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100"> <!-- auto sharding by id (long) 制定Mycat中的邏輯表。最後要作數據分片的表。 dataNode把相應的表存到對應的DB中。rule分片規則。對應 rule.xml中的規則。 type邏輯表的類型。普通表和全局表。name表名 --> <table name="travelrecord" dataNode="dn1,dn2,dn3" rule="auto-sharding-long" /> <!-- global table is auto cloned to all defined data nodes ,so can join with any table whose sharding node is in the same data node --> <table name="company" primaryKey="ID" type="global" dataNode="dn1,dn2,dn3" /> <table name="goods" primaryKey="ID" type="global" dataNode="dn1,dn2" /> <!-- random sharding using mod sharind rule autoIncrement Mycat根據last_insert_id()返回結果。這個須要mysql主鍵設置配合。 --> <table name="hotnews" primaryKey="ID" autoIncrement="true" dataNode="dn1,dn2,dn3" rule="mod-long" /> <!-- needAddLimit 是否自動在每一條SQL語句後面加上limit限制。 <table name="dual" primaryKey="ID" dataNode="dnx,dnoracle2" type="global" needAddLimit="false"/> <table name="worker" primaryKey="ID" dataNode="jdbc_dn1,jdbc_dn2,jdbc_dn3" rule="mod-long" /> --> <table name="employee" primaryKey="ID" dataNode="dn1,dn2" rule="sharding-by-intfile" /> <table name="customer" primaryKey="ID" dataNode="dn1,dn2" rule="sharding-by-intfile"> <childTable name="orders" primaryKey="ID" joinKey="customer_id" parentKey="id"> <childTable name="order_items" joinKey="order_id" parentKey="id" /> </childTable> <childTable name="customer_addr" primaryKey="ID" joinKey="customer_id" parentKey="id" /> </table> <!-- <table name="oc_call" primaryKey="ID" dataNode="dn1$0-743" rule="latest-month-calldate" /> --> </schema> <!-- <dataNode name="dn1$0-743" dataHost="localhost1" database="db$0-743" /> --> <dataNode name="dn1" dataHost="localhost1" database="db1" /> <dataNode name="dn2" dataHost="localhost1" database="db2" /> <dataNode name="dn3" dataHost="localhost1" database="db3" /> <!--<dataNode name="dn4" dataHost="sequoiadb1" database="SAMPLE" /> <dataNode name="jdbc_dn1" dataHost="jdbchost" database="db1" /> <dataNode name="jdbc_dn2" dataHost="jdbchost" database="db2" /> <dataNode name="jdbc_dn3" dataHost="jdbchost" database="db3" /> --> <!--dataHost 主要定義和Mysql集羣有關的信息,數據實例、讀寫分離配置和心跳檢測語句。 balance 負載均衡配置 0 表明不開啓讀寫分離,全部的讀操做都發送到writeHost上。 1 writeHost和readHost都要參與select語句的負載均衡。 雙主雙從模式 M1->S1, M2->S2, M1和M2互爲主備。M2/S1/S2都要參與select語句的負載均衡。 2 全部讀操做都隨機分配給writeHost/readHost 3 全部的讀操做隨機分發到writeHost下面的readHost上執行。 writeType 0 全部的寫操做都分發到第一個writeHost。若是第二個掛了,分發到第二個。 1 全部的寫操做都要隨機分發到全部配置的writeHost上。1.5之後不推薦。 dbType 支持多種db類型。 switchType -1 表明不自動切換 1 默認值,自動切換。 2 基於Mysql主從同步的狀態決定是否切換 show slave status; 3 基於MySQL Galera Cluster切換機制。 show status like ‘wsrep%’; --> <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="172.17.0.4:3306" user="root" password="123456"> <!-- can have multi read hosts --> <readHost host="hostS2" url="172.17.0.2:3306" user="root" password="123456" /> </writeHost> <!-- <writeHost host="hostS1" url="172.17.0.4:3306" user="root" password="123" /> --> </dataHost> <!-- <dataHost name="sequoiadb1" maxCon="1000" minCon="1" balance="0" dbType="sequoiadb" dbDriver="jdbc"> <heartbeat> </heartbeat> <writeHost host="hostM1" url="sequoiadb://1426587161.dbaas.sequoialab.net:11920/SAMPLE" user="jifeng" password="jifeng"></writeHost> </dataHost> <dataHost name="oracle1" maxCon="1000" minCon="1" balance="0" writeType="0" dbType="oracle" dbDriver="jdbc"> <heartbeat>select 1 from dual</heartbeat> <connectionInitSql>alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss'</connectionInitSql> <writeHost host="hostM1" url="jdbc:oracle:thin:@127.0.0.1:1521:nange" user="base" password="123456" > </writeHost> </dataHost> <dataHost name="jdbchost" maxCon="1000" minCon="1" balance="0" writeType="0" dbType="mongodb" dbDriver="jdbc"> <heartbeat>select user()</heartbeat> <writeHost host="hostM" url="mongodb://192.168.0.99/test" user="admin" password="123456" ></writeHost> </dataHost> <dataHost name="sparksql" maxCon="1000" minCon="1" balance="0" dbType="spark" dbDriver="jdbc"> <heartbeat> </heartbeat> <writeHost host="hostM1" url="jdbc:hive2://feng01:10000" user="jifeng" password="jifeng"></writeHost> </dataHost> --> <!-- <dataHost name="jdbchost" maxCon="1000" minCon="10" balance="0" dbType="mysql" dbDriver="jdbc"> <heartbeat>select user()</heartbeat> <writeHost host="hostM1" url="jdbc:mysql://localhost:3306" user="root" password="123456"> </writeHost> </dataHost> --> </mycat:schema>
cat /usr/local/mycat/conf/server.xml
是Mycat最重要的配置文件之一。主要管理Mycat邏輯庫、邏輯表、表、分片規則、Datasource。
<?xml version="1.0" encoding="UTF-8"?> <!-- - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. --> <!DOCTYPE mycat:server SYSTEM "server.dtd"> <mycat:server xmlns:mycat="http://io.mycat/"> <system> <property name="useSqlStat">0</property> <!-- 1爲開啓實時統計、0爲關閉 --> <property name="useGlobleTableCheck">0</property> <!-- 1爲開啓全加班一致性檢測、0爲關閉 --> <property name="sequnceHandlerType">2</property> <!-- <property name="useCompression">1</property>--> <!--1爲開啓mysql壓縮協議--> <!-- <property name="fakeMySQLVersion">5.6.20</property>--> <!--設置模擬的MySQL版本號--> <!-- <property name="processorBufferChunk">40960</property> --> <!-- <property name="processors">1</property> <property name="processorExecutor">32</property> --> <!--默認爲type 0: DirectByteBufferPool | type 1 ByteBufferArena--> <property name="processorBufferPoolType">0</property> <!--默認是65535 64K 用於sql解析時最大文本長度 --> <!--<property name="maxStringLiteralLength">65535</property>--> <!--<property name="sequnceHandlerType">0</property>--> <!--<property name="backSocketNoDelay">1</property>--> <!--<property name="frontSocketNoDelay">1</property>--> <!--<property name="processorExecutor">16</property>--> <!-- <property name="serverPort">8066</property> <property name="managerPort">9066</property> <property name="idleTimeout">300000</property> <property name="bindIp">0.0.0.0</property> <property name="frontWriteQueueSize">4096</property> <property name="processors">32</property> --> <!--分佈式事務開關,0爲不過濾分佈式事務,1爲過濾分佈式事務(若是分佈式事務內只涉及全局表,則不過濾),2爲不過濾分佈式事務,可是記錄分佈式事務日誌--> <property name="handleDistributedTransactions">0</property> <!-- off heap for merge/order/group/limit 1開啓 0關閉 --> <property name="useOffHeapForMerge">1</property> <!-- 單位爲m --> <property name="memoryPageSize">1m</property> <!-- 單位爲k --> <property name="spillsFileBufferSize">1k</property> <property name="useStreamOutput">0</property> <!-- 單位爲m --> <property name="systemReserveMemorySize">384m</property> <!--是否採用zookeeper協調切換 --> <property name="useZKSwitch">true</property> </system> <!-- 全局SQL防火牆設置 --> <!-- <firewall> <whitehost> <host host="127.0.0.1" user="mycat"/> <host host="127.0.0.2" user="mycat"/> </whitehost> <blacklist check="false"> </blacklist> </firewall> --> <user name="root"> <property name="password">123456</property> <property name="schemas">TESTDB</property> <!-- 表級 DML 權限設置 --> <!-- <privileges check="false"> <schema name="TESTDB" 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">TESTDB</property> <property name="readOnly">true</property> </user> </mycat:server>
主要用於管理Mycat的用戶名,權限,黑白名單等等設置。這個文件主要和Mycat Server運行環境有關。
System標籤
屬性 |
說明 |
備註 |
useSqlStat |
開啓實時統計 |
1爲開啓,0爲關閉 |
useGlobleTableCheck |
全局表一致性檢測 |
1爲開啓,0爲關閉 |
sequnceHandlerType |
Mycat全局ID類型 |
0本地文件方式 |
useCompression |
mysql壓縮協議 |
1爲開啓,0爲不開啓 |
fakeMySQLVersion |
假裝的MySQL版本號 |
|
processorBufferChunk |
每次分配Socket Direct Buffer大小 |
默認4096字節 |
processors |
系統可用線程數 |
默認Runtime.getRuntime().availableProcessors()返回值 |
processorExecutor |
NIOProcessor共享businessExecutor線程池大小 |
|
processorBufferPoolType |
每次分配Socket Direct Buffer大小 |
默認是4096個字節 |
maxStringLiteralLength |
sql解析時最大文本長度 |
默認是65535(即64K) |
backSocketNoDelay |
TCP鏈接相關屬性 |
默認值1 |
frontSocketNoDelay |
TCP鏈接相關屬性 |
默認值1 |
serverPort |
指定服務端口 |
默認8066 |
managerPort |
制定管理端口 |
默認9066 |
idleTimeout |
鏈接空閒時間 |
默認30分鐘,單位毫秒 |
bindIp |
Mycat服務監聽的IP地址 |
|
frontWriteQueueSize |
前端鏈接寫隊列長度 |
爲了讓用戶知道是否隊列過長(SQL結果集返回太多)。當超過指定閥值後,會產生一個告警日誌 |
handleDistributedTransactions |
分佈式事務開關 |
0不過濾分佈式事務 |
useOffHeapForMerge |
是否讓Mycat開啓非堆內存 |
1 開啓,0關閉 |
memoryPageSize |
內存分頁大小 |
|
useStreamOutput |
是否使用流輸出 |
|
systemReserveMemorySize |
系統保留內存大小 |
|
useZKSwitch |
是否採用zookeeper協調切換 |
true/false |
Firewall標籤
定義訪問控制策略:如白名單/黑名單
User標籤
定義可訪問mycat的用戶名稱/密碼/是否只讀
Privileges標籤
控制DML:insert update select delete
單獨給select權限:0010
單獨給insert權限:1000
<?xml version="1.0" encoding="UTF-8"?> <!-- - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. --> <!DOCTYPE mycat:rule SYSTEM "rule.dtd"> <mycat:rule xmlns:mycat="http://io.mycat/"> <tableRule name="rule1"> <rule> <columns>id</columns> <algorithm>func1</algorithm> </rule> </tableRule> <tableRule name="rule2"> <rule> <columns>user_id</columns> <algorithm>func1</algorithm> </rule> </tableRule> <tableRule name="sharding-by-intfile"> <rule> <columns>sharding_id</columns> <algorithm>hash-int</algorithm> </rule> </tableRule> <tableRule name="auto-sharding-long"> <rule> <columns>id</columns> <algorithm>rang-long</algorithm> </rule> </tableRule> <tableRule name="mod-long"> <rule> <columns>id</columns> <algorithm>mod-long</algorithm> </rule> </tableRule> <tableRule name="sharding-by-murmur"> <rule> <columns>id</columns> <algorithm>murmur</algorithm> </rule> </tableRule> <tableRule name="crc32slot"> <rule> <columns>id</columns> <algorithm>crc32slot</algorithm> </rule> </tableRule> <tableRule name="sharding-by-month"> <rule> <columns>create_time</columns> <algorithm>partbymonth</algorithm> </rule> </tableRule> <tableRule name="latest-month-calldate"> <rule> <columns>calldate</columns> <algorithm>latestMonth</algorithm> </rule> </tableRule> <tableRule name="auto-sharding-rang-mod"> <rule> <columns>id</columns> <algorithm>rang-mod</algorithm> </rule> </tableRule> <tableRule name="jch"> <rule> <columns>id</columns> <algorithm>jump-consistent-hash</algorithm> </rule> </tableRule> <function name="murmur" class="io.mycat.route.function.PartitionByMurmurHash"> <property name="seed">0</property><!-- 默認是0 --> <property name="count">2</property><!-- 要分片的數據庫節點數量,必須指定,不然無法分片 --> <property name="virtualBucketTimes">160</property><!-- 一個實際的數據庫節點被映射爲這麼多虛擬節點,默認是160倍,也就是虛擬節點數是物理節點數的160倍 --> <!-- <property name="weightMapFile">weightMapFile</property> 節點的權重,沒有指定權重的節點默認是1。以properties文件的格式填寫,以從0開始到count-1的整數值也就是節點索引爲key,以節點權重值爲值。全部權重值必須是正整數,不然以1代替 --> <!-- <property name="bucketMapPath">/etc/mycat/bucketMapPath</property> 用於測試時觀察各物理節點與虛擬節點的分佈狀況,若是指定了這個屬性,會把虛擬節點的murmur hash值與物理節點的映射按行輸出到這個文件,沒有默認值,若是不指定,就不會輸出任何東西 --> </function> <function name="crc32slot" class="io.mycat.route.function.PartitionByCRC32PreSlot"> <property name="count">2</property><!-- 要分片的數據庫節點數量,必須指定,不然無法分片 --> </function> <function name="hash-int" class="io.mycat.route.function.PartitionByFileMap"> <property name="mapFile">partition-hash-int.txt</property> </function> <function name="rang-long" class="io.mycat.route.function.AutoPartitionByLong"> <property name="mapFile">autopartition-long.txt</property> </function> <function name="mod-long" class="io.mycat.route.function.PartitionByMod"> <!-- how many data nodes --> <property name="count">3</property> </function> <function name="func1" class="io.mycat.route.function.PartitionByLong"> <property name="partitionCount">8</property> <property name="partitionLength">128</property> </function> <function name="latestMonth" class="io.mycat.route.function.LatestMonthPartion"> <property name="splitOneDay">24</property> </function> <function name="partbymonth" class="io.mycat.route.function.PartitionByMonth"> <property name="dateFormat">yyyy-MM-dd</property> <property name="sBeginDate">2015-01-01</property> </function> <function name="rang-mod" class="io.mycat.route.function.PartitionByRangeMod"> <property name="mapFile">partition-range-mod.txt</property> </function> <function name="jump-consistent-hash" class="io.mycat.route.function.PartitionByJumpConsistentHash"> <property name="totalBuckets">3</property> </function> </mycat:rule>
定義分片規則策略文件。
tableRule標籤
定義table分片策略
rule標籤
策略定義標籤
columns
對應的分片字段
algorithm標籤
tableRule分片策略對應的function名稱
function標籤
定義分片函數
這裏面須要本身配置
若是隻想簡單的配置,
<writeHost 寫 url="172.17.0.4:3306" 主mysql數據庫地址 ,user="root"主用戶名password="123456"主密碼
< readHost url="172.17.0.2:3306" 從mysql數據庫地址 user="root" 從用戶名password="123456" 從密碼/>
</writeHost >
/usr/local/mycat/bin/mycat start
mysql -uroot -p123456 -hzhuangy -P8066
mysql -uroot(server.xml裏面配置的user name) -p123456(server.xml裏面配置的password)
-hzhuangy(主機地址,mycat安裝地址,能夠是localhost) -P8066 (mycat端口數據端口是8066,管理端口是9066)
<user name="root"> <property name="password">123456</property> <property name="schemas">TESTDB</property> <!-- 表級 DML 權限設置 --> <!-- <privileges check="false"> <schema name="TESTDB" dml="0110" > <table name="tb01" dml="0000"></table> <table name="tb02" dml="1111"></table> </schema> </privileges> --> </user>
cat /etc/hosts 查看綁定地址
show databases;
show tables;
能夠看出就是以前Schema.xml文件中的table表
鏈接主mysql建立db(這裏是mysql的主從複製,
參考https://my.oschina.net/u/3647713/blog/1801735)
create database db1;
create database db2;
create database db2;
use db1;
建立create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int);
show tables;
use db2;
建立create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int);
show tables;
use db3;
建立create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int);
show tables;
這樣的從庫就會自動同步了 ,
cat /usr/local/mycat/conf/auto-sharding-long.txt
查看travelrecord分片規則根據id分片
插入數據
mysql> insert into travelrecord(id,user_id,traveldate,fee,days) values(1,'Victor',20160101,100,10);
Query OK, 1 row affected (0.00 sec)
mysql>insert into travelrecord(id,user_id,traveldate,fee,days) values(1,'Victor',20160101,100,10);
Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
mysql>insert into travelrecord(id,user_id,traveldate,fee,days) values(1,'Victor',20160101,100,10);
Query OK, 1 row affected (0.00 sec)
use db1
select * from travelrecord;能夠看出db1中travelrecord只有一條數據ID=1
use db2;
select * from travelrecord;db2中travelrecord只有一條數據ID=5000001
use db3;
select * from travelrecord;db3中travelrecord只有一條數據ID=10000001