MYSQL+MYCAT Read and Write Separation Actual Combat

****1. Actual combat MYSQL+MYCAT read and write separation actual combat, realize the MYSQL database 1 master 2 slave architecture. ** 2. Write out the MYSQL 1 master 2 slave architecture deployment process and the entire MYCAT actual combat process, write out all the deployment processes and draw the architecture diagram.

insert image description here

1. Configure one master and two slaves of MySQL

1.1 Install MySQL

| wget https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
#Install MYSQL8 YUM source;
rpm -ivh mysql80-community-release-el7-1.noarch.rpm
#Install MYSQL8.0 software service;
sed -i 's#gpgcheck=1#gpgcheck=0#g' /etc/yum.repos.d/mysql-community.repo
yum install mysql-community-server -y
#start MYSQL service &Start MYSQL service;
systemctl enable mysqld.service
systemctl start mysqld.service
#View MYSQL password;
grep 'temporary password' /var/log/mysqld.log
login password needs to have ''
[root@itzfl ~]# mysql -uroot -p'rpsqzLq8*/wC'
#Modify password rules and length restrictions;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'aaaAAA111.';
set global validate_password.policy=0;
set global validate_password.length=1;
#Change the password to 123456, the command is as follows;
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
#The default root user cannot log in remotely, and the password information needs to be updated;
use mysql;
update mysql.user set host='%' where user="root";
use mysql;
grant system_user on . to 'root';
flush privileges;
#If you want to change the root password, the command is as follows;
update user set authentication_string=password("root") where user='root' and host='localhost';
#In versions before mysql8.0, the encryption rule is mysql_native_password, but after mysql8, the encryption rule is
caching_sha2_password
When using navicat to log in to mysql, a pop-up window will report an error
grant system_user on . to 'root';
ALTER USER 'root'@'%' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER;
ALTER USER ‘root’@‘%’ IDENTIFIED WITH mysql_native_password BY ‘123456’;
FLUSH PRIVILEGES;|

1.2 Configure master-slave

Modify the configuration file of mysql
and set the server-id differently,
set the bin-log
configuration on the master as before
insert image description here
insert image description here
insert image description here

The database created in the master will be synchronized from the library

2. MYCAT installation

2.1 MYCAT installation environment

192.168.140.148 MYCAT
192.168.140.144 MYSQL-MASTER
192.168.140.145 MYSQL-SLAVE1
192.168.140.150 MYSQL-SLAVE2

2.2JDK

2.2.1 Install jdk

[root@itzfl ~]# rz

[root@itzfl ~]# ls
anaconda-ks.cfg       jdk1.8.0_131.tar.gz  模板  图片  下载  桌面
initial-setup-ks.cfg  公共                 视频  文档  音乐
[root@itzfl ~]# tar -xzf jdk1.8.0_131.tar.gz 
[root@itzfl ~]# ls
anaconda-ks.cfg       jdk1.8.0_131         公共  视频  文档  音乐
initial-setup-ks.cfg  jdk1.8.0_131.tar.gz  模板  图片  下载  桌面
[root@itzfl ~]# mkdir -p /usr/java
[root@itzfl ~]# mv jdk1.8.0_131 /usr/java/
[root@itzfl ~]# cd !$
cd /usr/java/
[root@itzfl java]# cd jdk1.8.0_131/
[root@itzfl jdk1.8.0_131]# ls
bin             jre          release
COPYRIGHT       lib          src.zip
db              LICENSE      THIRDPARTYLICENSEREADME-JAVAFX.txt
include         man          THIRDPARTYLICENSEREADME.txt
javafx-src.zip  README.html
[root@itzfl jdk1.8.0_131]# 

2.2.2 Configure the JAVA environment variable, add the following code at the end of the /etc/profile configuration file

[root@itzfl jdk1.8.0_131]# clear
[root@itzfl jdk1.8.0_131]# vim /etc/profile

export JAVA_HOME=/usr/java/jdk1.8.0_131/


[root@itzfl jdk1.8.0_131]# source !$
source /etc/profile


2.2.3 View environment variables

[root@itzfl jdk1.8.0_131]# $JAVA_HOME/bin/java -version
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
[root@itzfl jdk1.8.0_131]# 
[root@itzfl jdk1.8.0_131]# /usr/java/jdk1.8.0_131/bin/java -version 
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
[root@itzfl jdk1.8.0_131]# 

Two ways to view
, but you can’t use Java -version to view because it is not loaded into the path,
so add a path to start java -version

[root@itzfl jdk1.8.0_131]# export PATH=$PATH:/usr/java/jdk1.8.0_131/bin/
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/java/jdk1.8.0_131/bin/

在etc/profile
 也加入这个路径

[root@itzfl jdk1.8.0_131]# java -version
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
[root@itzfl jdk1.8.0_131]#

2.3 Install MYCAT

下载Mycat安装包
 wget http://dl.mycat.org.cn/2.0/install-template/mycat2-install-template-1.21.zip
下载Mycat 2所需依赖 jar
[root@itzfl ~]# wget http://dl.mycat.org.cn/2.0/1.21-release/mycat2-1.21-release-jar-with-dependencies.jar

[root@itzfl ~]# unzip mycat2-install-template-1.21.zip 

[root@itzfl ~]# \mv mycat /usr/local/
#Cp Mycat 2 所需依赖JAR包至Mycat的LIB目录;
[root@itzfl ~]# cp mycat2-1.21-release-jar-with-dependencies.jar /usr/local/mycat/lib/
[root@itzfl ~]# 
#授予Mycat bin目录下文件执行权限;
chmod +x /usr/local/mycat/bin/*
#查看Mycat程序是否部署成功;
ls -l /usr/local/mycat/

2.4 MYCAT configuration file

2.4.1 Create and authorize users for the Mycat program on both the master and slave nodes

Create mycat users at 144 145 150 respectively

#创建用户&授权 mycat 用户访问;
create user mycat@'%' identified by '123456';
GRANT XA_RECOVER_ADMIN ON *.* to 'mycat'@'%';
GRANT ALL PRIVILEGES ON *.* to 'mycat'@'%';
FLUSH PRIVILEGES;

2.4.2 Create the database mycat used by Mycat on the Master master node

create database mycat;
#这个库称为Mycat 的原型库(prototype),
Mycat 在启动时会自动在原型库下创建其运行时所需的数据表。

mycat自己的数据会写入到这个数据库中
就是通过mysql 

2.4.3 Configure the data source (datasource) information of the Mycat prototype library

This library must be configured, otherwise an error will be reported when starting Mycat. The operation instructions are as follows:
After configuration, the mycat machine can connect to the mycat database under mysql

cd /usr/local/mycat/conf/datasources/
#修改prototypeDs.datasource.json代码;


[root@itzfl datasources]# vim prototypeDs.datasource.json 

{
        "dbType":"mysql",
        "idleTimeout":60000,
        "initSqls":[],
        "initSqlsGetConnection":true,
        "instanceType":"READ_WRITE",
        "maxCon":1000,
        "maxConnectTimeout":3000,
        "maxRetryCount":5,
        "minCon":1,
        "name":"prototypeDs",
        "password":"123456",
        "type":"JDBC",
        "url":"jdbc:mysql://192.168.140.144:3306/mycat?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8",
        "user":"mycat",
        "weight":0
}
~           

2.4.4 Add the data source information of the physical database discuz1 database

Add the master library and slave library of data storage, configure the ip and password to connect the master library and slave library

#进入DataSource目录;
cd /usr/local/mycat/conf/datasources/
#拷贝模板文件,生成master和slave配置文件;


[root@itzfl datasources]#  \cp prototypeDs.datasource.json  master.datasource.json
[root@itzfl datasources]# \cp prototypeDs.datasource.json  slave-01.datasource.json
[root@itzfl datasources]# \cp prototypeDs.datasource.json  slave-02.datasource.json
[root@itzfl datasources]# ls
master.datasource.json       slave-01.datasource.json
prototypeDs.datasource.json  slave-02.datasource.json
[root@itzfl datasources]# 




修改master.datasource.json代码;
[root@itzfl datasources]# vim master.datasource.json 

{
        "dbType":"mysql",
        "idleTimeout":60000,
        "initSqls":[],
        "initSqlsGetConnection":true,
        "instanceType":"READ_WRITE",
        "maxCon":1000,
        "maxConnectTimeout":3000,
        "maxRetryCount":5,
        "minCon":1,
        "name":"master",
        "password":"123456",
        "type":"JDBC",
        "url":"jdbc:mysql://192.168.140.144:3306/discuz1?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8",
        "user":"mycat",
        "weight":0
}

修改slave-01.datasource.json代码;
[root@itzfl datasources]# vim slave-01.datasource.json 

{
        "dbType":"mysql",
        "idleTimeout":60000,
        "initSqls":[],
        "initSqlsGetConnection":true,
        "instanceType":"READ",
        "maxCon":1000,
        "maxConnectTimeout":3000,
        "maxRetryCount":5,
        "minCon":1,
        "name":"slave-01",
        "password":"123456",
        "type":"JDBC",
        "url":"jdbc:mysql://192.168.140.145:3306/discuz1?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8",
        "user":"mycat",
        "weight":0
}
~                                                                 
~                                                                 
修改slave-02.datasource.json代码;~       
[root@itzfl datasources]# vim slave-02.datasource.json 

{
        "dbType":"mysql",
        "idleTimeout":60000,
        "initSqls":[],
        "initSqlsGetConnection":true,
        "instanceType":"READ",
        "maxCon":1000,
        "maxConnectTimeout":3000,
        "maxRetryCount":5,
        "minCon":1,
        "name":"slave-02",
        "password":"123456",
        "type":"JDBC",
        "url":"jdbc:mysql://192.168.140.150:3306/discuz1?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8",
        "user":"mycat",
        "weight":0
}
~                                                                 
~       

2.4.5 Configure the cluster (cluster) information mycat of the Discuz data source to associate one master and two slaves

进入Clustesr目录;
cd /usr/local/mycat/conf/clusters/
#不能删除 prototype.cluster.json,否则启动 Mycat 时会报错;

[root@itzfl datasources]# cd /usr/local/mycat/conf/clusters/
[root@itzfl clusters]# ls
prototype.cluster.json
[root@itzfl clusters]# \cp prototype.cluster.json master-slave.cluster.json
[root@itzfl clusters]# ls
master-slave.cluster.json  prototype.cluster.json



[root@itzfl clusters]# vim master-slave.cluster.json 

{
        "clusterType":"MASTER_SLAVE",
        "heartbeat":{
                "heartbeatTimeout":1000,
                "maxRetry":3,
                "minSwitchTimeInterval":300,
                "slaveThreshold":0
        },
        "masters":[
                "master"
        ],

        "replicas":[
                "slave-01"
                "slave-02"

        ],

        "maxCon":5000,
        "name":"master-slave",
        "readBalanceType":"BALANCE_ALL",
        "switchType":"NOT_SWITCH"
}

2.4.6 Configure the relationship between the physical library (schema) and the data source/data source cluster in Mycat

进入Schemas目录;
cd /usr/local/mycat/conf/schemas/
#创建discuz.schema.json文件,代码;


[root@itzfl schemas]# ls
discuz1.schema.json             mysql.schema.json
information_schema.schema.json
[root@itzfl schemas]# vim discuz1.schema.json 
[root@itzfl schemas]# vim discuz1.schema.json 

{
    "schemaName": "discuz1",
    "targetName": "master-slave",
    "normalTables": {}
}


2.4.7 Modify Mycat login user information

#进入Users目录;
cd /usr/local/mycat/conf/users/
新建mycat.user.json

[root@itzfl users]# ls
mycat.user.json  root.user.json
[root@itzfl users]# vim mycat.user.json 

{
        "dialect":"mysql",
        "ip":null,
        "password":"123456",
        "transactionType":"xa",
        "username":"mycat"
}


2.4.8 Start the Mycat service and check its running status

#启动服务;
/usr/local/mycat/bin/mycat start
#查看服务状态;
/usr/local/mycat/bin/mycat status
#查看Mycat帮助信息;
/usr/local/mycat/bin/mycat --help


[root@itzfl ~]# /usr/local/mycat/bin/mycat start
Starting mycat2...
[root@itzfl ~]# 


[root@itzfl ~]# /usr/local/mycat/bin/mycat start
Starting mycat2...
[root@itzfl ~]# ps -ef |grep java
root      18002  18000 66 17:58 ?        00:00:02 java -DMYCAT_HOME=./conf -Dfile.encoding=UTF-8 -server -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1984 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Xmx32G -Xms1G -Xms256m -Xmx4096m -Djava.library.path=lib -classpath lib/wrapper.jar:lib/libwrapper-aix-ppc-32.a:lib/libwrapper-aix-ppc-64.a:lib/libwrapper-hpux-parisc-64.sl:lib/libwrapper-linux-ppc-64.so:lib/libwrapper-linux-x86-32.so:lib/libwrapper-linux-x86-64.so:lib/libwrapper-macosx-ppc-32.jnilib:lib/libwrapper-macosx-universal-32.jnilib:lib/libwrapper-macosx-universal-64.jnilib:lib/libwrapper-solaris-sparc-32.so:lib/libwrapper-solaris-sparc-64.so:lib/libwrapper-solaris-x86-32.so:lib/wrapper-windows-x86-32.dll:lib/wrapper-windows-x86-64.dll:lib/wrapper.jar:lib/mycat2-1.21-release-jar-with-dependencies.jar -Dwrapper.key=6YGIjt4ARzihLUD1 -Dwrapper.port=32000 -Dwrapper.jvm.port.min=31000 -Dwrapper.jvm.port.max=31999 -Dwrapper.pid=18000 -Dwrapper.version=3.2.3 -Dwrapper.native_library=wrapper -Dwrapper.service=TRUE -Dwrapper.cpu.timeout=10 -Dwrapper.jvmid=1 org.tanukisoftware.wrapper.WrapperSimpleApp io.mycat.MycatCore start
root      18034  12390  0 17:58 pts/1    00:00:00 grep --color=auto java



查看日志


[root@itzfl datasources]# !ta
tail -fn 10  /usr/local/mycat/logs/wrapper.log 
INFO   | jvm 1    | 2022/08/27 19:13:06 | 2022-08-27 19:13:06,864[INFO]com.alibaba.druid.pool.DruidDataSource.init:990{dataSource-2} inited
INFO   | jvm 1    | 2022/08/27 19:13:07 | 2022-08-27 19:13:07,832[INFO]io.mycat.config.MycatRouterConfigOps.recoveryXA:735readXARecoveryLog start
INFO   | jvm 1    | 2022/08/27 19:13:08 | 2022-08-27 19:13:07,985[INFO]io.mycat.vertx.VertxMycatServer.lambda$start$1:166Mycat Vertx server a2dece8a-e4ef-4c29-b2b3-a863e0493194 started up.
INFO   | jvm 1    | 2022/08/27 19:13:08 | 2022-08-27 19:13:07,986[INFO]io.mycat.vertx.VertxMycatServer.lambda$start$1:166Mycat Vertx server 30030d69-cd92-44b0-b5d7-6d9186a2466c started up.
INFO   | jvm 1    | 2022/08/27 19:13:08 | 2022-08-27 19:13:07,987[INFO]io.mycat.vertx.VertxMycatServer.lambda$start$1:166Mycat Vertx server 3152ed0c-deb8-4809-aefc-3cd9e3ef7c9c started up.
INFO   | jvm 1    | 2022/08/27 19:13:08 | 2022-08-27 19:13:07,987[INFO]io.mycat.vertx.VertxMycatServer.lambda$start$1:166Mycat Vertx server 2302a753-040d-491a-b30a-d049804740cc started up.
INFO   | jvm 1    | 2022/08/27 19:13:08 | 2022-08-27 19:13:07,989[INFO]io.mycat.vertx.VertxMycatServer.lambda$start$1:166Mycat Vertx server 88ed2bc1-2e82-4858-b1b5-74c1650240a1 started up.
INFO   | jvm 1    | 2022/08/27 19:13:08 | 2022-08-27 19:13:07,990[INFO]io.mycat.vertx.VertxMycatServer.lambda$start$1:166Mycat Vertx server a381084d-fc95-4c1b-b411-0ef32bd53f74 started up.
INFO   | jvm 1    | 2022/08/27 19:13:08 | 2022-08-27 19:13:07,992[INFO]io.mycat.vertx.VertxMycatServer.lambda$start$1:166Mycat Vertx server ca55bc12-c587-4b01-a973-f1c0457cd75f started up.
INFO   | jvm 1    | 2022/08/27 19:13:08 | 2022-08-27 19:13:07,992[INFO]io.mycat.vertx.VertxMycatServer.lambda$start$1:166Mycat Vertx server 2659af49-b635-430d-ba2c-e9448fe090b1 started up.


netstat -ntl|grep -E --color "8066|9066"
tcp6       0      0 127.0.0.1:9066          :::*                    LISTEN     
tcp6       0      0 :::8066                 :::*                    LISTEN     
[root@itzfl datasources]# 

3. MYCAT read and write separation test

3.1 Check whether ports 8066 and 9066 are enabled

Among them, 8066 is used for WEB connection to Mycat, and 9066 is used for SA|DBA management port;

netstat -ntl|grep -E --color "8066|9066"
tcp6       0      0 127.0.0.1:9066          :::*                    LISTEN     
tcp6       0      0 :::8066                 :::*                    LISTEN     
[root@itzfl datasources]# 

3.2 Enter the MyCAT command line interface

[root@master ~]# mysql -h192.168.140.148 -uroot -p123456 -P8066
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 0
Server version: 5.7.33-mycat-2.0 MySQL Community Server - GPL

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> 

3.2.1 The client creates a database and inserts data into the main database to check whether it is synchronized

Client inserts data

insert image description here

View in the main library

in slave-01
insert image description here

slave-02
insert image description here

write success

3.2.2 Insert data in the slave database and query whether there is data on the client side to achieve master-write-slave-read

in slave-01
insert image description here
insert image description here

read data on client side

3.2.3 Check whether the data can be found through port 8066. If it can be found, it proves that the read-write separation verification is successful. mysql -h192.168.140.148 -uroot -p123456 -P8066 -e "select * from discuz.t1;"

insert image description here

insert image description here

4. Summary,

1. There are a lot of problems when doing read-write separation. For example, the created mycat user is not authorized, so the mycat server cannot connect to the database through the mycat user. 2. If there is an
error, find the error in the log file and correct it.
3. Be careful in configuration carefulness

Guess you like

Origin blog.csdn.net/xiaolong1155/article/details/126562633