全局表:每个分片节点上都有这张表,都会运行创建表的DDL语句。多份写入,多处读取
应用场景:比如说数据库有一张表总体变动很小,而其它的查询操作经常需要关联此表,那么这个表就适合做成mycat全局表
1登录两个分片节点创建t_area表
[root@db01 ~]# mysql -S /data/3307/mysql.sock mysql> use taobao mysql> create table t_area (id int not null primary key auto_increment,name varchar(20) not null); mysql> exit
|
[root@db01 ~]# mysql -S /data/3308/mysql.sock mysql> use taobao mysql> create table t_area (id int not null primary key auto_increment,name varchar(20) not null); mysql> exit
|
2将t_area表配置成mycat全局表:
[root@db01 ~]# vim /application/mycat/conf/schema.xml [root@db01 ~]# cat /application/mycat/conf/schema.xml <?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="sh1"> <table name="t3" dataNode="sh1,sh2" rule="auto-sharding-long" /> <table name="t5" dataNode="sh1,sh2" rule="sharding-by-intfile" /> <table name="t_area" primaryKey="id" type="global" dataNode="sh1,sh2" /> </schema> <dataNode name="sh1" dataHost="oldguo1" database= "taobao" /> <dataNode name="sh2" dataHost="oldguo2" database= "taobao" /> <dataHost name="oldguo1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1"> <heartbeat>select user()</heartbeat> <writeHost host="db1" url="10.0.0.201:3307" user="root" password="123"> <readHost host="db2" url="10.0.0.201:3309" user="root" password="123" /> </writeHost> <writeHost host="db3" url="10.0.0.202:3307" user="root" password="123"> <readHost host="db4" url="10.0.0.202:3309" user="root" password="123" /> </writeHost> </dataHost> <dataHost name="oldguo2" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1"> <heartbeat>select user()</heartbeat> <writeHost host="db1" url="10.0.0.201:3308" user="root" password="123"> <readHost host="db2" url="10.0.0.201:3310" user="root" password="123" /> </writeHost> <writeHost host="db3" url="10.0.0.202:3308" user="root" password="123"> <readHost host="db4" url="10.0.0.202:3310" user="root" password="123" /> </writeHost> </dataHost> </mycat:schema>
|
3重启mycat后登录mycat数据库插入测试数据:
[root@db01 ~]# mycat restart Stopping Mycat-server... Mycat-server was not running. Starting Mycat-server... [root@db01 ~]# mysql -uroot -p123456 -h10.0.0.201 -P8066 mysql> use TESTDB mysql> insert into t_area(id,name) values(1,'a'); mysql> insert into t_area(id,name) values(2,'b');
mysql> insert into t_area(id,name) values(3,'c'); mysql> insert into t_area(id,name) values(4,'d'); mysql> select * from t_area; +----+------+ | id | name | +----+------+ | 1 | a | | 2 | b | | 3 | c | | 4 | d | +----+------+ 4 rows in set (0.09 sec)
mysql> exit
|
4测试,在不同的后端节点进行查询
[root@db01 ~]# mysql -S /data/3307/mysql.sock -e "select * from taobao.t_area;" +----+------+ | id | name | +----+------+ | 1 | a | | 2 | b | | 3 | c | | 4 | d | +----+------+ [root@db01 ~]# mysql -S /data/3308/mysql.sock -e "select * from taobao.t_area;" +----+------+ | id | name | +----+------+ | 1 | a | | 2 | b | | 3 | c | | 4 | d | +----+------+ [root@db01 ~]#
|