This is the 31st day of my participation in the August More Text Challenge

The last article wrote how to use Docker to build the master/slave replication of Mysql, this article is based on the master/slave replication of Mysql has been built to achieve read/write separation.

Direct CV can also be built, don’t panic.

Let's come on!!

Create Mycat folder

mkdir /usr/local/mycat/conf -p
Copy the code

Create Docker folder

mkdir /usr/local/docker/mycat/ -p
Copy the code

Then CD to /usr/local/docker-mycat

cd /usr/local/docker/mycat
Copy the code

Download Mycat and unzip it

Download mycat from /usr/local/docker-mycat

Wget HTTP: / / http://dl.mycat.org.cn/1.6.7.1/Mycat-server-1.6.7.1-release-20190627191042-linux.tar.gzCopy the code

Rename mycat-server-1.6.7.1-release-20190627191042-Linux to Mycat

Mv Mycat - server - 1.6.7.1 - release - 20190627191042 - Linux. Tar. Gz Mycat. Tar. GzCopy the code

Unzip mycat.tar.gz

tar -zxvf mycat.tar.gz
Copy the code

Directory structure:

In order not to destroy the original file, we copy the mycat configuration file to /usr/local/mycat.

cp -r mycat/conf/ /usr/local/mycat Copy mycat/conf/ to /usr/local/mycat
Copy the code

You can see that it has been copied successfully.

4, write Dockerfile file

/usr/local/docker-mycat = /usr/local/ docker-mycat = /usr/local/ docker-mycat

vim dockerfile
Copy the code

Dockerfile file contents:

# create an image based on openJDK :8
If centos is used, you must ensure that the JDK is installed, otherwise you need to ADD it to the Dockerfile file
FROM openjdk:8

Copy the files from the host directory into the image and the ADD command will automatically process the URL and unzip the tar
/usr/local/mycat = /usr/local/mycat = /usr/local/mycat
ADD mycat.tar.gz /usr/local

Container data volumes for data preservation and persistence
Mycat config file address exposed mapping address, startup directly mapping host folder
VOLUME /usr/local/mycat
WORKDIR /usr/local/mycat

Used to set environment variables during the image build process
ENV MYCAT_HOME=/usr/local/mycat

Expose the required port for MyCat
EXPOSE 8066 9066

# Start MyCat as a desktop process
CMD ["/usr/local/mycat/bin/mycat"."console"."&"]   
Copy the code

Five, pack the image

Docker build-t mycat:1.6.The last decimal point. Dot represents the dockerfile file in the directory where the package command was executed
Copy the code

Write the Mycat configuration file

Jump to /usr/local/mycat/conf/ (the same directory we copied the configuration files to)

cd /usr/local/mycat/conf/ ls # display file
Copy the code

The key files are the three highlighted in red

  • Schema. XML, server. XML: Used for read/write separation. In our little Demo, only schema. XML was edited.
  • Rule-xml: Used to divide tables and libraries into configuration files.
  • For details, please refer to the relevant official documentation.

Edit the schema.xml file:

vim schema.xml
Copy the code

Default file contents:

Delete unnecessary, change to the following can be.


      <! DOCTYPEmycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">        
    <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">        
    </schema>
    <dataNode name="dn1" dataHost="localhost1" database="testdb" />        
    <dataHost name="localhost1" maxCon="1000" minCon="10" balance="3"                          writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">                
        <heartbeat>select user()</heartbeat>                
        <writeHost host="hostM1" url="47.113.227.254:3310" user="root"                                   password="123456">                        
            <readHost host="hostS2" url="47.113.227.254:3311" user="root" password="123456" />                </writeHost>        
    </dataHost>
</mycat:schema>
Copy the code

A few quick notes about schema.xml:

  1. The balance property under dataHost, which is also used to set the type of read/write separation:
    • Balance =”0″, the read/write separation mechanism is disabled and all read operations are sent to the currently available writeHost.
    • Balance =”1″, all readHost and stand by writeHost participate in load balancing of SELECT statement,
    • Balance =”2″, all read operations are distributed randomly on writeHost and readhost.
    • Balance =”3″, all read requests are randomly distributed to readhost for execution, writerHost does not bear the read pressure
  2. WriteType =”0″: All write operations are sent to the first configured writeHost. The first one fails and the second one survives
  3. switchType=”1″:
    • 1 Default value: automatic switchover.
    • -1 Indicates that the switchover is not automatic
    • The switchover is determined based on the primary/secondary synchronization status of MySQL.

Supplementary – Quick delete in command mode:

Press insert and then Esc to enter command mode.

  • According to theddDelete the line where the cursor is currently located.
  • According to thenddDelete n lines including the beginning of the line where the cursor is.

7. Start the mirror

docker run --name mycat -p 8066:8066 -p 9066:9066 -v /usr/local/mycat/conf/:/usr/local/mycat/conf/ -v /usr/local/mycat/logs/:/usr/local/ mycat/logs / - d mycat: 1.6Copy the code
docker ps -a Docker logs mycat
Copy the code

8. Connection test

8.1 Navicat connection

You can useNavicatorCMDThe command line.

About the account and password here:

This is in the server.xml configuration file mentioned earlier.

My connection looks like this, because I’ve set up a master-slave copy, which also has tables in it, so it looks like this.

8.2 CMD Connection

Mysql -uroot -p123456 -h IP address -p 8066Copy the code

8.3 read/write separation test

Insert into mytable values(99,@@hostname);

When the slave machine copies this statement to execute, and appears and the host is not the same as the data (mixed configuration can be processed, I did not process here, mainly generated by the function), so we use mycat to read the data, we can see whether to achieve read and write separation.

Host:

From the machine:

The data from the machine is different.

Read mycat:

You can see that we are reading data from the machine, which means that we have indeed achieved read/write separation.

Talk to yourself

I’m actually really curious about how many servers a truly high availability system would use 😂.

Reference:

Docker build Mycat (single node)