The database performance optimization generally adopts the cluster method, and the Oracle cluster hardware and software investment is expensive. Today, I spent a day building a cluster environment based on MySQL.
1. Main idea
Simply put, realize mysql master and backup replication --> use mycat to achieve load balancing.
Compared with the commonly used separation methods of read and write, mycat is recommended, which has an active community and stable performance.
2. Test environment
MYSQL version: Server version: 5.5.53, you can download the WINDWOS installation package on the official website. Note: Make sure that the MySQL version is 5.5 or later, and the active-standby synchronization configuration is different in previous versions.
Linux implementation ideas are similar, just modify my.cnf.
1) A master mysql. 192.168.110.1:3306, user root, password root. Operating system: win7 x64, memory: 4g
Installation path: C:\Program Files\MySQL\MySQL Server 5.5\bin
2) B prepares mysql. 192.168.110.2:3306, user root, password root. Operating system: win2003 x64, memory: 1g
Installation path: C:\Program Files\MySQL\MySQL Server 5.5\bin
3) Create the sync_test database in the mysql of A master and B backup.
3. Implement mysql master-slave replication
The main idea: A master mysql opens the log, B standby mysql reads the operation log, and executes it synchronously.
Generally, master-slave synchronization is not recommended.
3.1 Configure A master mysql.
1) Modify my.ini. You need to create the log directory and the mysql-bin.log file in the relevant location of log-bin="C:/Program Files/MySQL/MySQL Server 5.5/log/mysql-bin.log".
[mysqld] server-id=1 #host identifier, integer port=3306 log-bin="C:/Program Files/MySQL/MySQL Server 5.5/log/mysql-bin.log" #Ensure this file is writable read-only=0 #Host, can read and write binlog-do-db=sync_test #Need to back up the database, write multiple lines binlog-ignore-db=mysql #Databases that do not need to be backed up, write multiple lines
2) Allow MYSQL remote access
#Login to mysql console Enter %home%/bin and execute mysql -uroot -proot #Authorization. Allow root user to remotely access A master mysql from the IP range of 192.168.110.* mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.110.*' IDENTIFIED BY 'root' WITH GRANT OPTION; # to take effect. This action is important! ! ! mysql>FLUSH PRIVILEGES;
3) Restart A main mysql database
Enter %home%/bin and execute mysql -uroot -proot mysql>net stop mysql; mysql>net start mysql;
4) View the main mysql log status
mysql> show master status\G; *************************** 1. row *************************** File: mysql-bin.000003 Position: 107 Binlog_Do_DB: sync_test Binlog_Ignore_DB: mysql 1 row in set (0.00 sec) ERROR: No query specified
3.2 Configure B backup mysql
1) Modify my.ini. You need to create the log directory and the mysql-bin.log file in the relevant location of log-bin="C:/Program Files/MySQL/MySQL Server 5.5/log/mysql-bin.log".
[mysqld] # add for sycn test server-id=2 #Slave ID log-bin="C:/Program Files/MySQL/MySQL Server 5.5/log/mysql-bin.log" #Ensure this file is writable #master-host="192.168.110.1" #Host Ip #master-user=root #Database access username #master-pass=root #Database access password #master-port=3306 #host port #master-connect-retry=60 #If the slave server finds that the master server is disconnected, the reconnection time difference (seconds) replicate-do-db=sync_test #Only replicate a library replicate-ignore-db=mysql #Do not replicate a library
2) Restart B standby mysql database
Enter %home%/bin and execute mysql -uroot -proot mysql>net stop mysql; mysql>net start mysql;
3) Configure the data source of the B standby database, and verify whether the highlighted state is normal.
mysql>change master to master_host='192.168.110.1',master_port='3306',master_user='root',master_password='root'; mysql>slave start; mysql>show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send even Master_Host: 192.168.110.1 Master_User: root Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 107 Relay_Log_File: wjt-1c698d8a032-relay-bin.00001 Relay_Log_Pos: 253 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: sync_test Replicate_Ignore_DB: mysql Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 107 Relay_Log_Space: 565 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 1 row in set (0.00 sec) ERROR: No query specified
3.3 Verify the synchronization configuration result.
1) A master mysql: Use the navicat tool to create a sync_table table in the sync_test library and add some data.
2) B standby mysql: Use the navicat tool to view the sync_test library, and you can see that the sync_table table and data have been synchronized.
4. Realize read-write separation
The main idea: use the mycat middleware to forward sql commands to the back-end mysql node. mycat is not responsible for database synchronization.
4.1 Install mycat
1) What is mycat? It can be considered as a database access middleware, but it is more like f5, ngnix and other products, with functions such as access routing, multi-table sharding and sharding operations. It's powerful anyway
Download: http://www.mycat.io/ , this article uses: 1.6-RELEASE
mycat is written in java, so it can be downloaded in multiple versions such as linux and windows.
2) Unzip Mycat-server-1.6-RELEASE-20161012170031-win.tar to D:\dev-bin\mycat directory
3) Make sure that the java environment is above jdk1.7, otherwise mycat will not support it.
4) Installation is complete
4.2 Configure mycat
1) server.xml. Configure access users and permissions. Modify the highlighted information, where admin and user are users who access mycat, and TESTDB is a virtual database of mycat for upper-layer applications to access.
<user name="admin"> <property name="password">admin</property> <property name="schemas">TESTDB</property> <!-- Table-level DML permission settings--> <!-- <privileges check="false"> <schema name="TESTDB" dml="0110" > <table name="tb01" dml="0000"></table> <table name="tb02" dml="1111"></table> </schema> </privileges> --> </user> <user name="user"> <property name="password">user</property> <property name="schemas">TESTDB</property> <property name="readOnly">true</property> </user>
2) schema.xml. This part is not very easy to understand. It is simplified and divided into three main configurations: schema, dataNode, and dataHost.
The <scheme> node defines the virtual database of mycat as TESTDB, balance="1": write operations are routed to machine A, and read operations are routed to machine B.
<?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"> <!-- Not configured here, it means that all tables are sharded to the dn1 node --> </schema> <dataNode name="dn1" dataHost="localhost1" database="sync_test" /> <dataHost name="localhost1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <!-- can have multi write hosts --> <writeHost host="hostM1" url="192.168.110.1:3306" user="root" password="root"> <!-- can have multi read hosts --> <readHost host="hostS2" url="192.168.110.2:3306" user="root" password="root" /> </writeHost> </dataHost> </mycat:schema>
4.3 Start mycat
1) Start mycat
D:\dev-bin\mycat\bin>startup_nowrap.bat
The background information is as follows:
D:\dev-bin\mycat\bin>startup_nowrap.bat D:\dev-bin\mycat\bin>REM check JAVA_HOME & java D:\dev-bin\mycat\bin>set "JAVA_CMD=C:\Program Files (x86)\Java\jdk1.7.0_13/bin/java" D:\dev-bin\mycat\bin>if "C:\Program Files (x86)\Java\jdk1.7.0_13" == "" goto noJavaHome D:\dev-bin\mycat\bin>if exist "C:\Program Files (x86)\Java\jdk1.7.0_13\bin\java.exe" goto mainEntry D:\dev-bin\mycat\bin>REM set HOME_DIR D:\dev-bin\mycat\bin>set "CURR_DIR=D:\dev-bin\mycat\bin" D:\dev-bin\mycat\bin>cd .. D:\dev-bin\mycat>set "MYCAT_HOME=D:\dev-bin\mycat" D:\dev-bin\mycat>cd D:\dev-bin\mycat\bin #If the startup fails, please modify the following parameters in the D:\dev-bin\mycat\bin\startup_nowrap.bat file. The default memory usage is 2G
D:\dev-bin\mycat\bin>"C:\Program Files (x86)\Java\jdk1.7.0_13/bin/java" -server -Xms512m -Xmx512m -XX:MaxPermSize=64M -XX:+AggressiveOpts -XX:MaxDirectMemorySize=768m -DMYCAT_HOME=D:\ p "..\conf;..\lib\*" io.mycat.MycatStartup 。。。。。。。。。。。。。。。。。。。。。。。。。。。 MyCAT Server startup successfully. see logs in logs/mycat.log #Startup successfully, you will see the following information.
If information such as 192.168.110.2 not connected appears in the log, please allow B standby mysql remote access.
#Login to mysql console Enter %home%/bin and execute mysql -uroot -proot #Authorization. Allow root user to remotely access Bmysql from the IP range of 192.168.110.* mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.110.*' IDENTIFIED BY 'root' WITH GRANT OPTION; # to take effect. This action is important! ! ! mysql>FLUSH PRIVILEGES;
4.4 Test read-write separation.
1. Verify that it is synchronized
1) Use navicat to connect to mycat, the operation method is the same as that of connecting to the physical mysql library, user admin, password admin, port 8066.
2) In the TESTDB virtual library, create a new table test2 and add some data
3) Check that the data of node A and node B have been synchronized.
2. Turn off the synchronization of B standby mysql, and verify the read-write separation.
mysql> slave stop; Query OK, 0 rows affected (0.00 sec) mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Master_Host: 192.168.110.1 Master_User: root Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 478 Relay_Log_File: wjt-1c698d8a032-relay-bin.00001 Relay_Log_Pos: 624 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: No Slave_SQL_Running: No Replicate_Do_DB: sync_test Replicate_Ignore_DB: mysql Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 478 Relay_Log_Space: 936 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: NULL Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 1 row in set (0.00 sec) ERROR: No query specified
3) Use navicat to connect to mycat, the operation method is the same as that of connecting to the physical mysql library, user admin, password admin, port 8066.
a. After the connection is successful, you will see the TESTDB database and test data table.
b. Add some data to the test table and save it.
c. Execute select * from test to view the test operation, and you will see that the data has not been updated.
Reason: mycat routes the query sq to B, so the result set read is inconsistent.
Finally, use mycat to learn from the authoritative guide on the official website. The dual-active and dual-standby architecture will be updated later.