[MySQL Cluster 2] Use MyCat and ProxySql to proxy MySQL cluster

MyCat

Insert image description here

Install MyCat

introduce:

Mycat is an open source database middleware used to provide high availability, load balancing and sharding capabilities.

Step 1: Install Java environment

First, you need to install the Java environment on the server. Open a terminal and run the following command:

sudo yum install java-1.8.0-openjdk-devel

Step 2: Download and unzip Mycat

  1. Download the latest version of the Mycat compressed package on the Mycat official website, URL: Mycat official website or directly use the command to download on the server wget:
# 下载Mycat压缩包
wget http://dl.mycat.org.cn/2.0/install-template/mycat2-install-template-1.21.zip
  1. Enter the directory you downloaded in the terminal and run the following command to decompress the file:
tar -zxvf mycat2-install-template-1.21.zip

The following error may occur when using tardecompression because the file contains multiple compressed packages:
Insert image description here
Solution: Use unzipor p7zipto decompress.

unzip mycat2-install-template-1.21.zip

Step 3: Configure Mycat

  1. Enter the decompressed Mycat directory:
cd mycat/lib/
  1. Download the dependency package or copy the downloaded dependency package to the directory:
# 直接下载依赖包
wget http://dl.mycat.org.cn/2.0/1.21-release/mycat2-1.21-release-jar-with-dependencies.jar
# 复制
cp mycat2-1.21-release-jar-with-dependencies.jar mycat/lib/
  1. Edit the configuration file confin the directory server.xml:
vim mycat/conf/datasources/prototypeDs.datasource.json
  1. Configure the mysql connection as the master database. The only things that need to be modified are url: , userand password:
{
    
    
        "dbType":"mysql",
        "idleTimeout":60000,
        "initSqls":[],
        "initSqlsGetConnection":true,
        "instanceType":"READ_WRITE",
        "maxCon":1000,
        "maxConnectTimeout":3000,
        "maxRetryCount":5,
        "minCon":1,
        "name":"prototypeDs",
        "password":"Ddz@5201413",
        "type":"JDBC",
        "url":"jdbc:mysql://114.132.156.12:3306/ddz_test?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8",
        "user":"root",
        "weight":0
}

Enter :wq!to save and exit.

Step 4: Start Mycat

  1. mycat/bin/Start the Mycat server using the following command in the directory :
sh mycat start

There may be a startup failure:
Insert image description here
the reason is that the permissions of the two files wrapper-linux-x86-64 and wrapper-linux-x86-32 are incorrectly set.

# 查看文件权限
ls -l /home/mycat/bin/./wrapper-linux-x86-64  
ls -l /home/mycat/bin/./wrapper-linux-x86-32
# 设置文件权限
chmod +x /home/mycat/bin/./wrapper-linux-x86-64  
chmod +x /home/mycat/bin/./wrapper-linux-x86-32
或给/bin文件赋权限
chmod -R 777 bin/

After setting up, enter mycat/bin/the directory and execute the startup command:

cd mycat/bin/
./mycat start
  1. After confirming that Mycat starts successfully, use the following command to check whether the Myat process is running:
ps -ef | grep mycat

Insert image description here

Step 5: Connect and test Mycat

  1. Use the MySQL client to connect to the Mycat server, example:
mysql -h114.132.156.12 -P8066 -uroot -p
  1. After entering the password and successfully connecting to the Myat server, you can execute SQL statements for testing.

Conclusion:
By following the above steps, Mycat was successfully installed and configured on CentOS 7 and able to connect and test the Mycat server. This will provide high availability, load balancing and sharding capabilities for your database management, improving database efficiency and performance.

ProxySql

Install ProxySql

introduce:

ProxySQL is a high-performance MySQL proxy server used for load balancing, failover and query filtering. In this blog, we will explain how to install and configure ProxySQL on CentOS 7.9.

Step 1: Update your system

First, update all packages on your system using the following command:

sudo yum update

Step 2: Install ProxySQL

Add the ProxySQL software repository:

sudo yum install -y https://github.com/sysown/proxysql/releases/download/v2.5.0/proxysql-2.5.0-1-centos7.x86_64.rpm

Install proxysql package:

sudo yum install proxysql

Step 3: Configure ProxySQL

Edit the ProxySQL configuration file:

sudo vi /etc/proxysql/proxysql.cnf

mysql_serversAdd the details of the MySQL backend in the section :

mysql_servers =
(
    {
    
     
        address = '127.0.0.1', 
        port = 3306, 
        hostgroup = 10, 
        max_connections = 100, 
        max_replication_lag = 5, 
        use_ssl = 0 
    }
)

Step 4: Start ProxySQL

Start the ProxySQL service:

sudo systemctl start proxysql
sudo systemctl enable proxysql

Step 5: Use ProxySQL

Connect to the ProxySQL administrator command line interface:

mysql -u admin -p -h 127.0.0.1 -P 6032 --prompt='ProxySQLAdmin>'

Configure user, host, and port mapping on the ProxySQL administrator command line interface:

INSERT INTO mysql_users (username, password, default_hostgroup) VALUES ('user', 'password', 10);
LOAD MYSQL USERS TO RUNTIME;
SAVE MYSQL USERS TO DISK;
INSERT INTO mysql_servers (hostgroup_id, hostname, port) VALUES (10, '127.0.0.1', 3306);
LOAD MYSQL SERVERS TO RUNTIME;
SAVE MYSQL SERVERS TO DISK;
INSERT INTO mysql_query_rules (active, match_pattern, destination_hostgroup) VALUES (1, '^SELECT.*', 10);
LOAD MYSQL QUERY RULES TO RUNTIME;
SAVE MYSQL QUERY RULES TO DISK;

Now you can use ProxySQL to proxy MySQL queries.

Conclusion:
Installing ProxySQL proxy MySQL on CentOS 7.9 is a simple process, just follow the steps in this article. Using ProxySQL can improve the load balancing and failover capabilities of the MySQL server, while also filtering and redirecting queries.

Guess you like

Origin blog.csdn.net/weixin_45626288/article/details/133002454