什么是MYCAT
一个彻底开源的,面向企业应用开发的大数据库集群支持事务、ACID、可以替代MySQL的加强版数据库一个可以视为MySQL集群的企业级数据库,用来替代昂贵的Oracle集群一个融合内存缓存技术、NoSQL技术、HDFS大数据的新型SQL Server结合传统数据库和新型分布式数据仓库的新一代企业级数据库产品一个新颖的数据库中间件产品环境准备
MyCAT 是使用 JAVA 语言进行编写开发,使用前需要先安装 JAVA 运行环境(JRE),由于 MyCAT 中使用了JDK7 中的一些特性,所以要求必须在 JDK7 以上的版本上运行。
http://www.oracle.com/technetwork/pt/java/javase/downloads/jdk8-downloads-2133151.html
下载合适版本的jdk,并解压到合适的目录如 /usr/local/下,一定要合适的版本,我之前用32位系统下了个64位的,无法运行。
添加环境变量:
vim /etc/profile //添加以下内容 JAVA_HOME=/usr/local/jdk1.8.0_161 CLASSPATH=.:$JAVA_HOME/lib.tools.jar PATH=$JAVA_HOME/bin:$PATH export JAVA_HOME CLASSPATH PATH
测试下
[root@centos6 ~]# java -version java version "1.8.0_161" Java(TM) SE Runtime Environment (build 1.8.0_161-b12) Java HotSpot(TM) Client VM (build 25.161-b12, mixed mode)<code></code>
最好再建一个/usr/bin/java的超链接,因为一些自己注册的linux服务(如springboot的jar注册的服务),默认情况从/usr/bin/java路径使用java,yum安装的时候,这个超链接会自动创建,如果你自己下载包安装的话,这个超链接就需要你手动创建了。
[root@centos6 local]# ln -s /usr/local/jdk1.8.0_161/bin/java /usr/bin/java
安装MyCAT
下载:
wget http://dl.mycat.io/1.6-RELEASE/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
解压到合适目录;
解压后目录如下:
[root@centos6 mycat]# ll 总用量 24 drwxr-xr-x. 2 root root 4096 3月 14 23:15 bin drwxrwxrwx. 2 root root 4096 3月 1 2016 catlet drwxrwxrwx. 4 root root 4096 3月 14 23:15 conf drwxr-xr-x. 2 root root 4096 3月 14 23:15 lib drwxrwxrwx. 2 root root 4096 10月 28 2016 logs -rwxrwxrwx. 1 root root 217 10月 28 2016 version.txt
主要是要修改conf下的server.xml和schema.xml文件
server.xml
<user name="mycat_client_user"> <!--此项设置的是连接mycat服务的用户密码和数据库名,此数据库并非真实存在,而是对真实数据库的映射--> <property name="password">123456</property> <property name="schemas">mycat_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>
schema.xml
<schema name="mycat_testdb" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1" > </schema> <span class="redactor-invisible-space"><dataNode name="dn1" dataHost="localhost1" database="test_db" /> <span class="redactor-invisible-space"><dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"<span class="redactor-invisible-space"> writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"><span class="redactor-invisible-space"> <span class="redactor-invisible-space"><heartbeat>select user()</heartbeat><span class="redactor-invisible-space"> <span class="redactor-invisible-space"><writeHost host="hostM1" url="192.168.206.130:3306" user="u_mycat"<span class="redactor-invisible-space"> password="123456" /> <!--此处用户名密码是真实数据库的用户密码--><span class="redactor-invisible-space"> </dataHost><span class="redactor-invisible-space"></span></span></span></span></span></span></span></span></span></span>
启动
[root@centos6 mycat]# ./bin/mycat start Starting Mycat-server...
看起来没什么不对,但是等一会就发现出问题了
[root@centos6 mycat]# ./bin/mycat status Mycat-server is not running.
查看日志文件
[root@centos6 mycat]# tail logs/wrapper.log STATUS | wrapper | 2018/03/15 00:05:57 | Launching a JVM... ERROR | wrapper | 2018/03/15 00:05:57 | JVM exited while loading the application. INFO | jvm 5 | 2018/03/15 00:05:57 | Java HotSpot(TM) Server VM warning: ignoring option MaxPermSize=64M; support was removed in 8.0 INFO | jvm 5 | 2018/03/15 00:05:57 | Invalid maximum heap size: -Xmx4G INFO | jvm 5 | 2018/03/15 00:05:57 | The specified size exceeds the maximum representable size. INFO | jvm 5 | 2018/03/15 00:05:57 | Error: Could not create the Java Virtual Machine. INFO | jvm 5 | 2018/03/15 00:05:57 | Error: A fatal exception has occurred. Program will exit. FATAL | wrapper | 2018/03/15 00:05:57 | There were 5 failed launches in a row, each lasting less than 300 seconds. Giving up. FATAL | wrapper | 2018/03/15 00:05:57 | There may be a configuration problem: please check the logs. STATUS | wrapper | 2018/03/15 00:05:57 | <-- Wrapper Stopped
发现是配置文件有问题
vim conf/wrapper.conf //找到如下两行 wrapper.java.additional.10=-Xmx4G wrapper.java.additional.11=-Xms1G 修改为 wrapper.java.additional.10=-Xmx1G //此两处要根据运行环境,我这用的是1g的虚拟机 wrapper.java.additional.11=-Xms64M
再次启动mycat,查看日志
[root@centos6 mycat]# tail logs/wrapper.log INFO | jvm 1 | 2018/03/15 00:23:35 | 2018-03-15 00:23:35,571 [INFO ][$_NIOREACTOR-0-RW] connected successfuly MySQLConnection [id=4, lastTime=1521044615571, user=u_mycat, schema=test_db, old shema=test_db, borrowed=true, fromSlaveDB=false, threadId=38, charset=latin1, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=192.168.206.130, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false] (io.mycat.backend.mysql.nio.handler.GetConnectionHandler:GetConnectionHandler.java:67) INFO | jvm 1 | 2018/03/15 00:23:35 | 2018-03-15 00:23:35,571 [INFO ][$_NIOREACTOR-0-RW] connected successfuly MySQLConnection [id=3, lastTime=1521044615571, user=u_mycat, schema=test_db, old shema=test_db, borrowed=true, fromSlaveDB=false, threadId=44, charset=latin1, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=192.168.206.130, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false] (io.mycat.backend.mysql.nio.handler.GetConnectionHandler:GetConnectionHandler.java:67) INFO | jvm 1 | 2018/03/15 00:23:35 | 2018-03-15 00:23:35,571 [INFO ][$_NIOREACTOR-0-RW] connected successfuly MySQLConnection [id=9, lastTime=1521044615571, user=u_mycat, schema=test_db, old shema=test_db, borrowed=true, fromSlaveDB=false, threadId=46, charset=latin1, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=192.168.206.130, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false] (io.mycat.backend.mysql.nio.handler.GetConnectionHandler:GetConnectionHandler.java:67) INFO | jvm 1 | 2018/03/15 00:23:35 | 2018-03-15 00:23:35,572 [INFO ][$_NIOREACTOR-0-RW] connected successfuly MySQLConnection [id=10, lastTime=1521044615572, user=u_mycat, schema=test_db, old shema=test_db, borrowed=true, fromSlaveDB=false, threadId=40, charset=latin1, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=192.168.206.130, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false] (io.mycat.backend.mysql.nio.handler.GetConnectionHandler:GetConnectionHandler.java:67) INFO | jvm 1 | 2018/03/15 00:23:35 | 2018-03-15 00:23:35,572 [INFO ][$_NIOREACTOR-0-RW] connected successfuly MySQLConnection [id=2, lastTime=1521044615572, user=u_mycat, schema=test_db, old shema=test_db, borrowed=true, fromSlaveDB=false, threadId=43, charset=latin1, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=192.168.206.130, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false] (io.mycat.backend.mysql.nio.handler.GetConnectionHandler:GetConnectionHandler.java:67) INFO | jvm 1 | 2018/03/15 00:23:35 | 2018-03-15 00:23:35,575 [INFO ][$_NIOREACTOR-0-RW] connected successfuly MySQLConnection [id=5, lastTime=1521044615575, user=u_mycat, schema=test_db, old shema=test_db, borrowed=true, fromSlaveDB=false, threadId=45, charset=latin1, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=192.168.206.130, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false] (io.mycat.backend.mysql.nio.handler.GetConnectionHandler:GetConnectionHandler.java:67) INFO | jvm 1 | 2018/03/15 00:23:35 | 2018-03-15 00:23:35,576 [INFO ][$_NIOREACTOR-0-RW] connected successfuly MySQLConnection [id=1, lastTime=1521044615576, user=u_mycat, schema=test_db, old shema=test_db, borrowed=true, fromSlaveDB=false, threadId=41, charset=latin1, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=192.168.206.130, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false] (io.mycat.backend.mysql.nio.handler.GetConnectionHandler:GetConnectionHandler.java:67) INFO | jvm 1 | 2018/03/15 00:23:35 | 2018-03-15 00:23:35,580 [INFO ][WrapperSimpleAppMain] init result :finished 10 success 10 target count:10 (io.mycat.backend.datasource.PhysicalDBPool:PhysicalDBPool.java:319) INFO | jvm 1 | 2018/03/15 00:23:35 | 2018-03-15 00:23:35,580 [INFO ][WrapperSimpleAppMain] localhost1 index:0 init success (io.mycat.backend.datasource.PhysicalDBPool:PhysicalDBPool.java:265) INFO | jvm 1 | 2018/03/15 00:23:35 | MyCAT Server startup successfully. see logs in logs/mycat.log
启动成功
连接测试下
[root@centos6 ~]# mysql -umycat_client_user -p -h192.168.206.130 -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, 2018, 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> show databases; +--------------+ | DATABASE | +--------------+ | mycat_testdb | +--------------+ 1 row in set (0.01 sec)
一切ok。