当前位置:网站首页>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()
边栏推荐
- Aike AI frontier promotion (7.7)
- 有什么类方法或是函数可以查看某个项目的Laravel版本的?
- [爬虫]使用selenium时,躲避脚本检测
- 测试下摘要
- 免费手机号码归属地API查询接口
- About IPSec
- Sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]
- AUTOCAD——大于180度的角度标注、CAD直径符号怎么输入?
- 《ASP.NET Core 6框架揭秘》样章[200页/5章]
- PHP calls the pure IP database to return the specific address
猜你喜欢
DHCP 动态主机设置协议 分析
[爬虫]使用selenium时,躲避脚本检测
Day-15 common APIs and exception mechanisms
[statistical learning methods] learning notes - Chapter 5: Decision Tree
《开源圆桌派》第十一期“冰与火之歌”——如何平衡开源与安全间的天然矛盾?
2022a special equipment related management (boiler, pressure vessel and pressure pipeline) simulated examination question bank simulated examination platform operation
聊聊Redis缓存4种集群方案、及优缺点对比
Common knowledge of one-dimensional array and two-dimensional array
Leetcode skimming: binary tree 27 (delete nodes in the binary search tree)
处理链中断后如何继续/子链出错removed from scheduling
随机推荐
Sorting, dichotomy
HZOJ #236. 递归实现组合型枚举
关于 appium 如何关闭 app (已解决)
What kind of methods or functions can you view the laravel version of a project?
HZOJ #236. Recursive implementation of combinatorial enumeration
[pytorch practice] write poetry with RNN
.Net下极限生产力之efcore分表分库全自动化迁移CodeFirst
ip2long与long2IP 分析
Common knowledge of one-dimensional array and two-dimensional array
mysql怎么创建,删除,查看索引?
Day21 multithreading
Leetcode skimming: binary tree 23 (mode in binary search tree)
Polymorphism, final, etc
基于NeRF的三维内容生成
SSM框架搭建的步骤
Find ID value MySQL in string
@Resource和@Autowired的区别?
Leetcode skimming: binary tree 27 (delete nodes in the binary search tree)
“新红旗杯”桌面应用创意大赛2022
2022 polymerization process test question simulation test question bank and online simulation test