http://blog.csdn.net/zhanglei_16/article/details/50707487

Mycat读写分离、主从切换学习
问题一:分表、分库的优缺点,以及分表无法成为主流
分表方式:是在将一个大表,在一个db库内,分成多个分表,
优点是:分开之后的表,仍然在一个库内,便于查看、管理。
缺点:db只能在一个服务器内,不能解决I/O问题,负载集中

分库方式:将一个大表,分布在多个DB中,每个DB只保留一部分数据,所有数据组合起来才是全库的数据。
优点:优点是分担IO、负载均衡,可以让整个集群性能随机器数扩大,性能水平扩展
缺点:不易维护、数据统计以及jion操作会跨库处理,对性能有影响。

分表无法成为主流原因:目前mysql等数据库随着业务增长,绝大对数的解决方案都是通过增加服务器来水平扩展
提高集群的性能,而分表方式,只能局限在一个服务器上,会有性能瓶颈。

1:环境
192.168.1.21   mycat1
192.168.1.22   mysql1
192.168.1.23   mysql2

2:在mysql1、mysql2上安装mysql
安装Mysql、初始化mysql省略
use mysql
GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "oracle";
update user set Password = password('oracle') where User='root';
GRANT replication slave ON *.* TO 'repluser'@'%' identified by 'oracle';
flush privileges;
exit;

3:配置mysql复制
在mysql1:
mysql> show master status;
+------------------+----------+--------------+--------------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------+-------------------+
| mysql-bin.000003 |      401 |              | mysql,information_schema |                   |
+------------------+----------+--------------+--------------------------+-------------------+
    
在mysql2:向主库做同步操作,开启复制
change master to master_host='192.168.10.22',
master_port=3306, master_user='repluser', 
master_password='oracle', master_log_file='mysql-bin.000003',master_log_pos=401;
start slave;

show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.22
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000005
          Read_Master_Log_Pos: 2095
               Relay_Log_File: mysql2-relay-bin.000002
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000005
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

在mysql1创建测试库、用户:
create database testdb1;
create database testdb2;
create database testdb3;
GRANT ALL PRIVILEGES ON testdb1.* TO 'zhang'@"%" IDENTIFIED BY "oracle";
GRANT ALL PRIVILEGES ON testdb2.* TO 'zhang'@"%" IDENTIFIED BY "oracle";
GRANT ALL PRIVILEGES ON testdb3.* TO 'zhang'@"%" IDENTIFIED BY "oracle";
flush privileges;
exit

数据同步测试:
explain create table company(id int not null primary key,name varchar(100));

4:安装、配置mycat
测试思路:
Mysql高可用和读写分离的几个配置点:
writeType="0"   --写操作只写入一个节点

balance=0,1,2,3  --是否启用读写分离。
0:为不启用读写分离;1:为启用读写分离,只有非写入主库的其他库分担读请求,常用配置;
2:为所有读操作都随机在主从节点分发;3:为只在readhost上分发读操作负载,不在writehost上分担负载;

switchType="-1,1,2,3"  --是否启用主从切换
-1:表示不启用主从切换;1:为默认值,自动切换;2:基于主从同步的状态,决定是否切换,与show slave status心跳对应;
3:基于多住galary集群切换,与show status like 'wsrep%'对应;

<heartbeat>show slave status</heartbeat>  --心跳检测语句,一般为select user();
如果是switchType=2,基于主从同步状态决定是否切换,则心跳设为show slave status;
如果是switchType=3,则心跳设为show status like 'wsrep%';

<writeHost host="mysql1" url="192.168.10.22:3306" user="root" password="oracle">
<readHost host="mysql2" url="192.168.10.23:3306" user="root" password="oracle" weight="1" />
</writeHost>
<writeHost host="mysql2" url="192.168.10.23:3306" user="root" password="oracle">
</writeHost>
--具体写服务器的设置,每个host都对应一个DB,需要设置连接mysql DB的URL、用户名和密码,主机的名称都不能相同;
对于主机设置的数量需要事先规划好。
情况一:对于不使用mycat做高可用和读写分离的情况,只需要设置一个写主机writeHost即可;
情况二:对于使用mycat做读写分离的情况,可以设置一个writeHost作为写节点,设置一个对应的读节点readHost,
采用balance=3的方式进行读写分离;也可以设置多个writeHost,只在第一个writeHost上写入,其他都根据负载均衡读请求;
情况一:对于使用mycat做高可用的情况,必须设置两个、或两个以上的writeHost节点,这样才可以切换;
高可用可以根据不同情况设置不同的心跳,主从切换方式。

从物理上讲,mycat中定义的writeHost和readHost,在物理上,都是只有写请求的那个writeHost是对应Master数据库的,
其他的writeHost和readHost都是对应slave节点的。
一般实际应用中,也是将读写分离和高可用结合起来使用,将物理上的slave设置为writeHost,即可实现读写分离,也可以高可用。
mycat中switchType=1,2,3 这三种情况,分别适用于不同的场景,设置为1,自动切换,是指mycat在屋里master宕机时,才会主从切换。

配置mycat:
vi schema.xml

<schema name="db1" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">
       </schema>       
       <schema name="db2" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn2">
       </schema>
       <schema name="db3" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn3">
       </schema>
        <dataNode name="dn1" dataHost="mysql1" database="db1" />
        <dataNode name="dn2" dataHost="mysql1" database="db2" />
        <dataNode name="dn3" dataHost="mysql1" database="db3" />

<!--######### TESTDB脢戮脻麓 ########-->
        <dataHost name="mysql1" maxCon="1000" minCon="10" balance="1"
                writeType="0" dbType="mysql" dbDriver="native" switchType="2"  slaveThreshold="100">
                <heartbeat>show slave status</heartbeat>
                <writeHost host="mysql1" url="192.168.10.22:3306" user="root" password="oracle">
                </writeHost>

<writeHost host="mysql2" url="192.168.10.23:3306" user="root" password="oracle">
                </writeHost>
        </dataHost>
</mycat:schema>

vi server.xml
        <user name="root">
                <property name="password">oracle</property>
                <property name="schemas">db1,db2,db3</property>
        </user>

[root@mycat1 logs]# tail -800 wrapper.log 
STATUS | wrapper  | 2016/02/21 04:36:45 | --> Wrapper Started as Daemon
STATUS | wrapper  | 2016/02/21 04:36:45 | Launching a JVM...
INFO   | jvm 1    | 2016/02/21 04:36:46 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
INFO   | jvm 1    | 2016/02/21 04:36:46 |   Copyright 1999-2006 Tanuki Software, Inc.  All Rights Reserved.
INFO   | jvm 1    | 2016/02/21 04:36:46 | 
INFO   | jvm 1    | 2016/02/21 04:36:46 | log4j 2016-02-21 04:36:46 [./conf/log4j.xml] load completed.
INFO   | jvm 1    | 2016/02/21 04:36:47 | MyCAT Server startup successfully. see logs in logs/mycat.log

[root@mycat1 logs]# netstat -an|grep 9066
tcp        0      0 :::9066                     :::*                        LISTEN

[root@mycat1 logs]# tail -800 mycat.log
......
2/21 04:42:17.174  DEBUG [$_NIOREACTOR-1-RW] (PhysicalDatasource.java:403) -release channel 
MySQLConnection [id=9, lastTime=1456000937137, user=root, schema=db1, old shema=db1, borrowed=true, 
fromSlaveDB=false, threadId=30, charset=utf8, txIsolation=0, autocommit=true, attachment=null, 
respHandler=null, host=192.168.10.22, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
02/21 04:42:17.175  DEBUG [$_NIOREACTOR-3-RW] (PhysicalDatasource.java:403) -release channel 
MySQLConnection [id=11, lastTime=1456000937173, user=root, schema=db1, old shema=db1, borrowed=true, 
fromSlaveDB=false, threadId=4, charset=latin1, txIsolation=3, autocommit=true, attachment=null, 
respHandler=null, host=192.168.10.23, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]

测试验证读写分离模式:
[root@mycat1 conf]# mysql -uroot -poracle -h192.168.10.21 -P8066
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.8-mycat-1.5-alpha-20151221110028 MyCat Server (OpenCloundDB)
Copyright (c) 2000, 2013, 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> show databases;
+----------+
| DATABASE |
+----------+
| db1      |
| db2      |
| db3      |
+----------+
3 rows in set (0.02 sec)

use db1;
create table company(id int not null primary key,name varchar(100));
insert into company(id,name) values(1,'alibaba');
查看mycat.log:
02/21 06:28:07.085  DEBUG [$_NIOREACTOR-0-RW] (ServerQueryHandler.java:56) -ServerConnection [id=5, schema=db1, host=192.168.10.21, user=root,txIsolation=3, autocommit=true, schema=db1]insert into company(id,name) values(1,'alibaba')
02/21 06:28:07.085  DEBUG [$_NIOREACTOR-0-RW] (NonBlockingSession.java:113) -ServerConnection [id=5, schema=db1, host=192.168.10.21, user=root,txIsolation=3, autocommit=true, schema=db1]insert into company(id,name) values(1,'alibaba'), route={
   1 -> dn1{insert into company(id,name) values(1,'alibaba')}
} rrs 
02/21 06:28:07.086  DEBUG [$_NIOREACTOR-0-RW] (MySQLConnection.java:445) -con need syn ,total syn cmd 2 commands SET names latin1;SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;schema change:false con:MySQLConnection [id=1, lastTime=1456007287085, user=root, schema=db1, old shema=db1, borrowed=true, fromSlaveDB=false, threadId=24, charset=latin1, txIsolation=0, autocommit=true, attachment=dn1{insert into company(id,name) values(1,'alibaba')}, respHandler=SingleNodeHandler [node=dn1{insert into company(id,name) values(1,'alibaba')}, packetId=0], host=192.168.10.22, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=true]
02/21 06:28:07.124  DEBUG [$_NIOREACTOR-1-RW] (NonBlockingSession.java:229) -release connection MySQLConnection [id=1, lastTime=1456007287072, user=root, schema=db1, old shema=db1, borrowed=true, fromSlaveDB=false, threadId=24, charset=latin1, txIsolation=3, autocommit=true, attachment=dn1{insert into company(id,name) values(1,'alibaba')}, respHandler=SingleNodeHandler [node=dn1{insert into company(id,name) values(1,'alibaba')}, packetId=0], host=192.168.10.22, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=true]

连接mycat进行select操作:
select * from company;
查看mycat.log:
02/21 06:31:17.261  DEBUG [$_NIOREACTOR-0-RW] (ServerQueryHandler.java:56) -ServerConnection[id=5, schema=db1, host=192.168.10.21, user=root,txIsolation=3, autocommit=true, schema=db1]select * from company
02/21 06:31:17.265  DEBUG [$_NIOREACTOR-0-RW] (EnchachePool.java:76) -SQLRouteCache  miss cache ,key:db1select * from company
02/21 06:31:17.265  DEBUG [$_NIOREACTOR-0-RW] (NonBlockingSession.java:113) -ServerConnection [id=5, schema=db1, host=192.168.10.21, user=root,txIsolation=3, autocommit=true, schema=db1]select * from company, route={
   1 -> dn1{select * from company}
} rrs 
02/21 06:31:17.265  DEBUG [$_NIOREACTOR-0-RW] (PhysicalDBPool.java:452) -select read source mysql2 for dataHost:mysql1
02/21 06:31:17.267  DEBUG [$_NIOREACTOR-3-RW] (NonBlockingSession.java:229) -release connection MySQLConnection [id=11, lastTime=1456007477250, user=root, schema=db1, old shema=db1, borrowed=true, fromSlaveDB=false, threadId=4, charset=latin1, txIsolation=3, autocommit=true, attachment=dn1{select * from company}, respHandler=SingleNodeHandler [node=dn1{select * from company}, packetId=5], host=192.168.10.23, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
经过多次观察,select * from company都去读从库了;

查看管理信息:
[root@mycat1 conf]# mysql -uroot -poracle -h192.168.10.21 -P9066
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.5.8-mycat-1.5-alpha-20151221110028 MyCat Server (monitor)
Copyright (c) 2000, 2013, 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> show @@heartbeat;
+--------+-------+---------------+------+---------+-------+--------+---------+--------------+---------------------+-------+
| NAME   | TYPE  | HOST          | PORT | RS_CODE | RETRY | STATUS | TIMEOUT | EXECUTE_TIME | LAST_ACTIVE_TIME    | STOP  |
+--------+-------+---------------+------+---------+-------+--------+---------+--------------+---------------------+-------+
| mysql1 | mysql | 192.168.10.22 | 3306 |       1 |     0 | idle   |       0 | 5,12,10      | 2016-02-21 18:12:07 | false |
| mysql2 | mysql | 192.168.10.23 | 3306 |       1 |     0 | idle   |       0 | 1,41,15      | 2016-02-21 18:12:07 | false |
+--------+-------+---------------+------+---------+-------+--------+---------+--------------+---------------------+-------+
2 rows in set (0.04 sec)

mysql> show @@backend;
+------------+------+---------+---------------+------+--------+----------+---------+-------+--------+----------+------------+--------+----------+---------+------------+
| processor  | id   | mysqlId | host          | port | l_port | net_in   | net_out | life  | closed | borrowed | SEND_QUEUE | schema | charset  | txlevel | autocommit |
+------------+------+---------+---------------+------+--------+----------+---------+-------+--------+----------+------------+--------+----------+---------+------------+
| Processor0 |   13 |      32 | 192.168.10.22 | 3306 |  36280 |  1391408 |   13288 | 47798 | false  | false    |          0 | db1    | utf8:33  | 0       | true       |
| Processor0 |    8 |      26 | 192.168.10.22 | 3306 |  36270 |  1451598 |   13860 | 48998 | false  | false    |          0 | db1    | utf8:33  | 0       | true       |
| Processor1 |    5 |      28 | 192.168.10.22 | 3306 |  36272 |   226963 |    2222 | 48998 | false  | false    |          0 | db2    | utf8:33  | 0       | true       |
| Processor1 |    9 |      30 | 192.168.10.22 | 3306 |  36274 |  1449316 |   13983 | 48998 | false  | false    |          0 | db1    | latin1:8 | 3       | true       |
| Processor1 |    1 |      24 | 192.168.10.22 | 3306 |  36268 |  1449316 |   13964 | 48998 | false  | false    |          0 | db1    | latin1:8 | 3       | true       |
| Processor1 |   14 |      33 | 192.168.10.22 | 3306 |  36281 |  1368258 |   13068 | 47198 | false  | false    |          0 | db1    | utf8:33  | 0       | true       |
| Processor2 |   10 |      23 | 192.168.10.22 | 3306 |  36267 |  1451598 |   13860 | 48998 | false  | false    |          0 | db1    | utf8:33  | 0       | true       |
| Processor2 |   15 |      34 | 192.168.10.22 | 3306 |  36282 |  1347423 |   12870 | 46598 | false  | false    |          0 | db1    | utf8:33  | 0       | true       |
| Processor3 |   12 |      31 | 192.168.10.22 | 3306 |  36279 |  1433078 |   13684 | 48698 | false  | false    |          0 | db1    | utf8:33  | 0       | true       |
| Processor3 |    7 |      27 | 192.168.10.22 | 3306 |  36271 |   226963 |    2222 | 48998 | false  | false    |          0 | db2    | utf8:33  | 0       | true       |
| Processor3 |   11 |       4 | 192.168.10.23 | 3306 |  60806 | 13061320 |  107991 | 48998 | false  | false    |          0 | db1    | latin1:8 | 3       | true       |
+------------+------+---------+---------------+------+--------+----------+---------+-------+--------+----------+------------+--------+----------+---------+------------+
11 rows in set (0.01 sec)

现在停止主库,看看还能否正常提供服务:
[root@mysql1 mysql]# /etc/init.d/mysql stop
Shutting down MySQL............[  OK  ]

查看mycat.log:
02/21 18:22:58.508   INFO [$_NIOConnector] (SQLJob.java:111) -can't get connection for sql :show slave status
02/21 18:22:58.508   INFO [$_NIOConnector] (PhysicalDatasource.java:373) -not ilde connection in pool,create new connection for mysql1 of schema db1
02/21 18:22:58.510   INFO [$_NIOConnector] (AbstractConnection.java:458) -close connection,reason:java.net.ConnectException: Connection refused ,MySQLConnection [id=0, lastTime=1456050178505, user=root, schema=db1, old shema=db1, borrowed=false, fromSlaveDB=false, threadId=0, charset=utf8, txIsolation=0, autocommit=true, attachment=null, respHandler=null, host=192.168.10.22, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
02/21 18:22:58.510   INFO [$_NIOConnector] (SQLJob.java:111) -can't get connection for sql :show slave status
02/21 18:22:58.510   INFO [$_NIOConnector] (PhysicalDatasource.java:373) -not ilde connection in pool,create new connection for mysql1 of schema db1
02/21 18:22:58.511   INFO [$_NIOConnector] (AbstractConnection.java:458) -close connection,reason:java.net.ConnectException: Connection refused ,MySQLConnection [id=0, lastTime=1456050178505, user=root, schema=db1, old shema=db1, borrowed=false, fromSlaveDB=false, threadId=0, charset=utf8, txIsolation=0, autocommit=true, attachment=null, respHandler=null, host=192.168.10.22, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
02/21 18:22:58.511   INFO [$_NIOConnector] (SQLJob.java:111) -can't get connection for sql :show slave status
02/21 18:22:58.511   INFO [$_NIOConnector] (PhysicalDatasource.java:373) -not ilde connection in pool,create new connection for mysql1 of schema db1
02/21 18:22:58.513   INFO [$_NIOConnector] (AbstractConnection.java:458) -close connection,reason:java.net.ConnectException: Connection refused ,MySQLConnection [id=0, lastTime=1456050178505, user=root, schema=db1, old shema=db1, borrowed=false, fromSlaveDB=false, threadId=0, charset=utf8, txIsolation=0, autocommit=true, attachment=null, respHandler=null, host=192.168.10.22, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]

再次连接mycat,看看是否还能正常进行操作:
[root@mycat1 conf]# mysql -uroot -poracle -h192.168.10.21 -P8066
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.5.8-mycat-1.5-alpha-20151221110028 MyCat Server (OpenCloundDB)
Copyright (c) 2000, 2013, 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> show databases;
+----------+
| DATABASE |
+----------+
| db1      |
| db2      |
| db3      |
+----------+
3 rows in set (0.01 sec)

mysql> use db1;
Database changed
mysql> select * from company;
+----+---------+
| id | name    |
+----+---------+
|  1 | alibaba |
+----+---------+
1 row in set (0.00 sec)

mysql> insert into company(id,name) values(2,'jingdong');
Query OK, 1 row affected (0.09 sec)

mysql> select * from company;
+----+----------+
| id | name     |
+----+----------+
|  1 | alibaba  |
|  2 | jingdong |
+----+----------+
2 rows in set (0.02 sec)
可以看出,还可以正常操作。

再查看管理信息:
[root@mycat1 conf]# mysql -uroot -poracle -h192.168.10.21 -P9066
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.5.8-mycat-1.5-alpha-20151221110028 MyCat Server (monitor)

Copyright (c) 2000, 2013, 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> show @@heartbeat;
+--------+-------+---------------+------+---------+-------+--------+---------+--------------+---------------------+-------+
| NAME   | TYPE  | HOST          | PORT | RS_CODE | RETRY | STATUS | TIMEOUT | EXECUTE_TIME | LAST_ACTIVE_TIME    | STOP  |
+--------+-------+---------------+------+---------+-------+--------+---------+--------------+---------------------+-------+
| mysql1 | mysql | 192.168.10.22 | 3306 |      -1 |     0 | idle   |       0 | 2,1,3        | 2016-02-21 18:29:58 | false |
| mysql2 | mysql | 192.168.10.23 | 3306 |       1 |     0 | idle   |       0 | 10,4,18      | 2016-02-21 18:29:58 | false |
+--------+-------+---------------+------+---------+-------+--------+---------+--------------+---------------------+-------+
2 rows in set (0.01 sec)

mysql> show @@backend;
+------------+------+---------+---------------+------+--------+----------+---------+-------+--------+----------+------------+--------+----------+---------+------------+
| processor  | id   | mysqlId | host          | port | l_port | net_in   | net_out | life  | closed | borrowed | SEND_QUEUE | schema | charset  | txlevel | autocommit |
+------------+------+---------+---------------+------+--------+----------+---------+-------+--------+----------+------------+--------+----------+---------+------------+
| Processor0 |   16 |       5 | 192.168.10.23 | 3306 |  60959 |       93 |      66 |   270 | false  | false    |          0 | db3    | utf8:33  | 0       | true       |
| Processor1 |   17 |       6 | 192.168.10.23 | 3306 |  60960 |    33756 |     483 |   270 | false  | false    |          0 | db1    | latin1:8 | 3       | true       |
| Processor2 |   18 |       7 | 192.168.10.23 | 3306 |  60961 |       93 |      66 |   270 | false  | false    |          0 | db2    | utf8:33  | 0       | true       |
| Processor3 |   11 |       4 | 192.168.10.23 | 3306 |  60806 | 13320587 |  110199 | 50071 | false  | false    |          0 | db1    | latin1:8 | 3       | true       |
+------------+------+---------+---------------+------+--------+----------+---------+-------+--------+----------+------------+--------+----------+---------+------------+
4 rows in set (0.00 sec)

源主库恢复后,现在主从角色对换了,需要进行如下操作:
恢复原主节点的操作:
先停止源从节点的从复制操作,在启动原主节点的从复制操作
在原来的从库,也就是从现在的主库:
mysql> stop slave;
Query OK, 0 rows affected (0.09 sec)

并将DB1库导出:
/usr/local/mysql/bin/mysqldump -uroot -poracle --master-data=2 --single-transaction -R -B db1 > /opt/db1.sql
并将/opt/db1.sql传送到原来的主库。

在原来的主库上:
[root@mysql1 mysql]# /etc/init.d/mysql start
Starting MySQL......[  OK  ]

mysql> drop table company;
Query OK, 0 rows affected (0.09 sec)

mysql> source /opt/db1.sql;
......

mysql> change master to master_host='192.168.10.23',
    -> master_port=3306, master_user='repluser', 
    -> master_password='oracle', master_log_file='mysql-bin.000003',master_log_pos=324;
Query OK, 0 rows affected, 2 warnings (0.24 sec)

mysql> start slave;
Query OK, 0 rows affected (0.06 sec)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.10.23
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 324
               Relay_Log_File: my3306-relay-bin.000002
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
已经恢复正常。

再次登录mycat,进行操作:
[root@mycat1 conf]# mysql -uroot -poracle -h192.168.10.21 -P8066
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.5.8-mycat-1.5-alpha-20151221110028 MyCat Server (OpenCloundDB)
Copyright (c) 2000, 2013, 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> show databases;
+----------+
| DATABASE |
+----------+
| db1      |
| db2      |
| db3      |
+----------+
3 rows in set (0.00 sec)

查看mycat.log
02/21 18:54:34.371  DEBUG [$_NIOREACTOR-3-RW] (ServerQueryHandler.java:56) -ServerConnection [id=10, schema=db1, host=192.168.10.21, user=root,txIsolation=3, autocommit=true, schema=db1]select * from company
02/21 18:54:34.371  DEBUG [$_NIOREACTOR-3-RW] (EnchachePool.java:76) -SQLRouteCache  miss cache ,key:db1select * from company
02/21 18:54:34.372  DEBUG [$_NIOREACTOR-3-RW] (NonBlockingSession.java:113) -ServerConnection [id=10, schema=db1, host=192.168.10.21, user=root,txIsolation=3, autocommit=true, schema=db1]select * from company, route={
   1 -> dn1{select * from company}
} rrs 
02/21 18:54:34.372  DEBUG [$_NIOREACTOR-3-RW] (PhysicalDBPool.java:452) -select read source mysql1 for dataHost:mysql1
02/21 18:54:34.375  DEBUG [$_NIOREACTOR-1-RW] (NonBlockingSession.java:229) -release connection MySQLConnection [id=24, lastTime=1456052074371, user=root, schema=db1, old shema=db1, borrowed=true, fromSlaveDB=false, threadId=1, charset=latin1, txIsolation=3, autocommit=true, attachment=dn1{select * from company}, respHandler=SingleNodeHandler [node=dn1{select * from company}, packetId=6], host=192.168.10.22, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
02/21 18:54:34.375  DEBUG [$_NIOREACTOR-1-RW] (PhysicalDatasource.java:403) -release channel MySQLConnection [id=24, lastTime=1456052074371, user=root, schema=db1, old shema=db1, borrowed=true, fromSlaveDB=false, threadId=1, charset=latin1, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=192.168.10.22, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
02/21 18:54:36.670  DEBUG [$_NIOREACTOR-3-RW] (ServerQueryHandler.java:56) -ServerConnection [id=10, schema=db1, host=192.168.10.21, user=root,txIsolation=3, autocommit=true, schema=db1]select * from company
02/21 18:54:36.671  DEBUG [$_NIOREACTOR-3-RW] (EnchachePool.java:76) -SQLRouteCache  miss cache ,key:db1select * from company
02/21 18:54:36.671  DEBUG [$_NIOREACTOR-3-RW] (NonBlockingSession.java:113) -ServerConnection [id=10, schema=db1, host=192.168.10.21, user=root,txIsolation=3, autocommit=true, schema=db1]select * from company, route={
   1 -> dn1{select * from company}
} rrs 
02/21 18:54:36.671  DEBUG [$_NIOREACTOR-3-RW] (PhysicalDBPool.java:452) -select read source mysql1 for dataHost:mysql1
02/21 18:54:36.673  DEBUG [$_NIOREACTOR-1-RW] (NonBlockingSession.java:229) -release connection MySQLConnection [id=24, lastTime=1456052076671, user=root, schema=db1, old shema=db1, borrowed=true, fromSlaveDB=false, threadId=1, charset=latin1, txIsolation=3, autocommit=true, attachment=dn1{select * from company}, respHandler=SingleNodeHandler [node=dn1{select * from company}, packetId=6], host=192.168.10.22, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
发现select都在原来的主库上了。

Mycat读写分离、主从切换学习(转)的更多相关文章

  1. Mycat读写分离、主从切换、分库分表的操作记录

    系统开发中,数据库是非常重要的一个点.除了程序的本身的优化,如:SQL语句优化.代码优化,数据库的处理本身优化也是非常重要的.主从.热备.分表分库等都是系统发展迟早会遇到的技术问题问题.Mycat是一 ...

  2. mycat读写分离与主从切换【转】

    什么是mycat,以及mycat的优点和特性本文不做赘述,本文继续本着实战的态度,来分享一些个人对mycat的基础功能实践.本文mycat的读写分离和主从切换的环境为mysql主从环境. 如何安装my ...

  3. mycat读写分离与主从切换

    1, 分库分表的优缺点.以及为什么分表方式无法成为主流? 分表:在台server上,长处是易维护,相似表分区.缺点是在一台dbserver上.无法分担IO.负载集中. 分库:在多台server上,长处 ...

  4. mysql主从同步+mycat读写分离+.NET程序连接mycat代理

    背景 最近新项目需要用到mysql数据库,并且由于数据量大的原因,故打算采用1主1从(主数据库负责增.删.改操作:从数据库负责查操作)的数据库架构,在实现主从之后还要实现读写分离的代理,在网上搜寻了很 ...

  5. LVS+MYCAT读写分离+MYSQL同步部署手册(第三版)

    1      配置MYSQL主备同步 1.1    测试环境 mysql版本:5.6.24: 操作系统内核版本:Linux-3.13-0-32 主数据库IP:192.168.10.3: 主数据库名:d ...

  6. LVS+MYCAT+读写分离+MYSQL主备同步部署手册

    LVS+MYCAT+读写分离+MYSQL主备同步部署手册 1          配置MYSQL主备同步…. 2 1.1       测试环境… 2 1.2       配置主数据库… 2 1.2.1  ...

  7. 【转载】LVS+MYCAT+读写分离+MYSQL主备同步部署手册(邢锋)

    LVS+MYCAT+读写分离+MYSQL主备同步部署手册 1          配置MYSQL主备同步…. 2 1.1       测试环境… 2 1.2       配置主数据库… 2 1.2.1  ...

  8. MyCat读写分离-笔记(四)

    概述 Mycat能够实现数据库读写分离,不能实现主从同步,数据库的备份还是基于数据库层面的.Mycat只是数据库的中间件: Mycat读写分离配置 在MySQL中间件出现之前,对于MySQL主从集群, ...

  9. Mycat 读写分离

    简介 Mycat 是 MySQL中间件,Mycat的原理中最重要的一个动词就是'拦截',它拦截了用户发送过来的SQL语句,首先对SQL语句做了一些特定的分析:如分片分析.路由分析.读写分离分析.缓存分 ...

随机推荐

  1. 加快Qemu Aarch32虚拟开发板的启动速度

    软件版本 Qemu: 2.8.0 虚拟开发板: vexpress-ca9 概述 之前的博文介绍了将Python移植到开发板上, 根文件系统采用的是ramdisk, 这个文件系统的缺点是修改的内容重启会 ...

  2. AngularJS过滤排序思路

    本篇主要整理使用AngularJS进行过滤排序的思路. 在controller中,$scope的persons字段存储数组. $scope.persons = [ { "name" ...

  3. ASP.NET Web API实践系列11,如何设计出优秀的API

    本篇摘自:InfoQ的微信公众号 在设计API的时候考虑的问题包括:API所使用的传输协议.支持的消息格式.接口的控制.名称.关联.次序,等等.我们很难始终作出正确的决策,很可能是在多次犯错之后,并从 ...

  4. Delphi的命令行编译命令

    Borland出品的Delphi,有着闪电般的编译速度,但是在界面控件使用较多.工程项目较大的时候,编译一个工程仍需要一段时间,打开庞大的Delphi IDE,也需要时间.其实,在一个工程开发结束,调 ...

  5. CListCtrlEx:一个支持文件拖放和实时监视的列表控件——用未公开API函数实现Shell实时监视

    一.需求无论何时,当你在Explorer窗口中创建.删除或重命名一个文件夹/文件,或者插入拔除移动存储器时,Windows总是能非常快速地更新它所有的视图.有时候我们的程序中也需要这样的功能,以便当用 ...

  6. 11i and R12 Table Count in Different Module

    Advertisement Module 11i Tables R12 Tables New Tables AR 551 616 118 BOM 264 337 73 GL 186 309 140 A ...

  7. 神探夏洛克第一季/全集Sherlock1迅雷下载

    第一季 Sherlock Season 1 (2010)看点:夏洛克·福尔摩斯(Sherlock Holmes)是一个虚构的侦探人物,是由19世纪末的英国侦探小说家阿瑟·柯南·道尔所塑造的一个才华横溢 ...

  8. Orchard模块开发全接触7:订单与支付之Event Bus

    在这部分,我们要完成的工作有: 1:将购物车内的商品变成真正的订单: 2:理解 父子及一对多关系: 3:写一个针对 Event Bus 的扩展点: 4:实现一个针对该扩展点的模拟的 支付服务: 一:创 ...

  9. Just-In-Time Debugging in Visual Studio 禁止VS在服务器上调试

    To disable Just-In-Time debugging by editing the registry On the Start menu, search for and run rege ...

  10. 【BZOJ】【3697】采药人的路径&【3127】【USACO2013 Open】Yin and Yang

    点分治 Orz hzwer 倒是比较好想到点分治……然而在方案统计这里,我犯了两个错误…… 1.我比较傻逼的想的是:通过儿子来更新父亲,也就是统计以x为根的子树中xxxx的路径有多少条……这样转移. ...