Configuring the mod-long Sharding Strategy
- Modify the schema.xml file that is used to configure the MyCAT databases, MySQL tables, sharding strategy, and sharding type.
- Open the file.
1vim /usr/local/mycat/conf/schema.xml - Replace the file content with the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
<?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <schema name="sysbench" checkSQLschema="false" > <table name="users" primaryKey="id" dataNode="dn1,dn2,dn3" rule="mod-long" autoIncrement="true" fetchStoreNodeByJdbc="true"> </table> </schema> <dataNode name="dn1" dataHost="dh1" database="sysbench" /> <dataNode name="dn2" dataHost="dh2" database="sysbench" /> <dataNode name="dn3" dataHost="dh3" database="sysbench" /> <dataHost name="dh1" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <writeHost host="lh" url="localhost:3309" user="root" password="123456"> <readHost host="lh" url="localhost:3309" user="root" password="123456"/> </writeHost> </dataHost> <dataHost name="dh2" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <writeHost host="lh" url="localhost:3310" user="root" password="123456"> <readHost host="lh" url="localhost:3310" user="root" password="123456"/> </writeHost> </dataHost> <dataHost name="dh3" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <writeHost host="lh" url="localhost:3311" user="root" password="123456"> <readHost host="lh" url="localhost:3311" user="root" password="123456"/> </writeHost> </dataHost> </mycat:schema>

In this example, data in the users table is evenly distributed to three databases based on the mod-long sharding strategy, and the user password, IP address, and port number of each database are configured. The MyCAT and MySQL databases are deployed on the same physical server, and use the IP address of local host as their IP address. You can also change the IP address based on the actual environment.
- Press Esc, type :wq!, and press Enter to save the file and exit.
- Open the file.
- Start MyCAT.
- Start MyCAT.
1mycat start - Check whether MyCAT is started.
1mycat status
If the command output is the same as that shown in the figure, MyCAT is started.
- Start MyCAT.
- Connect to the MyCAT database and insert data.
- Open the file.
vim /home/insert.sh
- Add the following content to the script:
1 2 3 4 5
#!/bin/bash for i in {1..10000} do /usr/local/mysql/bin/mysql -uroot -p1234567 -P8066 -h127.0.0.1 --default_auth=mysql_native_password -Dsysbench -e "insert into users(id,name) values($i,'ccc')" done
- Press Esc, type :wq!, and press Enter to save the file and exit.
- Execute the script.
1sh /home/insert.sh
- Open the file.
- View data distribution.
1/usr/local/mysql/bin/mysql -uroot -p123456 -S /data/mysql/run/mysql1.sock -Dsysbench -e "select count(*) from users"

1/usr/local/mysql/bin/mysql -uroot -p123456 -S /data/mysql/run/mysql2.sock -Dsysbench -e "select count(*) from users"

1/usr/local/mysql/bin/mysql -uroot -p123456 -S /data/mysql/run/mysql3.sock -Dsysbench -e "select count(*) from users"

The 10,000 data records are evenly distributed on the database shards.
Parent topic: Deploying MyCAT Database Sharding