[Linux] MySQL High Availability Mysql Read and Write Separation Practice

1. The principle of MySQL read and write separation

Separation of reading and writing is to modify on the master server, and the data will be synchronized to the slave server. The slave server can only provide read data, but not write it. While realizing backup, it also realizes the optimization of database performance and improves server security.

dc9b57ef0dc84c8b8c41aa937adec381.png

 2. Practice of reading and writing separation

The vast majority of enterprise application scenarios are more read-only than write-less for databases, such as Weibo, where a celebrity posts a Weibo, and tens of millions of people read it. Therefore, in order to share the pressure on the database and do load balancing, the first thing to consider is the separation of reading and writing. The separation of reading and writing is based on the master-slave replication implemented above. Using the master library as the writing library and the slave library as the reading library can improve database performance and IO performance. .

3. Implementation of read-write separation

In order to achieve the separation of reading and writing, many solutions have emerged. Among them, middleware is used as a proxy to keep the application layer code from changing with changes in the database. This includes Amoeba, Atlas, Cobar, Mycat, MySQL Proxy, etc. Mycat is a relatively mature solution in the current open source database middleware.

4. Install JDK

① Unzip the package

tar -xzvf jdk-8u161-linux-x64.tar.gz -C /usr/local/java

b9d2a9ecb67648b9bbf6a4b107be3be5.png

②Edit/etc/profile

vim /etc/profile

2da5cbd210f04910bfe1e16c5941b15e.png

 Use the source command to make the modification take effect immediately without restarting the server:

source /etc/profile

 ③ Check whether the installation is successful, if there is a version number, the installation is successful;

java -version

d04f9dc7cf2c469aabba882fdf4d9840.png

5. Install Mycat

MyCat installation and deployment Mycat installation actually only needs to decompress the compressed package, which is very simple.

[After the installation is complete, the directory is as follows]: 

8538d090f5434b4a9c70944188580183.png

【Configuration】

The configuration files of Mycat are all in the conf directory. Here are a few commonly used files:

f1994f3f79b248a5ab0d51dc732d24bc.png

【MyCat structure】

The structure of Mycat is actually very easy to understand. Mycat is an agent, and behind Mycat is a physical database. Similar to Nginx for web servers. For users, all access is Mycat, and they will not touch the back-end database.

6. Configure Mycat

① Configuration of server.xml

Delete and modify the last one as follows

959b2eacbf82400e8c590b5a54c16f3c.png

 [The meaning of each configuration parameter]

2fc3d327ffef4e96bbb4a92fc562cf8e.png

② Configuration of schema.xml

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

        <schema name="nebula" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"/>
        <dataNode name="dn1" dataHost="auth" database="mytest" />
        <dataHost name="auth" maxCon="1000" minCon="10" balance="3"
                          writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
                <heartbeat>select user()</heartbeat>
                <writeHost host="hostM" url="192.168.198.142:3306" user="root"
                                   password="Nebula@123">
                        <readHost host="hostS" url="192.168.198.148:3306" user="root" password="Nebula@123" />
                </writeHost>
        </dataHost>
</mycat:schema>

b12fd54439f247389b6ac45a891f8c9b.png

 【Parameter Configuration Description】

f293c237ee4346ae859731403bd6c91c.png

[The following is a description of the configuration of each node]

2104d9c1e2204926bf9dabbcdb1e88b5.png

3c4184bab11a49fd9e25bd7ccf71e055.png

830f8477d4594bb3b2394ed591806084.pnge071ee63c2764b80a1aab9d1546532f9.png

 

 

 

 

7. Create a read-only user for the slave

The user test who reads the library is an added mysql user with only read permissions:

The master has write permission, and the slave only has read permission;

create user 'test'@'%' identified with mysql_native_password by
'Nebula@123';
GRANT select ON *.* TO 'test';
flush privileges;

afcc5a98eb904cb09679ea63dec240d6.png

Eight, start mycat

① The startup of Mycat is also very simple, enter the bin directory under mycat:

#启动
./mycat start(后台启动) ./mycat console(前台启动)


#停止
./mycat stop


#重启
./mycat restart

[If an exception is found at startup, check the logs in the logs directory]

  • wrapper.log is the log of program startup, see this for startup problems
  • mycat.log is the log when the script is executed. Check this file for the specific error content after the SQL script executes and reports an error.
  • mycat.log is the latest error log, and the historical log will be saved in a directory based on time.

The results of normal startup are as follows:

f210615929ec4a6fbc55dcff186b5e68.png

 [After mycat starts, the execution of the command is unsuccessful, and there may actually be an error in the configuration, resulting in poor execution of subsequent commands.


There will be two ports highlighted when mycat starts successfully

b73a85d4b2ed42c4aaaf56a0b312023b.png

8066 is the service port, which can add, delete, modify and check the virtual database connected to mycat;

9066 is the management port, check heartbeat, etc.;

9. Verify read-write separation service

① Connect to port 9066 of mycat to check the heartbeat

957ffe48e45946dc9153aca0166e8639.png

 ② Right-click mycat, click the command line interface to enter the sql statement

bf94af7dda504a478910878a8de0f988.png

③ Enter the command to check whether the heartbeat is normal, RS_CODE is 1 is normal

show @@heartbeat;

7e922ca130974a7f991a45046e16c28c.png

 You can see that hostM has W write permission, and hostS has R read permission

 ④ Use navicat to connect to mycat, as shown in the figure below, note that the port is 8066.

f75a947f656a4897883acf7cf4f270c4.png

 If the connection is successful, the following green will appear, otherwise please check:

28379a3b94ac46448e2c4b83dbd3a15c.png

④ Test reading, there are two ways to verify:

1) Turn off master-slave replication

  1. Close the slave from the data (that is, close the master-slave replication);
  2. Then insert a piece of data into a table in the mycat management terminal;
  3. Then use select to query the table, and you can see that there is no new piece of data in the query results.

(Explanation: Because the master-slave replication is turned off, new data is inserted in the master database, and the slave database is queried, so the newly inserted data will not be queried);

# close the slave

stop slave;
show slave status\G;

e0f8d1d900244dd685d122576179ee13.png

 #There is no data in the table_2 table of the original master and slave;

99980378bb764492b52911e1d464cc6d.png261b22708a334995aeaf4d490bb5fbbe.png

#Insert a piece of data to table_2 on mycat, and then view the table

[We found that the insertion was successful but there was no data in the view table. At this time, because we closed the master-slave connection, the data will not be synchronized from the master to the slave, and the slave has read permission, and the master has write permission, which is equivalent to our Viewing the database on mycat is viewing the slave machine, and writing data is writing data to the master machine, so there is no data when viewing the table_2 table, because the table_2 table of the slave machine is viewed]

bd274e994ce6455dbe8b1f0c66528d08.png

 #Check the table_2 table on mysql and find that the data is inserted successfully;

cbd8bb99afbb4d7c910876617801948a.png

  #Check the table_2 table on mysql and find that there is no data;

837cc7979fa04802a508fe3adb8f9680.png

2) Master-slave replication without closing slave

Do not close the master-slave replication of the slave , directly modify a certain value in the table in the slave library, while the value of the master library remains unchanged, when directly using the query table data, you will find that the query result is the data in the slave library table;

#Enable slave master-slave replication

778ed5adc0fb49fcbab3c2f13b7648eb.png

#Here insert a piece of data for table_2 on the slave library

19d28f5afc50478b98fbaa0cd1461cb0.png

 #Check the table_2 table of the main library and find that the data of the main library has not changed

fe7711f85d7e4bc087c95ea2b72d1399.png

 #Query on mycat, and find that the query is the data of the table_2 table of the slave library;

 42b4d8c1405f45dba3b6869e34a9e271.png

 

 

 

Guess you like

Origin blog.csdn.net/weixin_65690979/article/details/130170704