Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Separation of read and write by mysql+mycat

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Centos7

Masterslavemycat1.6client192.168.41.10192.168.41.11192.168.41.12192.168.41.13

The experimental environment shuts down its own firewall

Configure all host hosts files:

Master (41.10):

Vim / etc/my.cnf

Systemctl restart mysqld\ restart service read profile parameters

1) configure replication user and root user rights

2. Slave (41.11)

Configuration / etc/my.cnf profile

Systemctl restart mysqld

Mysql > change master to master_host='192.168.41.10',master_user='myslave',master_password='123.com',master_log_file='mysql-bin.000002',master_log_pos=1334

Mysql > start slave

Mysql > show slave status\ G

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Mysql > grant all on. To root@'%' identified by '123.com'

Mysql > flush privileges

Go back to the master host to create a test library

Mysql > create database test

3. Configure mycat (41.12)

1) deploy mycat

Wget http://dl.mycat.io/1.6-RELEASE/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz

[root@192] # tar zxf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz-C / usr/local

2) load environment variables

[root@192 ~] # vi / etc/profile

.

Export PATH=$PATH:/usr/local/java/bin

Export JAVA_HOME=/usr/local/java

Export MYCAT_HOME=/usr/local/mycat

Export PATH=$PATH:/usr/local/mycat/bin

[root@192 ~] # source / etc/profile

3) configure serve.xml

[root@192 ~] # cd / usr/local/mycat/conf/

[root@192 conf] # vim server.xml

.

123.com

Test

User test true

4) configure schema.xml

[root@192 conf] # vim schema.xml

The load balancer type of show slave status balance attribute. Currently, there are 4 values:

Balance= "0", the read-write separation mechanism is not enabled, and all read operations are sent to the currently available writeHost. Balance= "1", all readHost and stand by writeHost participate in the load balancing of select statements. To put it simply, when the double master and double slave mode (M1-> S1, M2-> S2, and M1 and M2 are the master and standby of each other), under normal circumstances, M2Magi S1 and S2 participate in the load balancing of select statements. Balance= "2", all read operations are randomly distributed on writeHost and readhost. Balance= "3", all read requests are randomly distributed to the corresponding readhost of wiriterHost for execution. WriterHost does not bear the read pressure. Note that balance=3 is only available in 1.4 and later versions, but not in 1.3.

WriteType attribute, load balancer type. Currently, there are three values:

WriteType= "0", all write operations are sent to the first writeHost of the configuration, and the first one hangs the second writeHost that is still alive. After restarting, the switch is recorded in the configuration file: dnindex.properties. WriteType= "1", all write operations are randomly sent to the configured writeHost. WriteType= "2", not realized.

SwitchType attribute

-1 means that the default value of 1 is not automatically switched, while the automatic switch of 2 is based on the status of MySQL master-slave synchronization.

5) start the mycat server

[root@192 logs] # mycat start

[root@192 logs] # tailf wrapper.log # observe the startup log to facilitate troubleshooting

[root@192 logs] # ss-anpt | grep java

LISTEN 01 127.0.0.1 java 32000: users: ("java", pid=40133,fd=4)

LISTEN 0 50:: 50632: users: ("java", pid=40133,fd=51)

LISTEN 0 100:: 9066: users: ("java", pid=40133,fd=69)

LISTEN 0 50:: 33782: users: ("java", pid=40133,fd=53)

LISTEN 0 50:: 1984: users: ("java", pid=40133,fd=52)

LISTEN 0 100: 8066: * users: ("java", pid=40133,fd=73)

4. The client (41.13) verifies the separation of read and write.

① logs in to the connection port

[root@192] # mysql-h 192.168.41.12-P 8066-uroot-p123.com

Mysql > show databases

+-+

| | DATABASE |

+-+

| | test |

+-+

1 row in set (0.01 sec)

Mysql > use test

Database changed

Mysql > create table tb (id int)

Query OK, 0 rows affected (0.03 sec)

Mysql > insert into tb values (1)

Query OK, 1 row affected (0.05sec)

Mysql > select * from tb

+-+

| | id |

+-+

| | 1 |

+-+

1 row in set (0.02 sec)

② login management port

[root@192] # mysql-P9066-uroot-p123.com-h 192.168.41.12

Mysql > show @ @ datasource

+-+

| | DATANODE | NAME | TYPE | HOST | PORT | Wamp R | ACTIVE | IDLE | SIZE | EXECUTE | READ_LOAD | WRITE_LOAD |

+-+

| | dn1 | hostM1 | mysql | 192.168.1.12 | 3306 | W | 0 | 10 | 1000 | 49 | 0 | 2 |

| | dn1 | hostS1 | mysql | 192.168.1.10 | 3306 | R | 0 | 6 | 1000 | 44 | 2 | 0 |

+-+

Observing the load change of the two hosts from the management port, it is found that READ_LOAD is 2 in hostS1 and WRITE_LOAD is 2 in hostM1, indicating that the separation of read and write has been realized.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report