Deploying MyCAT Database and Table Partitioning
- Create a service table for each of the three databases.
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
dn1: # Connect to the database. /usr/local/mysql/bin/mysql -uroot -p123456 -S /data/mysql/run/mysql1.sock -Dsysbench # Create a table. CREATE TABLE `test1` ( `id` int(11) NOT NULL, `name` varchar(50) NOT NULL DEFAULT '', `indate` datetime NOT NULL DEFAULT '1910-01-01 00:00:00', PRIMARY KEY (`id`) ) ENGINE=InnoDB; dn2: # Connect to the database. /usr/local/mysql/bin/mysql -uroot -p123456 -S /data/mysql/run/mysql2.sock -Dsysbench # Create a table. CREATE TABLE `test2` ( `id` int(11) NOT NULL, `name` varchar(50) NOT NULL DEFAULT '', `indate` datetime NOT NULL DEFAULT '1910-01-01 00:00:00', PRIMARY KEY (`id`) ) ENGINE=InnoDB; dn3: # Connect to the database. /usr/local/mysql/bin/mysql -uroot -p123456 -S /data/mysql/run/mysql3.sock -Dsysbench # Create a table. CREATE TABLE `test3` ( `id` int(11) NOT NULL, `name` varchar(50) NOT NULL DEFAULT '', `indate` datetime NOT NULL DEFAULT '1910-01-01 00:00:00', PRIMARY KEY (`id`) ) ENGINE=InnoDB;
- Modify the schema.xml file.
- Open the file.
1vim /usr/local/mycat/conf/schema.xml - Add the following content to the file:
1 2 3
<table name="test1" dataNode="dn1" > </table> <table name="test2" dataNode="dn2" > </table> <table name="test3" dataNode="dn3" > </table>

- Press Esc, type :wq!, and press Enter to save the file and exit.
- Open the file.
- Restart MyCAT and check the status.
1 2 3
mycat stop mycat start mycat status
- Insert test data.
- Open the file.
1vim /home/testdist.sh - Add the following content to the file:
1 2 3 4 5 6 7 8 9 10 11 12 13
#!/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 test1(id,name) values($i,'ccc')" done for i in {10001..20000} do /usr/local/mysql/bin/mysql -uroot -p1234567 -P8066 -h127.0.0.1 --default_auth=mysql_native_password -Dsysbench -e "insert into test2(id,name) values($i,'ccc')" done for i in {20001..30000} do /usr/local/mysql/bin/mysql -uroot -p1234567 -P8066 -h127.0.0.1 --default_auth=mysql_native_password -Dsysbench -e "insert into test3(id,name) values($i,'ccc')" done
- Press Esc, type :wq!, and press Enter to save the file and exit.
- Execute the script.
1sh /home/testdist.sh
- Open the file.
- Check the data distribution.
1/usr/local/mysql/bin/mysql -uroot -p123456 -S /data/mysql/run/mysql1.sock -Dsysbench -e "select max(id) from test1"

1/usr/local/mysql/bin/mysql -uroot -p123456 -S /data/mysql/run/mysql2.sock -Dsysbench -e "select max(id) from test2"

1/usr/local/mysql/bin/mysql -uroot -p123456 -S /data/mysql/run/mysql3.sock -Dsysbench -e "select max(id) from test3"

The 30,000 data records are distributed in different tables of different databases.
Parent topic: MySQL Database and Table Partitioning Scheme