MyCAT Installation & Configuration Guide
Download & Installation
- Extract installation package:
tar -zxvf Mycat-server-xx-RELEASE-xxxxx-linux.tar.gz
- Move to /usr/local:
mv mycat /usr/local/
- Set environment variables (optional):
export MYCAT_HOME=/usr/local/mycat
export PATH=$PATH:$MYCAT_HOME/bin
Basic Configuration
Main configuration files for MyCAT are in the conf directory:
- server.xml: Configure MyCAT service parameters
- schema.xml: Configure logical database and sharding rules
- rule.xml: Configure sharding algorithms
Service Management Commands
# Start MyCAT service
./mycat start
# Stop MyCAT service
./mycat stop
# Restart MyCAT service
./mycat restart
# Check service status
./mycat status
Connection Test
mysql -h127.0.0.1 -P8066 -uroot -p123456
Sharding Rules Explained
1. Time-Based Sharding Rules
- Daily sharding: Store data shards by natural day
- Natural month sharding: Shard by calendar month
- Single month hour sharding: Subdivide within a single month by hour
2. Hash-Based Sharding Rules
- Hash fixed sharding: Calculate standard hash algorithm on key fields then shard
- Date range hash sharding: First partition by date range, then hash data within partitions
- Consistent hash sharding: Use consistent hash algorithm to reduce data migration when nodes change
3. Modulo-Based Sharding Rules
- Modulo sharding: Directly modulo key field values to determine shard
- Modulo range sharding: First modulo then map to range interval
- Range modulo sharding: First partition by range, then modulo data within partition
4. Other Sharding Rules
- Enumeration sharding: Directly map to specific shard based on field enumerated values
- Range convention sharding: Pre-agree on numerical range for each shard
- Hot-cold data sharding: Store hot data and cold data separately based on access frequency
Common Sharding Configuration
Modulo Sharding
<tableRule name="mod-long">
<rule>
<columns>id</columns>
<algorithm>mod-long</algorithm>
</rule>
</tableRule>
<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
<property name="count">3</property>
</function>
Consistent Sharding
<function name="murmur"
class="io.mycat.route.function.PartitionByMurmurHash">
<property name="count">2</property>
<property name="virtualBucketTimes">160</property>
</function>