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.
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
②Edit/etc/profile
vim /etc/profile
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
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]:
【Configuration】
The configuration files of Mycat are all in the conf directory. Here are a few commonly used files:
【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
[The meaning of each configuration parameter]
② 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>
【Parameter Configuration Description】
[The following is a description of the configuration of each node]
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;
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:
[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
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
② Right-click mycat, click the command line interface to enter the sql statement
③ Enter the command to check whether the heartbeat is normal, RS_CODE is 1 is normal
show @@heartbeat;
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.
If the connection is successful, the following green will appear, otherwise please check:
④ Test reading, there are two ways to verify:
1) Turn off master-slave replication
- Close the slave from the data (that is, close the master-slave replication);
- Then insert a piece of data into a table in the mycat management terminal;
- 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;
#There is no data in the table_2 table of the original master and slave;
#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]
#Check the table_2 table on mysql and find that the data is inserted successfully;
#Check the table_2 table on mysql and find that there is no data;
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
#Here insert a piece of data for table_2 on the slave library
#Check the table_2 table of the main library and find that the data of the main library has not changed
#Query on mycat, and find that the query is the data of the table_2 table of the slave library;