1. 程式人生 > >mycat安裝及入門(windows下安裝)

mycat安裝及入門(windows下安裝)

一、安裝及啟動:

1.安裝

切換到mycat 目錄

mycat.bat install

2.啟動

mycat.bat start

3.查詢執行狀態

mycat.bat status

4.停止服務

mycat.bat stop

二、配置檔案介紹

     2.1 Mycat主要的配置檔案:

 --server.xml:是Mycat伺服器引數調整和使用者授權的配置檔案。

  --schema.xml:是邏輯庫定義和表以及分片定義的配置檔案。

  --rule.xml:  是分片規則的配置檔案,分片規則的具體一些引數資訊單獨存放為檔案,也在這個目錄下,配
                置檔案修改需要重啟MyCAT。

  --log4j.xml: 日誌存放在logs/log中,每天一個檔案,日誌的配置是在conf/log4j.xml中,根據自己的
               需要可以調整輸出級別為debug,debug級別下,會輸出更多的資訊,方便排查問題。

  --autopartition-long.txt,partition-hash-int.txt,sequence_conf.properties,
                   sequence_db_conf.properties 分片相關的id分片規則配置檔案

  --lib	    MyCAT自身的jar包或依賴的jar包的存放目錄。

  --logs    MyCAT日誌的存放目錄。日誌存放在logs/log中,每天一個檔案

其中:rule.xml、schema.xml、server.xml

  

      2.2  bserver.xml配置

<system>

	<property name="defaultSqlParser">druidparser</property>

</system>

<user name="mycat">

	<property name="password">mycat</property>

	<property name="schemas">user,pay</property>

</user>

新增兩個mycat邏輯庫:user,pay:

 system 引數是所有的mycat引數配置,比如新增解析器:defaultSqlParser,其他類推 user 是使用者引數。

2.3 schema.xml配置檔案

<schema name="user" checkSQLschema="false" sqlMaxLimit="100" dataNode="user" />
<schema name="pay"  checkSQLschema="false" sqlMaxLimit="100" dataNode="pay" >
   <table name="order" dataNode="pay1,pay2" rule="rule1"/>
</schema>

<dataNode name="user" dataHost="host" database="user" />
<dataNode name="pay1" dataHost="host" database="pay1" />
<dataNode name="pay2" dataHost="host" database="pay2" />

<dataHost name="host" maxCon="1000" minCon="10" balance="0"
   writeType="0" dbType="mysql" dbDriver="native">
   <heartbeat>select 1</heartbeat>
   <!-- can have multi write hosts -->
   <writeHost host="hostM1" url="192.168.0.2:3306" user="root" password="root" />
   <writeHost host="hostM2" url="192.168.0.3:3306" user="root" password="root" />
</dataHost>

schema: 實際邏輯庫的配置,user,pay分別對應兩個邏輯庫,多個schema代表多個邏輯庫。

dataNode:邏輯庫對應的分片,如果配置多個分片只需要多個dataNode即可。

dataHost:實際的物理庫配置地址,可以配置多主主從等其他配置,多個dataHost代表分片對應的物理庫地址,上面的

                   writeHost、readHost代表該分片是否配置多寫,主從,讀寫分離等高階特性。

三、測試Mycat

      3.1 修改schem.xml

<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
		<!-- auto sharding by id (long) -->
		<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 -->
		<table name="hotnews" primaryKey="ID" autoIncrement="true" dataNode="dn1,dn2,dn3"
			   rule="mod-long" />
		<!-- <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>

<dataHost name="localhost" maxCon="1000" minCon="10" balance="0"
			  writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
		<heartbeat>select user()</heartbeat>
		<!-- can have multi write hosts -->
		<writeHost host="hostM1" url="localhost:3306" user="root" //資料庫的使用者名稱
				   password="yutian">//資料庫的密碼
			<!-- can have multi read hosts -->
			<readHost host="hostS2" url="192.168.1.200:3306" user="root" password="xxx" />
		</writeHost>
		<!-- 
		<writeHost host="hostS1" url="localhost:3316" user="root"
				   password="123456" />
				   -->
		<!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
	</dataHost>

 3.2 修改server.xml

<user name="root">
		<property name="password">yutian</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>

3.2 連線Mycat

mysql -uroot -pyutian -P8066

3.3 檢視庫表

mysql>  show databases;
+----------+
| DATABASE |
+----------+
| TESTDB   |
+----------+
1 row in set (0.11 sec)

mysql> use TESTDB;
Database changed
mysql> show tables;
+------------------+
| Tables in TESTDB |
+------------------+
| company          |
| customer         |
| customer_addr    |
| employee         |
| goods            |
| hotnews          |
| orders           |
| order_items      |
| travelrecord     |
+------------------+
9 rows in set (0.10 sec)

mysql>

說明Mycat安裝成功