当前位置:网站首页>Practical case: using MYCAT to realize read-write separation of MySQL
Practical case: using MYCAT to realize read-write separation of MySQL
2022-07-07 12:57:00 【LC181119】
Practical cases : utilize Mycat Realization MySQL Separation of reading and writing
System environment of all hosts :
[[email protected] ~]# cat /etc/centos-release CentOS Linux release 8.3.2011
There are three servers in total
client 10.0.0.7 mycat-server 10.0.0.8 # Memory recommendations 2G above mysql-master 10.0.0.18 MySQL 8.0 mysql-slave 10.0.0.28 MySQL 8.0
close SELinu And the firewall
systemctl stop firewalld setenforce 0 Time synchronization
1. establish MySQL Master slave database
# master server [[email protected] ~]# yum -y install mysql-server [[email protected] ~]# systemctl enable --now mysqld # From the server [[email protected] ~]# yum -y install mysql-server [[email protected] ~]# systemctl enable --now mysqld
1) modify master and slave Configuration file on
#master Upper my.cnf [[email protected] ~]# vim /etc/my.cnf [mysqld] server-id=18 [[email protected] ~]# systemctl restart mysqld #slave Upper my.cnf [[email protected] ~]# vim /etc/my.cnf [mysqld] server-id=28 [[email protected] ~]# systemctl restart mysqld
2)Master Create a replication user on (10.0.0.8)
[[email protected] ~]# mysql mysql> show master logs; +---------------+-----------+-----------+ | Log_name | File_size | Encrypted | +---------------+-----------+-----------+ | binlog.000001 | 179 | No | | binlog.000002 | 156 | No | +---------------+-----------+-----------+ 2 rows in set (0.00 sec) mysql> create user r[email protected]'10.0.0.%' identified by '123456' ; mysql> grant replication slave on *.* to [email protected]'10.0.0.%';
3) Slave On the implementation (10.0.0.18)
[[email protected] ~]# mysql mysql> CHANGE MASTER TO MASTER_HOST='10.0.0.18', MASTER_USER='repluser', MASTER_PASSWORD='123456', MASTER_PORT=3306, MASTER_LOG_FILE='binlog.000002', MASTER_LOG_POS=156; mysql> start slave; mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for source to send event Master_Host: 10.0.0.18 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: binlog.000002 Read_Master_Log_Pos: 11277 Relay_Log_File: slave-relay-bin.000002 Relay_Log_Pos: 11442 Relay_Master_Log_File: binlog.000002 Slave_IO_Running: Yes Slave_SQL_Running: Yes ... Omit ...
2、 stay MySQL proxy server 10.0.0.8 install mycat And start the
[[email protected] ~]# yum -y install java [[email protected] ~]# java -version # Confirm successful installation openjdk version "1.8.0_312" OpenJDK Runtime Environment (build 1.8.0_312-b07) OpenJDK 64-Bit Server VM (build 25.312-b07, mixed mode) # Download and install [[email protected] ~]# wge thttp://dl.mycat.org.cn/1.6.7.6/20210303094759/Mycat-server-1.6.7.6-release-20210303094759-linux.tar.gz [[email protected] ~]# mkdir /apps [[email protected] ~]# tar xvf Mycat-server-1.6.7.6-release-20210303094759-linux.tar.gz -C /apps/ # Configure environment variables [[email protected] ~]# echo 'PATH=/apps/mycat/bin:$PATH' > /etc/profile.d/mycat.sh [[email protected] ~]# . /etc/profile.d/mycat.sh # start-up mycat Be careful : This step starts slowly , It's going to take a while , In addition, if the memory is too small , Will result in failure to start Recommended memory 3G [[email protected] ~]# mycat start Starting Mycat-server... # Check the log , Confirm success , It may take a while to see the hint of success [[email protected] ~]# tail -f /apps/mycat/logs/wrapper.log INFO | jvm 1 | 2021/12/25 00:22:40 | MyCAT Server startup successfully. see logs in logs/mycat.log # Use the default password 123456 To connect mycat [[email protected] ~]# mysql -uroot -p123456 -h10.0.0.8 -P8066 mysql> show databases; +----------+ | DATABASE | +----------+ | TESTDB | +----------+ 1 row in set (0.00 sec)
4、 stay mycat Modify on server server.xml File configuration Mycat Connection information (10.0.0.8)
... Omit ... # Delete Note , And modify the following line 8066 Change it to 3306 [[email protected] ~]# cat /apps/mycat/conf/server.xml <property name="serverPort">3306</property> 46 <property name="managerPort">3306</property> 47 <property name="idleTimeout">300000</property> 48 <property name="authTimeout">15000</property> 49 <property name="bindIp">0.0.0.0</property> 50 <property name="dataNodeIdleCheckPeriod">300000</property> # Delete 5 * 60 * 1000L; // Connection free check Delete # The following part 110 <user name="root" defaultAccount="true"> # Connect mycat Username 111 <property name="password">magedu</property> # Connect mycat Password 112 <poperty name="schemas">TESTDB</property> # The database name should be the same as schema.xml Corresponding ... Omit ... 125 </user>
What we use here is root, The password for magedu, The logical database is TESTDB, This information can be defined at will , Both read and write permissions
Yes , There is no special permission for the table . Focus on the above configuration , Other defaults are ok .
5、 modify schema.xml Implement the read-write separation strategy (10.0.0.8)
[[email protected] ~]# vim /apps/mycat/conf/schema.xml <!DOCTYPE mycat: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="***mycat***" /> # among mycat surface Show the actual database name of the back-end server <dataHost name="localhost1" maxCon="1000" minCon="10" balance="***1***" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> ***<writeHost host="host1" url="10.0.0.18:3306" user="root" password="123456">*** ***<readHost host="host2" url="10.0.0.28:3306" user="root" password="123456" />*** </writeHost> </dataHost> </mycat:schema> # above *** Part indicates the content to be modified in the original configuration file # Pay attention to case # Final document content <?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"> </schema> <dataNode name="dn1" dataHost="localhost1" database="hellodb" /> <dataHost name="localhost1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <writeHost host="host1" url="10.0.0.18:3306" user="root" password="123456"> <readHost host="host2" url="10.0.0.28:3306" user="root" password="123456" /> </writeHost> </dataHost> </mycat:schema> # Restart mycat [[email protected] ~]# mycat restart
In the above configuration ,balance Change it to 1, It means separation of reading and writing . The effect of the above configuration is 10.0.0.18 Give priority to the library ,10.0.0.28 by
Slave Library
Be careful : Be sure to use root/123456 Permission successfully logged in 10.0.0.18 and 10.0.0.28 On top of the machine mysql database . meanwhile ,
And you have to empower mycat Machines can use root/123456 Permission to successfully log in to these two machines mysql database !! It's important , no
Will cause login mycat after , Operation on Library and table failed !
Example :schema.xml
6. Create a user on the back-end master server and update it mycat to grant authorization (10.0.0.18)
[[email protected] ~]# mysql mysql> create [email protected]'10.0.0.%' identified by '123456'; mysql> grant all on *.* to [email protected]'10.0.0.%';
7、 Connect and test on the client (10.0.0.7)
[[email protected] ~]# mysql -uroot -pmagedu -h10.0.0.8 mysql> show databases; +----------+ | DATABASE | +----------+ | TESTDB | +----------+ 1 row in set (0.00 sec) mysql> use TESTDB; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +-------------------+ | Tables_in_hellodb | +-------------------+ | classes | | coc | | courses | | scores | | students | | teachers | | toc | +-------------------+
8. Read / write separation is realized through general log confirmation
stay mysql View the general log in
# Check whether the slow log is enabled mysql> show variables like 'general%'; +------------------+--------------------------+ | Variable_name | Value | +------------------+--------------------------+ | general_log | OFF | | general_log_file | /var/lib/mysql/slave.log | +------------------+--------------------------+ 2 rows in set (0.01 sec) # Open slow log mysql> set global general_log =1; # mysql> show variables like 'general_log_file'; +------------------+--------------------------+ | Variable_name | Value | +------------------+--------------------------+ | general_log_file | /var/lib/mysql/slave.log | +------------------+--------------------------+ 1 row in set (0.00 sec)
Enable common logging on master and slave servers respectively , View read write separation
[[email protected] ~]#vim /etc/my.cnf.d/mariadb-server.cnf [mysqld] general_log=ON [[email protected] ~]#systemctl restart mariadb [[email protected] ~]#tail -f /var/lib/mysql/centos8.log
9、 Stop from node ,MyCAT Automatically schedule read requests to the master node
[[email protected] ~]#systemctl stop mysqld [[email protected] ~]#mysql -uroot -pmagedu -h10.0.0.8 -P8066 mysql> select @@server_id; +-------------+ | @@server_id | +-------------+ | 1 | +-------------+ 1 row in set (0.00 sec) mysql> # Stop master ,MyCAT Write requests to the slave node are not automatically scheduled MySQL [TESTDB]> insert teachers values(5,'wang',30,'M'); ERROR 1184 (HY000): java.net.ConnectException: Connection refused
10、MyCAT Health check methods for back-end servers select user()
# Open the general log [[email protected] ~]#mysql mysql> set global general_log=1; [[email protected] ~]#mysql mysql> set global general_log=1; # View general log [[email protected] ~]# tail -f /var/lib/mysql/master.log /usr/libexec/mysqld, Version: 8.0.26 (Source distribution). started with: Tcp port: 3306 Unix socket: /var/lib/mysql/mysql.sock Time Id Command Argument 2022-01-02T15:42:42.887840Z 60 Query select user() 2022-01-02T15:42:52.889744Z 58 Query select user() 2022-01-02T15:43:02.887842Z 57 Query select user() [[email protected] ~]# tail -f /var/lib/mysql/slave.log /usr/libexec/mysqld, Version: 8.0.26 (Source distribution). started with: Tcp port: 3306 Unix socket: /var/lib/mysql/mysql.sock Time Id Command Argument 2021-12-25T17:38:29.668676Z 45 Query select user() 2021-12-25T17:38:32.291701Z 46 Query show variables like 'general%' 2021-12-25T17:38:39.670626Z 42 Query select user() 2021-12-25T17:38:49.669060Z 43 Query select user() 2021-12-25T17:38:59.673280Z 44 Query select user()
边栏推荐
猜你喜欢
Image pixel read / write operation
明星企业普渡科技大裁员:曾募资超10亿 腾讯红杉是股东
Aosikang biological sprint scientific innovation board of Hillhouse Investment: annual revenue of 450million yuan, lost cooperation with kangxinuo
ACL 2022 | 序列标注的小样本NER:融合标签语义的双塔BERT模型
How to apply @transactional transaction annotation to perfection?
Leetcode skimming: binary tree 23 (mode in binary search tree)
认养一头牛冲刺A股:拟募资18.5亿 徐晓波持股近40%
Day22 deadlock, thread communication, singleton mode
聊聊Redis缓存4种集群方案、及优缺点对比
opencv的四个函数
随机推荐
Several ways to clear floating
COSCon'22 社区召集令来啦!Open the World,邀请所有社区一起拥抱开源,打开新世界~
test
事务的七种传播行为
Master formula. (used to calculate the time complexity of recursion.)
How to reset Firefox browser
Polymorphism, final, etc
飞桨EasyDL实操范例:工业零件划痕自动识别
Layer pop-up layer closing problem
Shortcut key of Bash
About IPSec
Day21 multithreading
HZOJ #240. Graphic printing IV
[statistical learning method] learning notes - support vector machine (I)
认养一头牛冲刺A股:拟募资18.5亿 徐晓波持股近40%
《ASP.NET Core 6框架揭秘》样章[200页/5章]
[statistical learning method] learning notes - logistic regression and maximum entropy model
【从 0 开始学微服务】【03】初探微服务架构
. Net ultimate productivity of efcore sub table sub database fully automated migration codefirst
PHP calls the pure IP database to return the specific address