使用mycat实现读写分离

centos6.5系统
主服务器:192.168.1.84
从服务器:192.168.1.83
一主一从模式
实现读写分离之前需要先进行mysql主从配置,参考msyql主从配置
因为mycat不负责同步数据问题
修改mycat配置文件schema.xml为:



<mycat:schema xmlns:mycat="http://org.opencloudb/">

        <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="node04">
                

        schema>
        <dataNode name="node04" dataHost="localhost" database="test01" />

        <dataHost name="localhost" maxCon="1000" minCon="10" balance="3"
                writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
                <heartbeat>select user()heartbeat>
                
                <writeHost host="hostM1" url="192.168.1.84:3306" user="tongbu"
                        password="123456">
                <readHost host="hostS1" url="192.168.1.83:3306" user="tongbu" 
                        password="123456">
                readHost>
                writeHost>
        dataHost>

mycat:schema>

修改mycat配置文件server.xml:




<mycat:server xmlns:mycat="http://org.opencloudb/">
        <system>
        <property name="defaultSqlParser">druidparserproperty>
       
        
        
                
                
                
                
                
                
                
        
        system>
        <user name="test">
                <property name="password">testproperty>
                <property name="schemas">TESTDBproperty>
        user>

        <user name="user">
                <property name="password">testproperty>
                <property name="schemas">TESTDBproperty>
                <property name="readOnly">trueproperty>
        user>

mycat:server>

验证mycat读写分离是否实现:

[root@node04 ~]# mysql -utest -ptest -h192.168.1.84 -P8066 -DTESTDB
mysql> show tables; #test01数据库之前已经创建好,以及数据库表
+------------------+
| Tables_in_test01 |
+------------------+
| grades           |
| student          |
| test             |
+------------------+
3 rows in set (0.00 sec)
mysql> select *from test;
+----+--------+
| id | name   |
+----+--------+
|  1 | aaa    |
|  2 | bbb    |
|  3 | ccc    |
| 11 | xxx    |
| 12 | zzz    |
| 13 | node03 |
| 14 | node03 |
| 16 | ddd    |
| 17 | eee    |
+----+--------+

在1.83的从库上也可以查看到数据信息:

[root@node03 ~]# mysql -u root -p111111 -Dtest01
mysql> show tables;
+------------------+
| Tables_in_test01 |
+------------------+
| grades           |
| student          |
| test             |
+------------------+
3 rows in set (0.00 sec)
mysql> select *from test;
+----+--------+
| id | name   |
+----+--------+
|  1 | aaa    |
|  2 | bbb    |
|  3 | ccc    |
| 11 | xxx    |
| 12 | zzz    |
| 13 | node03 |
| 14 | node03 |
| 16 | ddd    |
| 17 | eee    |
+----+--------+
mysql> stop slave #停止主从复制,验证读写分离

进入mycat虚拟数据库插入数据进行验证:

mysql> insert into test values(18, "fff");
mysql> select *from test;
+----+--------+
| id | name   |
+----+--------+
|  1 | aaa    |
|  2 | bbb    |
|  3 | ccc    |
| 11 | xxx    |
| 12 | zzz    |
| 13 | node03 |
| 14 | node03 |
| 16 | ddd    |
| 17 | eee    |
+----+--------+
#发现没有新插入的数据,因为关闭了主从复制,代理层只把数据写到了主库,从库是读端没有复制数据,只显示以前的数据。
[root@node04 ~]# mysql -uroot -p111111 -Dtest01
mysql> select *from test;
+----+--------+
| id | name   |
+----+--------+
|  1 | aaa    |
|  2 | bbb    |
|  3 | ccc    |
| 11 | xxx    |
| 12 | zzz    |
| 13 | node03 |
| 14 | node03 |
| 16 | ddd    |
| 17 | eee    |
| 18 | fff     |
+----+--------+
#主库里面有

开启从库的主从复制:

mysql> start slave;
mysql> select *from test;
+----+--------+
| id | name   |
+----+--------+
|  1 | aaa    |
|  2 | bbb    |
|  3 | ccc    |
| 11 | xxx    |
| 12 | zzz    |
| 13 | node03 |
| 14 | node03 |
| 16 | ddd    |
| 17 | eee    |
| 18 | fff     |
+----+--------+

进入mycat代理数据库:

msyql> select *from test;
+----+--------+
| id | name   |
+----+--------+
|  1 | aaa    |
|  2 | bbb    |
|  3 | ccc    |
| 11 | xxx    |
| 12 | zzz    |
| 13 | node03 |
| 14 | node03 |
| 16 | ddd    |
| 17 | eee    |
| 18 | fff     |
+----+--------+

你可能感兴趣的:(数据库)