关于分库分表,Mycat已经帮我们在内部实现了路由的功能,我们只需要在Mycat中配置以下切分规则即可,对于开发者来说,我们就可以把Mycat看做是一个数据库,接下来我们开始搭建环境
1、准备:
Mycat是使用java写的数据库中间件,所以要运行Mycat前要准备要jdk的环境,要求是jdk1.7以上的环境。所以需要在系统中配置JAVA_HOME的环境变量,如果没有配置好启动Mycat会是这样的提示:
[root@LinuxCentOS6 bin]# ./mycat startJAVA_HOME environment variable is not set[root@LinuxCentOS6 bin]#
正确安装JDK:【运维笔记】Linux 安装 jdk1.8.0_191 指南
2、下载MyCat安装包:
从官网下载Mycat,http://dl.mycat.io/1.6-RELEASE/ 我们是基于CentOS6.5
来搭建Mycat环境的,所以下载版本:
Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
3、将下载好的安装包上传到服务器上并解压.解压之后目录结构如下:
[root@LinuxCentOS6 mycat]# pwd
/usr/local/mycat
[root@LinuxCentOS6 mycat]# ll
total 24
drwxr-xr-x. 2 root root 4096 Oct 30 12:04 bin
drwxrwxrwx. 2 root root 4096 Mar 1 2016 catlet
drwxrwxrwx. 4 root root 4096 Oct 30 13:55 conf
drwxr-xr-x. 2 root root 4096 Oct 30 12:04 lib
drwxrwxrwx. 3 root root 4096 Oct 30 13:55 logs
-rwxrwxrwx. 1 root root 217 Oct 28 2016 version.txt
[root@LinuxCentOS6 mycat]#
4、配置名为db_sszh_mycat
的逻辑库
信息,将如下配置复制粘贴覆盖mycat/conf/schema.xml
的内容:
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/"><!-- 配置server.xml将会用到db_sszh_mycat,这是对外提供逻辑的数据库,rule1在rule.xml中配置 --> <schema name="db_sszh_mycat" checkSQLschema="false" sqlMaxLimit="100"><table name="user" primaryKey="id" dataNode="db_sszh_01,db_sszh_02" rule="rule1" /> </schema><!-- 设置dataNode 对应的数据库信息,及 mycat 连接的地址dataHost,database是真实的物理数据库名称 --> <dataNode name="db_sszh_01" dataHost="db_sszh" database="db_sszh_01" /> <dataNode name="db_sszh_02" dataHost="db_sszh" database="db_sszh_02" /> <!-- mycat 逻辑主机dataHost对应的物理主机.其中也设置对应的mysql登陆信息 --> <dataHost name="db_sszh" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native"> <heartbeat>select user()</heartbeat> <writeHost host="server1" url="192.168.10.88:3306" user="root" password="123456"/> </dataHost>
</mycat:schema>
说明:
<schema>
:表示的是在mycat中的逻辑库配置,逻辑库名称为:db_sszh_mycat
<table>
:表示在mycat中的逻辑表配置,逻辑表名称为:user
,映射到两个数据库节点dataNode
中,切分规则为:rule1
(在rule.xml配置)<dataNode>
:表示数据库节点,这个节点不一定是单节点,可以配置成读写分离<dataHost>
:真实的数据库的地址配置<heartbeat>
:用户心跳检测<writeHost>
:写库的配置
5、配置切分规则,将如下配置复制粘贴覆盖mycat/conf/rule.xml
的内容:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mycat:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://io.mycat/"><tableRule name="rule1"><rule><columns>id</columns><algorithm>mod-long</algorithm></rule></tableRule><function name="mod-long" class="io.mycat.route.function.PartitionByMod"><!-- how many data nodes --><property name="count">2</property></function>
</mycat:rule>
说明:
- 这里定义的是切分规则,是按照id列进行切分,切分规则是采取取模的方式,
<property name="count">2</property>
:这里配置了我们有拆分了多个库(表),需要和前面配置<table name="user" primaryKey="id" dataNode="db_sszh_01,db_sszh_02" rule="rule1" />
中的dataNode个数一致,否则会出错
6、配置mycat服务器信息,将如下配置复制粘贴覆盖mycat/conf/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><!-- 全局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>--><!-- 配置一个登录名为root,密码为123456的mycat用户账号,只能方位名为db_sszh_mycat的数据库 --><user name="root"><property name="password">123456</property><property name="schemas">db_sszh_mycat</property><!-- 是否为只读,默认fasle,设置为true将无法插入数据 --><property name="readOnly">false</property><!-- 表级 DML 权限设置 --><!-- <privileges check="false"><schema name="db_sszh_mycat" dml="0110" ><table name="db_sszh_01" dml="0000"></table><table name="db_sszh_02" dml="1111"></table></schema></privileges> --></user><user name="user"><property name="password">123456</property><property name="schemas">db_sszh_mycat</property><property name="readOnly">true</property></user></mycat:server>
7、新建数据库:在数据库中创建两个数据库 db_sszh_01, db_sszh_01
建库语句,略...
8、分别在每个库中执行如下建表语句:
CREATE TABLE `user` (`id` bigint(20) NOT NULL,`name` varchar(255) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
9、启动MyCat,进入/usr/local/mycat/bin
目录,执行命令./mycat start
:
[root@LinuxCentOS6 bin]# pwd
/usr/local/mycat/bin
[root@LinuxCentOS6 bin]# ./mycat start
Starting Mycat-server...
Mycat-server is already running.
[root@LinuxCentOS6 bin]#
10、查看MyCat启动日志(位于/usr/local/mycat/logs/wrapper.log
),启动成功会有如下显示,默认端口8066
:
STATUS | wrapper | 2019/10/30 15:08:37 | --> Wrapper Started as Daemon
STATUS | wrapper | 2019/10/30 15:08:37 | Launching a JVM...
INFO | jvm 1 | 2019/10/30 15:08:37 | Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=64M; support was removed in 8.0
INFO | jvm 1 | 2019/10/30 15:08:38 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
INFO | jvm 1 | 2019/10/30 15:08:38 | Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved.
INFO | jvm 1 | 2019/10/30 15:08:38 |
INFO | jvm 1 | 2019/10/30 15:08:39 | 2019-10-30 15:08:39,098 [INFO ][WrapperSimpleAppMain] total resouces of dataHost db_sszh is :1 (io.mycat.backend.datasource.PhysicalDBPool:PhysicalDBPool.java:100)
INFO | jvm 1 | 2019/10/30 15:08:39 | 2019-10-30 15:08:39,112 [INFO ][WrapperSimpleAppMain] create layer cache pool TableID2DataNodeCache of type encache ,default cache size 10000 ,default expire seconds18000 (io.mycat.cache.CacheService:CacheService.java:125)
INFO | jvm 1 | 2019/10/30 15:08:39 | 2019-10-30 15:08:39,123 [INFO ][WrapperSimpleAppMain] create child Cache: TESTDB_ORDERS for layered cache TableID2DataNodeCache, size 50000, expire seconds 18000 (io.mycat.cache.DefaultLayedCachePool:DefaultLayedCachePool.java:80)
INFO | jvm 1 | 2019/10/30 15:08:39 | 2019-10-30 15:08:39,699 [INFO ][WrapperSimpleAppMain] dyna class load from ./catlet,and auto check for class file modified every 60 seconds (io.mycat.config.classloader.DynaClassLoader:DynaClassLoader.java:34)
INFO | jvm 1 | 2019/10/30 15:08:39 | 2019-10-30 15:08:39,699 [INFO ][WrapperSimpleAppMain] =============================================== (io.mycat.MycatServer:MycatServer.java:266)
INFO | jvm 1 | 2019/10/30 15:08:39 | 2019-10-30 15:08:39,699 [INFO ][WrapperSimpleAppMain] MyCat is ready to startup ... (io.mycat.MycatServer:MycatServer.java:267)
INFO | jvm 1 | 2019/10/30 15:08:39 | 2019-10-30 15:08:39,699 [INFO ][WrapperSimpleAppMain] Startup processors ...,total processors:32,aio thread pool size:16
INFO | jvm 1 | 2019/10/30 15:08:39 | each process allocated socket buffer pool bytes ,a page size:2097152 a page's chunk number(PageSize/ChunkSize) is:512 buffer page's number is:40 (io.mycat.MycatServer:MycatServer.java:279)
INFO | jvm 1 | 2019/10/30 15:08:39 | 2019-10-30 15:08:39,699 [INFO ][WrapperSimpleAppMain] sysconfig params:SystemConfig [processorBufferLocalPercent=100, frontSocketSoRcvbuf=1048576, frontSocketSoSndbuf=4194304, backSocketSoRcvbuf=4194304, backSocketSoSndbuf=1048576, frontSocketNoDelay=1, backSocketNoDelay=1, maxStringLiteralLength=65535, frontWriteQueueSize=4096, bindIp=0.0.0.0, serverPort=8066, managerPort=9066, charset=utf8, processors=32, processorExecutor=16, timerExecutor=2, managerExecutor=2, idleTimeout=300000, catletClassCheckSeconds=60, sqlExecuteTimeout=300, processorCheckPeriod=1000, dataNodeIdleCheckPeriod=300000, dataNodeHeartbeatPeriod=10000, clusterHeartbeatUser=_HEARTBEAT_USER_, clusterHeartbeatPass=_HEARTBEAT_PASS_, clusterHeartbeatPeriod=5000, clusterHeartbeatTimeout=10000, clusterHeartbeatRetry=10, txIsolation=3, parserCommentVersion=50148, sqlRecordCount=10, bufferPoolPageSize=2097152, bufferPoolChunkSize=4096, bufferPoolPageNumber=40, maxResultSet=524288, bigResultSizeSqlCount=10, bufferUsagePercent=80, flowControlRejectStrategy=0, clearBigSqLResultSetMapMs=600000, defaultMaxLimit=100, sequnceHandlerType=0, sqlInterceptor=io.mycat.server.interceptor.impl.DefaultSqlInterceptor, sqlInterceptorType=select, sqlInterceptorFile=/usr/local/mycat/logs/sql.txt, mutiNodeLimitType=0, mutiNodePatchSize=100, defaultSqlParser=druidparser, usingAIO=0, packetHeaderSize=4, maxPacketSize=16777216, mycatNodeId=1] (io.mycat.MycatServer:MycatServer.java:280)
INFO | jvm 1 | 2019/10/30 15:08:40 | 2019-10-30 15:08:40,243 [INFO ][WrapperSimpleAppMain] useOffHeapForMerge = 1 (io.mycat.memory.MyCatMemory:MyCatMemory.java:53)
INFO | jvm 1 | 2019/10/30 15:08:40 | 2019-10-30 15:08:40,243 [INFO ][WrapperSimpleAppMain] memoryPageSize = 1m (io.mycat.memory.MyCatMemory:MyCatMemory.java:54)
INFO | jvm 1 | 2019/10/30 15:08:40 | 2019-10-30 15:08:40,243 [INFO ][WrapperSimpleAppMain] spillsFileBufferSize = 1k (io.mycat.memory.MyCatMemory:MyCatMemory.java:55)
INFO | jvm 1 | 2019/10/30 15:08:40 | 2019-10-30 15:08:40,243 [INFO ][WrapperSimpleAppMain] useStreamOutput = 0 (io.mycat.memory.MyCatMemory:MyCatMemory.java:56)
INFO | jvm 1 | 2019/10/30 15:08:40 | 2019-10-30 15:08:40,243 [INFO ][WrapperSimpleAppMain] systemReserveMemorySize = 384m (io.mycat.memory.MyCatMemory:MyCatMemory.java:57)
INFO | jvm 1 | 2019/10/30 15:08:40 | 2019-10-30 15:08:40,249 [INFO ][WrapperSimpleAppMain] totalNetWorkBufferSize = 80MB (io.mycat.memory.MyCatMemory:MyCatMemory.java:58)
INFO | jvm 1 | 2019/10/30 15:08:40 | 2019-10-30 15:08:40,250 [INFO ][WrapperSimpleAppMain] dataNodeSortedTempDir = /usr/local/mycat (io.mycat.memory.MyCatMemory:MyCatMemory.java:59)
INFO | jvm 1 | 2019/10/30 15:08:40 | 2019-10-30 15:08:40,251 [INFO ][WrapperSimpleAppMain] mycat.memory.offHeap.size: 1321MB (io.mycat.memory.MyCatMemory:MyCatMemory.java:122)
INFO | jvm 1 | 2019/10/30 15:08:40 | 2019-10-30 15:08:40,254 [INFO ][WrapperSimpleAppMain] using nio network handler (io.mycat.MycatServer:MycatServer.java:381)
INFO | jvm 1 | 2019/10/30 15:08:40 | 2019-10-30 15:08:40,273 [INFO ][WrapperSimpleAppMain] $_MyCatManager is started and listening on 9066 (io.mycat.MycatServer:MycatServer.java:397)
INFO | jvm 1 | 2019/10/30 15:08:40 | 2019-10-30 15:08:40,273 [INFO ][WrapperSimpleAppMain] $_MyCatServer is started and listening on 8066 (io.mycat.MycatServer:MycatServer.java:401)
INFO | jvm 1 | 2019/10/30 15:08:40 | 2019-10-30 15:08:40,274 [INFO ][WrapperSimpleAppMain] =============================================== (io.mycat.MycatServer:MycatServer.java:403)
INFO | jvm 1 | 2019/10/30 15:08:40 | 2019-10-30 15:08:40,274 [INFO ][WrapperSimpleAppMain] Initialize dataHost ... (io.mycat.MycatServer:MycatServer.java:407)
INFO | jvm 1 | 2019/10/30 15:08:40 | 2019-10-30 15:08:40,274 [INFO ][WrapperSimpleAppMain] init backend myqsl source ,create connections total 10 for server1 index :0 (io.mycat.backend.datasource.PhysicalDBPool:PhysicalDBPool.java:294)
11、启动失败中的一种错误提示:
INFO | jvm 4 | 2019/10/30 13:51:15 | sun.management.AgentConfigurationError:java.net.MalformedURLException: Local host name unknown: java.net.UnknownHostException: LinuxCentOS6: LinuxCentOS6: Name or service not known
解决办法:修改hosts文件,绑定主机名
,我的主机名为LinuxCentOS6
(这里要设置成自己的主机名):
[root@LinuxCentOS6 bin]# vim /etc/hosts
~
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.10.88 LinuxCentOS6 localhost localhost.localdomain localhost6 localhost6.localdomain6
~
~
~
:wq
[root@LinuxCentOS6 bin]#
重新启动mycat服务,命令:./mycat start
查看MyCat进程,命令:ps -ef |grep mycat
12、测试连接MyCat:
linux连接MyCat操作,命令格式:mysql -u用户名 -p密码 -h主机地址 -P端口号 -D数据库名称
:
[root@LinuxCentOS6 logs]# mysql -uroot -p123456 -h192.168.10.88 -P8066 -Ddb_sszh_mycatReading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -AWelcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.20-mycat-1.6-RELEASE-20161028204710 MyCat Server (OpenCloundDB)Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MySQL [db_sszh_mycat]> select * from user;
+----+---------+
| id | name |
+----+---------+
| 2 | 张三 |
| 4 | 李四 |
| 6 | 王五 |
| 8 | 赵六 |
| 1 | A米 |
| 3 | T利 |
| 5 | 凯瑞 |
| 7 | 芥末 |
+----+---------+
8 rows in set (0.002 sec)
JDBC连接MyCat,连接字符串:jdbc:mysql://192.168.10.88:8066/db_sszh_mycat?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false
Navicat连接工具连接MyCat,如图所示:
主机192.168.10.88:3306
,Mysql物理数据库db_sszh_01
数据库数据:
主机192.168.10.88:3306
,Mysql物理数据库db_sszh_02
数据库数据:
主机192.168.10.88:8066
,MyCat逻辑库db_sszh_mycat
数据库数据:
注:以上内容仅提供参考和交流,请勿用于商业用途,如有侵权联系本人删除!
持续更新中…
如有对思路不清晰或有更好的解决思路,欢迎与本人交流,QQ群:273557553
你遇到的问题是小编创作灵感的来源!