MyCat读写分离基础配置笔记

参考MyCat简单使用与配置

schema.xml基本配置

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
    <!-- name	逻辑数据库名,与server.xml中的schema对应 -->
	<schema name="MyDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"></schema>
	<!-- 
	   name	定义数据节点的名字,这个名字需要唯一。我们在table标签上用这个名字来建立表与分片对应的关系
	   dataHost	用于定义该分片属于哪个数据库实例,属性与datahost标签上定义的name对应
       database	用于定义该分片属于数据库实例上 的具体库。
	-->
	<dataNode name="dn1" dataHost="localhost1" database="crm_bjshy" />

	<!-- balance="3":所有读请求随机的分发到writeHst对应的readHost执行,writeHost不负担读写压力。 -->
	<dataHost name="localhost1" maxCon="1000" minCon="10" balance="3" 
	   writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
	    <!-- mysql 心跳检查 -->
		<heartbeat>select user()</heartbeat>		
		<!-- 主 -->
		<writeHost host="hostM1" url="192.168.3.104:3306" user="commentUser" password="emans_My0624">
		    <!-- 从 -->
			<readHost host="hostS2" url="192.168.3.106:3306" user="commentUser" password="emans_My0624" />
		</writeHost>
	</dataHost>
</mycat:schema>

server.xml配置(基本未改动,只添加了用户)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://io.mycat/">
	<system>
	<property name="useSqlStat">1</property>  <!-- 1为开启实时统计、0为关闭 -->
	<property name="useGlobleTableCheck">0</property>  <!-- 1为开启全加班一致性检测、0为关闭 -->
		<property name="sequnceHandlerType">2</property>
		<property name="processorBufferPoolType">0</property>
		<!-- 端口-->
		<property name="serverPort">8066</property>
		<!-- 监听ip -->
		<property name="bindIp">0.0.0.0</property> 
		<property name="handleDistributedTransactions">0</property>
		<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">false</property>
	</system>

	<!-- 全局SQL防火墙设置
	<firewall> 
	   <whitehost>
	      <host host="127.0.0.1" user="mycat"/>
	      <host host="192.168.3.56" user="mycat"/>
	      <host host="192.168.3.104" user="mycat"/>
	   </whitehost>
       <blacklist check="false">
       </blacklist>
	</firewall>
	 -->
	<user name="mycat">
		<property name="password">emans</property>
		<property name="schemas">MyDB</property>
	</user>
	

	<!-- 只读用户
	<user name="user">
		<property name="password">user</property>
		<property name="schemas">TESTDB</property>
		<property name="readOnly">true</property>
	</user>-->

</mycat:server>

root登录测试

[root@emans conf]# mysql -uroot -p -h127.0.0.1 -P8066
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.29-mycat-1.6-RELEASE-20161028204710 MyCat Server (OpenCloundDB)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

读写分离测试

  [读写分离测试](https://blog.csdn.net/wll_1017/article/details/82427608)
<!-- 主 -->
<writeHost host="hostM1" url="192.168.3.104:3306" user="commentUser" password="emans_My0624">
    <!-- 从 -->
	<readHost host="hostS2" url="192.168.3.106:3306" user="commentUser" password="emans_My0624" />
</writeHost>
104主机负责写操作,106从从负责度操作(主从配置)
1.在104上新建一张测试表.
mysql> create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int);
Query OK, 0 rows affected (0.02 sec)

2.104上插入一条数据

mysql> insert into travelrecord(id,user_id,traveldate,fee,days)  values(1,@@port,20160101,100,10);
Query OK, 1 row affected (0.00 sec)

mysql> select * from travelrecord;
+----+---------+------------+------+------+
| id | user_id | traveldate | fee  | days |
+----+---------+------------+------+------+
|  1 | 3306    | 2016-01-01 |  100 |   10 |
+----+---------+------------+------+------+
1 row in set (0.00 sec)

3.在106上插入一条数据

mysql> insert into travelrecord(id,user_id,traveldate,fee,days)  values(2,@@port,20160202,100,10);
Query OK, 1 row affected (0.00 sec)

mysql> select * from travelrecord;
+----+---------+------------+------+------+
| id | user_id | traveldate | fee  | days |
+----+---------+------------+------+------+
|  1 | 3306    | 2016-01-01 |  100 |   10 |
|  2 | 3306    | 2016-02-02 |  100 |   10 |
+----+---------+------------+------+------+
2 rows in set (0.00 sec)

4.最后使用mycat账号登录,执行查询,发现查询到的数据一直是2条(只读库分配在106上,配置成功)

[root@bogon ~]# mysql -umycat -pemans -h192.168.3.106 -P8066
mysql> use MyDB
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql>  select * from travelrecord;
+----+---------+------------+------+------+
| id | user_id | traveldate | fee  | days |
+----+---------+------------+------+------+
|  1 | 3306    | 2016-01-01 |  100 |   10 |
|  2 | 3306    | 2016-02-02 |  100 |   10 |
+----+---------+------------+------+------+
2 rows in set (0.00 sec

注意:

在此配置下:
1.如果此时将106只读库(从库)关闭;经测试mycat会自动将读取请求转到104写库上,并且在106只读库启动后,读取请求会自动转到106读库上,不再从104写库上获取数据
2..如果此时将104写库(主库)关闭;mycat账号再连接上述MyDB的逻辑库时报错连接失败
<!-- balance="3":所有读请求随机的分发到writeHst对应的readHost执行,writeHost不负担读写压力。 -->
	<dataHost name="localhost1" maxCon="1000" minCon="10" balance="3" 
	   writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">

3.防火墙,实测坑(也可能是自己某个地方配置错误)

<!-- 全局SQL防火墙设置 -->
	<firewall> 
	   <whitehost>
	      <!-- 将127.0.0.1的ip加入白名单配置,暂测发现要么写全要么不配置. -->
	      <host host="127.0.0.*" user="mycat"/>    <!--  错误-->
	      <host host="127.0.*.*" user="mycat"/>    <!--  错误-->
	      <host host="127.0.*"   user="mycat"/>    <!--  错误-->
	      <host host="127.0.0.1" user="mycat"/>    <!--  正确-->
	   </whitehost>
       <blacklist check="false">
       </blacklist>
	</firewall> 

猜你喜欢

转载自blog.csdn.net/qq_34769161/article/details/88702316
今日推荐