mycat 分库配置(mybatis连接mycat)


mycat是个支持分库分表,读写分离的分布式数据库中间件,以下是配置过程以及操作内容.

创建用户
CREATE USER 'user'@'%' IDENTIFIED BY 'user';
用户授权
GRANT ALL ON *.* TO 'user'@'%';


grant all PRIVILEGES on *.* to user@'%'  identified by 'user';

grant all PRIVILEGES on *.* to root@'%'  identified by 'root';


%MYCAT_HOMEW%/conf/server.xml

0
0

0
0
0

1

1m

1k

0

384m


true



root
distribute1


user
distribute1
true





%MYCAT_HOMEW%/conf/schema.xml



rule="sharding-by-intfile" />
rule="sharding-by-intfile">
parentKey="id">
parentKey="id" />
parentKey="id" />


writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
select user()
password="root">


writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
select user()
password="root">


writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
select user()
password="root">


8066:mycat的提供服务端口
9066:mycat的控制台端口
可以直接通过8066连接mysql,像是我们通过3306来连接一样
SELECT 1187334599760547840
MYCATSEQ_GLOBAL 可以在conf目录下面配置 sequence_conf.properties, 默认就可以,这个是使用本地配置文件来实现全局序列化主键id。

[heika_superadmin@172-16-2-146 ~]$ mysql -h 172.16.2.146 -uroot -proot -P8066
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.29-mycat-1.6-RELEASE-20161028204710 MyCat Server (OpenCloundDB)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
mysql> show databases;
+-------------+
| DATABASE |
+-------------+
| distribute1 |
+-------------+
1 row in set (0.00 sec)

mysql> select * from company where id=10003;
ERROR 3000 (HY000): No MyCAT Database selected
mysql> use distribute1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from company where id=10003;
Empty set (0.02 sec)

mysql> insert into company(id,name,sharding_id) values( 1187334599760547841,'hp',10000);
Query OK, 1 row affected (0.02 sec)

mysql> insert into company(id,name,sharding_id) values( 1187334599764742144,'hp',10000);
Query OK, 1 row affected (0.01 sec)

mysql> insert into company(id,name,sharding_id) values( 1187334599764742145,'hp',10000);
Query OK, 1 row affected (0.01 sec)

mysql> insert into travelrecord(id,name) values( 1187334599781519360,'hp');
Query OK, 1 row affected (0.00 sec)

mysql> insert into travelrecord(id,name) values( 1187334599785713664,'hp');insert into travelrecord(id,name) values( 1187334599785713665,'hp');
Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.01 sec)

mysql> select * from company com left join travelrecord tre on com.`name`=tre.`name` where com.`name`='hp' ;
+-------+------+-------------+-------+------+
| id | name | sharding_id | id | name |
+-------+------+-------------+-------+------+
| 10012 | hp | 10000 | NULL | NULL |
| 10011 | hp | 10000 | 10014 | hp |
| 10011 | hp | 10000 | 10015 | hp |
| 10011 | hp | 10000 | 10016 | hp |
| 10013 | hp | 10000 | NULL | NULL |
+-------+------+-------------+-------+------+
5 rows in set (0.01 sec)

mysql>


Mybatis实现的话,就需要改一个地方
<insert id="insert" parameterType="com.abin.lee.distribute.mycat.model.Team"  useGeneratedKeys="true" keyProperty="id" keyColumn="id">
  
    
  
  insert into team (id, team_name, create_time, update_time,
    version)
  values ( 1187334599789907968, #{teamName,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP},
    #{version,jdbcType=INTEGER})
insert>
主键id使用mycat的实现即可













你可能感兴趣的:(mycat 分库配置(mybatis连接mycat))