Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing Database Read-Write Separation Using MyCAT

Tech Nov 28 13

MyCAT Distributed Storage Service Deployment

1. Installing Java Runtime Environment

[root@db01~]# yum install -y java

2. Downloading and Extracting MyCAT

# Upload and extract MyCAT package
[root@db01 ~]# tar xf Mycat-server-1.6.7.4-release-20200105164103-linux_.tar.gz -C /usr/local/
[root@db01 ~]# ll /usr/local/mycat

# Configure environment variables
[root@db01~]# vim /etc/profile
export PATH=/usr/local/mycat/bin:$PATH
[root@db01~]# source /etc/profile

3. Starting Service and Testing Connection

[root@db01~]# mycat start
[root@db01~]# mysql -uroot -p123456 -h 127.0.0.1 -P8066
# or
[root@db01~]# mysql -uroot -p123456 -h 127.0.0.1 -P8066 --default-auth=mysql_native_password

Connection issues may occur due to Java driver compatibility between MyCAT (5.1) and MySQL 8.0. Update the driver:

[root@db01~]# cp mysql-connector-java-8.0.21/mysql-connector-java-8.0.21.jar /usr/local/mycat/lib/
[root@db01~]# mycat restart

MyCAT Read-Write Separation Configuration

Directory Structure Overview

DirectoryPurposeKey FilesDescription
binExecutable scripts
confConfiguration filesschema.xmlMain configuration (nodes, read-write separation, HA, sharding)
rule.xmlSharding rule definitions
server.xmlService configuration (users, network, permissions)
log4j2.xmlLogging configuration
libDriver libraries
logsLog fileswrapper.logService startup log
mycat.logService runtime log

schema.xml Configuration

[root@db01 ~]# cd /usr/local/mycat/conf/
[root@db01 conf]# vim schema.xml

<?xml version="1.0"?>
<mycat:schema xmlns:mycat="http://io.mycat/">

   <schema name="app_db" checkSQLschema="false" sqlMaxLimit="100" dataNode="node_app"></schema>
   <schema name="log_db" checkSQLschema="false" sqlMaxLimit="100" dataNode="node_log"></schema>
   
   <dataNode name="node_app" dataHost="app_cluster" database="production" />
   <dataNode name="node_log" dataHost="app_cluster" database="logs" />
   
   <dataHost name="app_cluster" maxCon="4000" minCon="10" balance="3" 
             writeType="0" dbType="mysql" dbDriver="native" 
             switchType="1" slaveThreshold="100">
      <heartbeat>select user()</heartbeat>
      <writeHost host="primary_db" url="192.168.1.100:3306" user="myuser" password="securepass">
         <readHost host="replica1" url="192.168.1.101:3306" user="myuser" password="securepass"/>
         <readHost host="replica2" url="192.168.1.102:3306" user="myuser" password="securepass"/>
         <readHost host="replica3" url="192.168.1.103:3306" user="myuser" password="securepass"/>
      </writeHost>
   </dataHost> 
</mycat:schema>

Schema Configuration Parameters

ParameterSettingDescription
checkSQLschematrue/falseWhether to check database.table syntax
sqlMaxLimitnumericDefault row limit for queries
dataNodestringBackend node reference

DataNode Configuration Parameters

ParameterDescription
nameCustom node identifier
dataHostHost group reference
databasePhysical database name

DataHost Configuration Parameters

ParameterDescription
maxConMaximum concurrent connections
minConMinimum pooled connections
balanceRead load balancing (0-3)
writeTypeWrite balancing strategy (0-1)
switchTypeFailover mechanism (-1 to 2)

server.xml Configuration

<?xml version="1.0" encoding="UTF-8"?>
<mycat:server xmlns:mycat="http://io.mycat/">
   <system>
      <property name="useHandshakeV10">1</property>
   </system>
   
   <user name="myuser" defaultAccount="true">
      <property name="password">securepass</property>
      <property name="schemas">app_db,log_db</property>
      <property name="defaultSchema">app_db</property>
   </user>
</mycat:server>

Verifying Read-Write Separation

1. Applying Configuration Changes

[root@db01 conf]# vim schema.xml
# Edit configuration as shown above

2. Reloading Configuration

# On master database instance
[root@db01 ~]# mysql -S /data/3307/mysql.sock -e "source /root/world.sql"

# Reload MyCAT configuration
[root@master conf]# mysql -uroot -p123456 -h 10.0.0.51 -P9066
mysql> reload @@config_all;
# or restart
[root@db01 ~]# mycat restart

3. Testing Read-Write Distribution

[root@db01~]# mysql -uroot -p123456 -h 10.0.0.51 -P8066

# Test read operation
mysql> select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 101 |
+-------------+

# Test write operation
mysql> begin; select @@server_id; commit;
+-------------+
| @@server_id |
+-------------+
| 100 |
+-------------+

MyCAT Operations and Maintenance

[root@db01 ~]# mysql -uroot -p123456 -h 10.0.0.51 -P9066

mysql> show @@help;              -- Display available commands
mysql> show @@server;            -- Check server status
mysql> show @@datanode;          -- View data nodes
mysql> show @@datasource;        -- Display data sources
mysql> reload @@config;          -- Reload schema.xml
mysql> reload @@config_all;      -- Reload all configurations

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.