mysql系列之 mycat安裝以及配置

一,mycat下載 安裝

我已安裝好了mysql 兩臺節點:【192.168.1.129,192.168.1.131】;
mycat安裝在192.168.1.130節點下

安裝

下載的文件直接解壓即可。

bin目錄腳本直接運行
./mycat start 啓動
./mycat stop 停止
./mycat console 前臺運行
./mycat restart 重啓服務
./mycat pause 暫停
./mycat status 查看啓動狀態

連接測試: mysql -uroot -p123456 -P8066 -h192.168.1.130

也可以用客戶端連接,注意要打開防火牆

 

二,mycat配置文件的配置

1,schema.xml

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
    <!-- test數據庫 -->
	<schema name="test" checkSQLschema="false" sqlMaxLimit="100">
		<table name="store" dataNode="db_store_dataNode" primaryKey="storeID"/>
		<table name="employee" dataNode="db_store_dataNode" primaryKey="employeeID"/>
	</schema>
	
	 <!-- db_user 數據庫 -->
     <!-- data_dictionary表 爲全局表,即所有分片中都會保存一份數據 -->
     <!-- users表 按規則rule="mod-userID-long" 進行分片,詳見rule.xml中的規則配置 -->
     <!-- user_address表 以users表爲父表規 -->
	<schema name="db_user" checkSQLschema="false" sqlMaxLimit="100">
		<table name="data_dictionary" 
               type="global" 
               dataNode="db_user_dataNode1,db_user_dataNode2"     
               primaryKey="dataDictionaryID"/>
		<table name="users" dataNode="db_user_dataNode$1-2"  rule="mod-userID-long" 
                 primaryKey="userID">
			
            <childTable name="user_address"  joinKey="userID" parentKey="userID" 
            primaryKey="addressID"/>
		</table>
	</schema>
	
	
	<!-- 節點配置 -->
	<!-- db_store -->
	<dataNode name="db_store_dataNode" dataHost="db_storeHOST" database="test" />
	
	<!-- db_user -->
	<dataNode name="db_user_dataNode1" dataHost="db_userHOST1" database="db_user" />
	<dataNode name="db_user_dataNode2" dataHost="db_userHOST2" database="db_user" />
	

	<!-- 節點主機配置 -->
	<!-- 配置db_store的節點主機 -->
	<dataHost name="db_storeHOST" 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.129:3306" user="root"  password="123456">
			<!-- can have multi read hosts -->
			<readHost host="hostS1" url="192.168.1.131:3306" user="root" 
            password="123456" />
		</writeHost>
		
	</dataHost>
	
	
	<!-- 配置db_user的節點主機 -->
	<dataHost name="db_userHOST1" maxCon="1000" minCon="10" balance="0"
			  writeType="0" dbType="mysql" dbDriver="native" switchType="1"  
           slaveThreshold="100">
		<heartbeat>select user()</heartbeat>
		<writeHost host="userHost1" url="192.168.1.129:3306" user="root"  
                   password="123456">
		</writeHost>
	</dataHost>
	
	<dataHost name="db_userHOST2" 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="userHost2" url="192.168.1.131:3306" user="root"  
             password="123456">
		</writeHost>
	</dataHost>
	
</mycat:schema>

2.rule.xml

<?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="mod-userID-long">
		<rule>
			<columns>userID</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="mod-long" class="io.mycat.route.function.PartitionByMod">
		<!-- how many data nodes -->
		<property name="count">2</property>
	</function>

	<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="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>

3.server.xml

<?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>
	
	<user name="root">
		<property name="password">123456</property>
		<property name="schemas">test,db_user</property>
	</user>

</mycat:server>

注意,我這裏mycat中  連接mysql的用戶名和密碼分別是 root   123456,要先確保root用戶允許遠程訪問,不然啓動mycat會產生很多無用的連接,可以會提示:

2019-04-09 09:55:43.275  WARN [$_NIOREACTOR-0-RW] (io.mycat.backend.mysql.nio.MySQLConnectionAuthenticator.handle(MySQLConnectionAuthenticator.java:91)) - can't connect to mysql server ,errmsg:Host '192.168.1.130' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts' MySQLConnection [id=421, lastTime=1554774943255, user=root, schema=db_user, old shema=db_user, borrowed=false, fromSlaveDB=false, threadId=0, charset=utf8, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=192.168.1.129, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]

解決方法:

使用mysqladmin flush-hosts 命令清理一下hosts文件(不知道mysqladmin在哪個目錄下可以使用命令查找:whereis mysqladmin);

 在查找到的目錄下使用命令修改:/usr/bin/mysqladmin flush-hosts -h{mysql-host} -P3306 -uroot -p{rootpwd};

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章