MyCat 安装
MyCat 安装
-
下载 mycat
wget http://dl.mycat.org.cn/1.6.7.4/Mycat-server-1.6.7.4-release/Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz tar -zxvf Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz mv mycat /usr/local/ cd /usr/local/mycat
-
配置server 用户
vim bin/server.xml
<!-- 配置 root 密码及 schemas --> <user name="root" defaultAccount="true"> <property name="password">root</property> <property name="schemas">user</property> <property name="defaultSchema">TESTDB</property> </user> <user name="user"> <property name="password">user</property> <property name="schemas">user</property> <property name="readOnly">true</property> <property name="defaultSchema">TESTDB</property> </user>
-
配置 schema 数据库配置, 笔者两个数据库 host 分别为
192.168.31.202:3306, 192.168.31.203:3306
, 两个库分别有数据库 user_node_1 和 user_node_2,每个库中都有一个 user 表vim bin/schema.xml
<schema name="user" checkSQLschema="true" sqlMaxLimit="100" randomDataNode="dn1"> <table name="user" dataNode="dn1,dn2" rule="auto-sharding-long" splitTableNames ="true"/> </schema> <dataNode name="dn1" dataHost="node-1" database="user_node_1" /> <dataNode name="dn2" dataHost="node-2" database="user_node_2" /> <dataHost name="node-1" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <writeHost host="hostM1" url="192.168.31.201:3306" user="root" password="root"> </dataHost> <dataHost name="node-2" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <writeHost host="M2" url="192.168.31.203:3306" user="root" password="root"> </writeHost> </dataHost>
-
由于本案例只有两个 database,所以需要修改默认auto-sharding-long的规则
vim conf/autopartition-long.txt 0-500M=0 500M-1000M=1
-
启动 mycat
./bin/mycat console
-
连接 mycat, 注意端口是 8066
可以看到里面有配置好的逻辑分库 user 和逻辑分表 user
-
测试插入
insert into `user` (id,username) values(1, 'thomas-fan');
可以看到插入成功,并且在分库上写入成功
-
配置开机自启脚本
vim /etc/init.d/mycat
!/bin/sh # # Simple Mycat init.d script conceived to work on Linux systems # as it does use of the /proc filesystem. ### BEGIN INIT INFO # Provides: mycat # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Mycat data structure server # Description: Mycat data structure server. See https://github.com/MyCATApache/Mycat-Server ### END INIT INFO EXEC=/usr/local/mycat/bin/mycat PIDFILE=/usr/local/mycat/logs/mycat.pid case "$1" in start) if [ -f $PIDFILE ] then echo "$PIDFILE exists, process is already running or crashed" else echo "Starting Mycat server..." $EXEC start fi ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE does not exist, process is not running" else PID=$(cat $PIDFILE) echo "Stopping ..." $EXEC stop while [ -x /proc/${PID} ] do echo "Waiting for Mycat to shutdown ..." sleep 1 done echo "Mycat stopped" fi ;; *) echo "Please use start or stop as first argument" ;; esac
cd /etc/init.d/ chmod +x mycat chkconfig mycat on systemctl start mycat