Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Deploy MyCat with Docker and Configure MySQL Master-Slave

Tech Oct 20 1

Refer to "Dockerr Deployment of Master-Slave" for deploying MySQL in Docker.

The MyCat Dockerfile is referenced for starting the Docker container.

FROM docker.io/adoptopenjdk/openjdk8:latest

ENV AUTO_RUN_DIR ./mycat2
ENV DEPENDENCE_FILE mycat2-1.21-release-jar-with-dependencies.jar
ENV TEMPLATE_FILE mycat2-install-template-1.21.zip

RUN sed -i "s@http://.*archive.ubuntu.com@http://mirrors.aliyun.com@g" /etc/apt/sources.list
RUN sed -i "s@http://.*security.ubuntu.com@http://mirrors.aliyun.com@g" /etc/apt/sources.list

RUN buildDeps='procps wget unzip' \
    && apt-get update \
    && apt-get install -y $buildDeps

RUN wget -P  $AUTO_RUN_DIR/ http://dl.mycat.org.cn/2.0/1.21-release/$DEPENDENCE_FILE \
   &&  wget -P  $AUTO_RUN_DIR/ http://dl.mycat.org.cn/2.0/install-template/$TEMPLATE_FILE 

RUN cd $AUTO_RUN_DIR/ \
    && unzip $TEMPLATE_FILE \
    && ls -al . \
    && mv  $DEPENDENCE_FILE mycat/lib/ \
    && chmod +x  mycat/bin/* \
    && chmod 755  mycat/lib/* \
    && mv mycat /usr/local
#copy mycat /usr/local/mycat/
VOLUME /usr/local/mycat/conf
VOLUME /usr/local/mycat/logs

EXPOSE 8066 1984
CMD ["/usr/local/mycat/bin/mycat", "console"]

Start the container:

docker run  -d   -it   -p 8066:8066   -p 1984:1984  \
--name mycat  \
-v /data/mycat2/conf:/usr/local/mycat/conf   \
-v /data/mycat2/logs:/usr/local/mycat/logs  \
mycat.org.cn/mycat2:20220706

Create a user named 'mycat' on the master database for connecting to the data source.

CREATE USER 'mycat'@'%' IDENTIFIED BY '123456';
GRANT ALL PRIVILEGES ON *.* to 'mycat'@'%';
FLUSH PRIVILEGES;

If the conf directory in the MyCat configuration is empty, download and extract it to this directory, refer to "Installing MyCat".

Since using the comment-based configuration method did not generate files on the physical machine, this document provides the following configuration.

Port Service
8066 MyCat
3307 MySQL Master
3308 MySQL Slave

MyCat Master-Slave Configuration:

# m and s are custom names, m-master, s-slave
cd /data/mycat2/conf/datasources

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

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

cd /data/mycat2/conf/clusters
cp prototype.cluster.json master-slave.cluster.json
vim master-slave.cluster.json
{
	"clusterType":"MASTER_SLAVE",
	"heartbeat":{
		"heartbeatTimeout":1000,
		"maxRetry":3,
		"minSwitchTimeInterval":300,
		"slaveThreshold":0
	},
	"masters":[
		"m"       # Master node
	],
	"maxCon":200,
	"name":"master-slave",  # Cluster name
	"readBalanceType":"BALANCE_ALL",
        "replicas":[
                "s" // Slave node
        ],
	"switchType":"SWITCH"
}

cd /data/mycat2/conf/schemas
cp mysql.schema.json mydb1.schema.json
vim mydb1.schema.json
{
     "customTables":{},
     "globalTables":{},
     "normalProcedures":{},
     "normalTables":{},
     "schemaName":"mydb1",         # Physical database
     "targetName":"master-slave",  # Cluster configuration /data/mycat2/conf/clusters/master-slave.cluster.json
     "shardingTables":{},
     "views":{}
}

# Restart the container
docker restart mycat

Testing:

Note: The current environment uses statement-based binlog synchronization, which can recognize the @@hostname variable. Connect to MyCat and insert into user(name, hostname) values ('Zhang San', @@hostname);

Master database:

Slave database data:

Connecting to MyCat and querying will show data from the slave if it is retrieved from the slave, otherwise from the master.

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...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

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