Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mycat realizes MySQL's sub-database and table, read-write separation and master-slave switching.

2025-03-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Recently, I have mainly invested in the project of Ping an Cloud MySQL DRDS. I am mainly responsible for the configuration of zookeeper module and the writing of background scripts. In order to deepen the understanding of the overall architecture of DRDS, I think it is necessary to manually build a Mysql distributed database cluster based on Mycat. I now share with you the construction process. Further research will be done on the configuration of Mycat. Please look forward to it.

Deployment of Mycat and MySQL instances:

Mycat:

IP:10.20.8.57,Port:3310/3311

MySQL:

Db1-M1,IP:10.20.8.126,Port:3306

Db1-M2,IP:10.20.8.126,Port:3307

Db2-M1,IP:10.25.80.7,Port:3307

The architecture diagram is as follows:

Configure Mycat

Server.xml:

10 druidparser 2 0 3310 3311 0 1 1m 1k 0 389m 123456 db false

Schema.xml:

Select user (); select user ()

Modify the following configuration items in rule.xml:

Id mod-long 2

For the meaning of the tags in the configuration file, please refer to the article: MyCat key configuration instructions

Start Mycat:

[root@SZB-L0059021 bin] #. / mycat startStarting Mycat-server... [root@SZB-L0059021 bin] #. / mycat statusMycat-server is running (27020). [root@SZB-L0059021 bin] # mysql-uroot-p123456-Ddb-h227.0.0.1-P3310

Sub-database and sub-table verification:

Mysql > show tables;+-+ | Tables in db | +-+ | T1 | +-+ 1 row in set (0.00 sec) mysql > desc T1 +-+ | Field | Type | Null | Key | Default | Extra | +-+- -+ | id | int (11) | YES | | NULL | db_name | varchar (20) | YES | | NULL | | +-+-+ 2 rows in set (0.01 sec) mysql > insert into T1 (id) Db_name) values (1 Magi database ()) Query OK, 1 row affected (0.01 sec) mysql > insert into T1 (id,db_name) values; Query OK, 1 row affected (0.03 sec) mysql > select * from T1 +-+-+ | id | db_name | +-+-+ | 2 | db1 |-- id=2, 0 for 2, so insert dn1 | 1 | db2 |-- id=1, 1 for 2 Therefore, insert dn2 +-+ 2 rows in set (0.01sec) the above query results show that the data inserted twice falls into db1 and db2, and the sub-database is realized.

Read-write separation verification:

Mysql > select * from T1 +-+-+ | id | db_name | +-+-+ | 2 | db1 |-- from db1-M2 on shard1 | 1 | db2 |-- from db2-M1+-+-+2 rows in set (0.01 sec) on shard2. The above query results are obtained from db1-M2 (port 3307) in dn1 and db2-M1:2018-05-08 15 db2-M1:2018 in dn2 03Parade 39.385 DEBUG [$_ NIOREACTOR-0-RW] (io.mycat.server.NonBlockingSession.execute (NonBlockingSession.java:110))-ServerConnection [id=1, schema=db, host=127.0.0.1, user=root,txIsolation=3, autocommit=true, schema=db] select * from T1 Route= {1-> dn1 {SELECT * FROM t1LIMIT 100} 2-> dn2 {SELECT * FROM t1LIMIT 100}... 2018-05-08 15 SELECT 0391 DEBUG [$_ NIOREACTOR-0-RW] (io.mycat.backend.mysql.nio.handler.MultiNodeQueryHandler.rowEofResponse (MultiNodeQueryHandler.java:311))-on row end reseponse MySQLConnection [id=29, lastTime=1525763019368, user=root, schema=db1, old shema=db1, borrowed=true, fromSlaveDB=true, threadId=511, charset=utf8, txIsolation=3, autocommit=true, attachment=dn1 {SELECT * FROM t1LIMIT 100} RespHandler=io.mycat.backend.mysql.nio.handler.MultiNodeQueryHandler@66328ec4, host=10.20.8.126, port=3307, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]... 2018-05-08 15 port=3307 03statusSync=null 39.392 DEBUG [$_ NIOREACTOR-0-RW] (io.mycat.backend.mysql.nio.handler.MultiNodeQueryHandler.rowEofResponse (MultiNodeQueryHandler.java:311))-on rowe nd reseponse MySQLConnection [id=3, lastTime=1525763019387, user=root, schema=db2, old shema=db2, borrowed=true, fromSlaveDB=false, threadId=28, charset=utf8, txIsolation=3, autocommit=true, attachment=dn2 {SELECT * FROM t1LIMIT 100} RespHandler=io.mycat.backend.mysql.nio.handler.MultiNodeQueryHandler@66328ec4, host=10.25.80.7, port=3307, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]...

Master-slave switch:

By looking at the dnindex.properties file in the mycat/conf directory, you can see that the writeHost,0 that mycat is using represents the first writeHost under the dataHost tag in the schema.xml.

[root@SZB-L0059021 conf] # cat dnindex.properties # update#Tue May 08 12:59:24 CST 2018shard2=0shard1=0-when the state is normal, mycat selects the first writeHost in each dataHost tag as the write entry

Switch to 10.20.8.126 host and manually stop db1-M1

10.20.8.126:3306:Master > mysqladmin-uroot-p123456 shutdown

Go back to 10.20.8.57 (mycat host) and look at dnindex.properties again

[root@SZB-L0059021 conf] # cat dnindex.properties # update#Tue May 08 15:12:12 CST 2018shard2=0shard1=1-- after db1-M1 is shutdown, mycat's writeHost on shard1 switches to db1-M2

Switch to 10.20.8.57 (the host where mycat resides) and execute the following insert command

Mysql > insert into T1 (id,db_name) values (4Jing database ()) Query OK, 1 row affected (0.01sec) check the log Mycat now chooses to write data through db1-M2 (port 3307): 2018-05-08 15 DEBUG 13NIOREACTOR-0-RW 44.987 DEBUG [$_ NIOREACTOR-0-RW] (io.mycat.server.NonBlockingSession.releaseConnection (NonBlockingSession.java:341))-release connection MySQLConnection [id=24, lastTime=1525763624968, user=root, schema=db1, old shema=db1, borrowed=true, fromSlaveDB=false, threadId=506, charset=utf8, txIsolation=3, autocommit=true, attachment=dn1 {insert into T1 (id,db_name) values (4 Magnum database ())}, respHandler=SingleNodeHandler [node=dn1 {insert into T1 (id,db_name) values (4) Database ()}, packetId=1], host=10.20.8.126, port=3307, statusSync=null, writeQueue=0, modifiedSQLExecuted=true]

Because writeType= "0" is configured in dataHost, even after db1-M1 restart and recovery, Mycat will still choose db1-M2 as the shard1

WriteHost .

Verify:

Switch to the 10.20.8.126 host and manually start db1-M110.20.8.126:3306:Master > mysqld_safe & insert validation data mysql > insert into T1 (id,db_name) values (6 Magi database ()) into Mycat Query OK, 1 row affected (0.02 sec) check the log The data is still written through db1-M2 (port 3307): 2018-05-08 15 NIOREACTOR-0-RW 16charge 09.579 DEBUG [$_ NIOREACTOR-0-RW] (io.mycat.server.NonBlockingSession.releaseConnection (NonBlockingSession.java:341))-release connection MySQLConnection [id=32, lastTime=1525763769548, user=root, schema=db1, old shema=db1, borrowed=true, fromSlaveDB=false, threadId=514, charset=utf8, txIsolation=3, autocommit=true, attachment=dn1 {insert into T1 (id,db_name) values (6mgb ())}, respHandler=SingleNodeHandler [node=dn1 {insert into T1 (id,db_name) values (6jrtb ())} PacketId=1], host=10.20.8.126, port=3307, statusSync=null, writeQueue=0, modifiedSQLExecuted=true]

At this point, if you want to change Mycat's writeHost on shard1 to db1-M1 again, simply change shard1=1 in dnindex.properties to shard1=0 and restart Mycat.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report