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

How to install and use MyCAT

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces how to install and use MyCAT, with a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

MyCAT installation

The operation of mycat depends on the jre environment. You need to install jdk7 or above first.

Check the jdk version shell > java-versionjava version "1.7.0,79" Java (TM) SE Runtime Environment (build 1.7.0_79-b15) Java HotSpot (TM) 64-Bit Server VM (build 24.79-b02, mixed mode) download the mycat server1.5 installation file shell > tar zxvf Mycat-server-1.5-RELEASE-20160301083012-linux.tar.gz to create mycat users And set the environment variable shell > groupadd mycatshell > useradd-g mycat mycatshell > passwd mycatshell > chown-R mycat.mycat / usr/local/mycatshell > vim / etc/profile to add the following content: export MYCAT_HOME=/usr/local/mycatshell > source / etc/profile test environment introduction

Master:10.106.58.178

Slave:10.106.58.179

Mycat:10.106.58.174

Mycat configuration

The mycat1.5 configuration file can be configured according to the local XML file, or it can be implemented using zookeeper. This example describes the use of local XML to configure.

Profile introduction

Conf/rule.xml defines sharding rules

Conf/schema.xml defines logical libraries, tables, sharding nodes, etc.

Conf/server.xml defines user authorization and configuration related to server parameters

Shell > more conf/schema.xml select user () shell > more conf/rule.xml id func1 8 128 server.xml 's user defines the user used by the client to connect to mycat to verify that the user of schema.xml is the user used by mycat to connect to the database shell > more conf/server.xml druidparser test test starts the test mycat to view wrapper.log Check whether the startup is successful shell > / usr/local/mycat/bin/mycat start connect to the database through mycat shell > mysql-h 10.106.58.174-P8066-u test-pmysql > select @ @ hostname +-+ | @ @ hostname | +-+ | mysql178 | +-+ 1 row in set (0.13 sec) mysql > create table user (id int primary key,name varchar (100)); Query OK, 0 rows affected (0.13 sec) mysql > insert into user (id,name) values; Query OK, 1 row affected (0.07 sec) query from library mysql > select @ @ hostname; select * from test.user +-+ | @ @ hostname | +-+ | mysql179 | +-+ 1 row in set (0.00 sec) +-+-+ | id | name | +-+-+ | 1 | xiaoming | +-+-+ 1 row in set (0.00 sec) Test for read-write separation

Mycat provides several forms of request load balancer distribution, which can be modified by defining the balance attribute of the dataHost tag

Balance=0, the read-write separation mechanism is not enabled, and all read and write operations are sent to the currently available writeHost

Balance=1, all readHost and stand by writeHost participate in the load balancing of select statements, such as double master and double slave mode, M2, S1, S2 all distribute requests

Balance=2, all read operations are randomly distributed on writeHost and readHost

Balance=3, all read requests are randomly distributed to the corresponding readHost on writeHost for execution, and writeHost does not bear the read pressure. (balance=3 only supports versions above mycat1.4)

Set balance=1 and change log4j to debug mode, and observe the execution result.

By performing a write operation, you can see that the distribution to the writeHost

Mysql > insert into test.user (id,name) values (2meme Xiaoli')

By performing the query operation, you can see that the distribution to the readHost

Mysql > select * from test.user where id = 2

Test failover

Mycat defines the failover method through the writeType and switchType of the dataHost tag.

WriteType=0, which means that all writes are sent to the first writeHost configured, and the first one is hung up and switched to the second writeHost. After restart, the switch shall prevail, and the switch will be recorded in conf/dnindex.properties.

WriteType=1, which means that all writes are randomly sent to the configured writeHost. The above mycat1.5 is discarded and not recommended.

SwitchType=-1, which means that the switch is not automatic

SwitchType=1, the default value, indicates automatic switching

SwitchType=2, decide whether to switch based on the status of MySQL master-slave synchronization, and the heartbeat statement is show slave status.

SwitchType=3, galaru cluster-based handover mechanism (suitable for cluster), supported by mycat1.4 or above, heartbeat statement is show status like 'wsrep%'.

Modify switchType=2,writeType=0, heartbeat language show slave status

Close Mastershell > service mysql stop

Perform the insert operation through mycat and find that the request is distributed to the second writeHost mysql > insert into test.user values (3 Query OK, 1 row affected (0.04 sec)

Restart the original Mastershell > service mysql start to view the current write node. 0 means the first, 1 means the second shell > more conf/dnindex.properties#update#Fri Mar 04 15:51:39 CST 2016cluster1=1 writes data through mycat mysql > insert into test.user values; Query OK, 1 row affected (0.04 sec) mysql > select * from test.user where id = 4 +-+-+ | id | name | +-+-+ | 4 | xiaozheng | +-+-+ 1 row in set (0.01sec) View main database data mysql > select * from test.user +-+-- +-+ | id | name | +-+-+ | 1 | xiaoming | | 2 | xiaoli | +-+-- + 2 rows in set (0.00 sec) View data from the database mysql > select * from test.user +-+-- +-+ | id | name | +-+-+ | 1 | xiaoming | | 2 | xiaoli | | 3 | xiaowang | | 4 | xiaozheng | +-+-+ 4 rows in set (0.00 sec)

When the original master library is restored, mycat does not switch back to the first write node, but needs to reconfigure the master-slave state. The failover of mycat does not synchronize the master-slave binlog difference data, which cannot completely guarantee the data consistency, and does not record the binlog location when the switched writeHost provides services. When other slave libraries specify a new master database, you need to back up the complete data manually, which can be used in conjunction with MHA.

Thank you for reading this article carefully. I hope the article "how to install and use MyCAT" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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