linux下mysql主从复制(第二篇读写分离) mycat 中间件
接着上篇文档补全,mysql 读写分离,在上篇文章案例下准备好Mycat-server-1.4-release-20151019230038-win.tar.gz 解压包
1.解压后的文件如下:
1.1 接下来进入到conf 目录下配置四个文件server.xml、schema.xml、rule.xml、log4j.xml 下面一一介绍
1.2 首先在我们主服务上面test 库下新建两张表如下:
CREATETABLE
`t_users`
(
`user_id`
varchar
(
64)
NOTNULL
COMMENT
‘注册用户ID‘
,
`user_email`
varchar
(
64)
NOTNULL
COMMENT
‘注册用户邮箱‘
,
`user_password`
varchar
(
64)
NOTNULL
COMMENT
‘注册用户密码‘
,
`user_nikename`
varchar
(
64)
NOTNULL
COMMENT
‘注册用户昵称‘
,
`user_creatime`
datetime
NOTNULL
COMMENT
‘注册时间‘
,
`user_status`
tinyint(
1)
NOTNULL
COMMENT
‘验证状态 1:已验证 0:未验证‘
,
`user_deleteflag`
tinyint(
1)
NOTNULL
COMMENT
‘删除标记 1:已删除 0:未删除‘
,
PRIMARY
KEY(
`user_id`)
)
ENGINE=
InnoDBDEFAULT
CHARSET
=utf8;
-- -------------------------------------
-- Table structure for `t_message`微博表
-- -------------------------------------
DROPTABLE
IF
EXISTS
`t_message`
;
CREATETABLE
`t_message`
(
`messages_id`
varchar
(
64)
NOTNULL
COMMENT
‘微博ID‘
,
`user_id`
varchar
(
64)
NOTNULL
COMMENT
‘发表用户‘
,
`messages_info`
varchar
(
255)
DEFAULTNULL
COMMENT
‘微博内容‘
,
`messages_time`
datetime
DEFAULTNULL
COMMENT
‘发布时间‘
,
`messages_commentnum`
int
(
12)
DEFAULTNULL
COMMENT
‘评论次数‘
,
`message_deleteflag`
tinyint(
1)
NOTNULL
COMMENT
‘删除标记 1:已删除 0:未删除‘
,
`message_viewnum`
int
(
12)
DEFAULTNULL
COMMENT
‘被浏览量‘
,
PRIMARY
KEY(
`messages_id`),
KEY
`user_id`
(
`user_id`),
CONSTRAINT
`t_message_ibfk_1`
FOREIGN
KEY(
`user_id`)
REFERENCES`t_users`
(
`user_id`)
)
ENGINE=
InnoDBDEFAULT
CHARSET
=utf8; 1.3配置server.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mycat:server SYSTEM "server.dtd"> <mycat:server xmlns:mycat="http://org.opencloudb/"> <system> <property name="defaultSqlParser">druidparser</property> <property name="charset">utf8</property> </system> <!--添加user 此用户可以读写-->
<user name="mycat"> <property name="password">mycat</property> <property name="schemas">mycat</property> </user> <!--添加user 此用户只能读--> <user name="mycat_red"> <property name="password">mycat_red</property> <property name="schemas">mycat</property> <property name="readOnly">true</property> </user> </mycat:server>1.4配置schema.x
<?xml version="1.
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://org.opencloudb/"> <schema name="mycat" checkSQLschema="true" sqlMaxLimit="100"> <table name="t_users" primaryKey="user_id" dataNode="dn1" rule="rule1"/> <table name="t_message" type="global" primaryKey="messages_id" dataNode="dn1" /> </schema> <dataNode name="dn1" dataHost="jdbchost" database="test"/> <dataHost name="jdbchost" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <writeHost host="hostMaster" url="192.168.226.128:3306" user="root" password="123456"> </writeHost> <writeHost host="hostSlave" url="192.168.226.128:3306" user="root" password="123456" /> </dataHost> </mycat:schema>
1.5配置rule.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mycat:rule SYSTEM "rule.dtd"> <mycat:rule xmlns:mycat="http://org.opencloudb/"> <tableRule name="rule1"> <rule> <columns>user_id</columns> <algorithm>func1</algorithm> </rule> </tableRule> <function name="func1" class="org.opencloudb.route.function.AutoPartitionByLong"> <property name="mapFile">autopartition-long.txt</property> </function> </mycat:rule> 1.6 配置log4j.xml只需要把level 改成debug 就行,报错可以在log看到错误信息方便定位 <level value="debug" /> ps:至此我们四个配置文件就配置完成了进入bin目录下启动我们的mycat吧!如下
启动后我们进入navicat 新建两个链接:
可读可写链接如下:
只可读链接如下:
结构如下:
看到区别了吗,莫名其妙多了主服务上面的三张表,到此为止你就可以进行测试了,简单的读写分离也就完成了!
最后说一句,MyCat 很好的管理了我们读写分离的用户最后附上读连接进行写操作的时候爆出来错误
ps :到此结束,小白教程,不喜勿喷,有问题留言,多谢大神指教!!!!!!!!!!!!!!!
原文地址:https://www.cnblogs.com/tjlboke/p/9709620.html
时间: 2024-09-30 22:55:38