Mycat讀寫分離、主從切換學習(轉)

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

 

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:在mysql一、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都在原來的主庫上了。node

相關文章
相關標籤/搜索