MYSQL: use la herramienta mycat para realizar la separación de lectura y escritura de maestro-esclavo de mysql

MYSQL: use la herramienta mycat para realizar la separación de lectura y escritura de maestro-esclavo de mysql

1. Introducción al principio de separación lectura-escritura

Arquitectura del entorno de producción : utilice la replicación maestro-esclavo para sincronizar los datos y luego use la separación de lectura y escritura para mejorar la capacidad de carga simultánea de la base de datos.

Las ventajas de la separación de lectura y escritura:
①Para aumentar la presión de acceso, el rendimiento de un solo servidor se ha convertido en un cuello de botella y la carga debe compartirse.
②La biblioteca principal es responsable de la escritura y la biblioteca esclava es responsable de la lectura, lo que alivia la ocupación del bloqueo X (bloqueo exclusivo) y el bloqueo S (bloqueo compartido).
③ El motor myisam se puede utilizar desde la biblioteca para mejorar el rendimiento de las consultas y ahorrar gastos generales del sistema.
④ Incrementar la redundancia y mejorar la disponibilidad.

La realización de la separación de lectura y escritura:
①La realización de la capa de aplicación : realizar la separación de lectura y escritura dentro de la aplicación y en el conector. [Implementación interna de separación de lectura y escritura, implementación más fácil, mejor rendimiento cuando el acceso no es grande, el código de ajuste de la arquitectura también debe ajustarse, es difícil implementar aplicaciones avanzadas y no se puede aplicar a escenarios de aplicaciones a gran escala]
② Implementación de middleware : leer en programas de middleware externos Escriba la separación. [Mycat, no mucha presentación]

2. Middleware de subtabla de subbase de datos de base de datos Mycat

2.1 Introducción a mycat

mycat es un clúster de base de datos de código abierto para el desarrollo de aplicaciones empresariales. La transacción de soporte, ACID, es un nuevo tipo de producto de middleware de base de datos.

mysql se da cuenta de la topología de separación de lectura y escritura:
Inserte la descripción de la imagen aquí
mycat se puede instalar y ejecutar en windows, linux, mac, solaris y otros sistemas.

sitio web oficial de mycat: http://www.mycat.org.cn/

2.2 El middleware Mycat se da cuenta de la implementación de separación de lectura y escritura

Nombre de la CPU dirección IP Tipo de base de datos maestro-esclavo versión de mysql host de instalación mycat
Maestro 192.168.10.10 Biblioteca principal mysql 5.7.31 192.168.10.10
esclavo 192.168.10.20 Biblioteca esclava mysql 5.7.31

①Descargar y descomprimir mycat:

# 下载tar包并解压到/usr/local/mycat目录
[root@master ~]# ll Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz 
-rw-r--r--. 1 root root 21760812 9月  23 15:36 Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz
[root@master ~]# tar xzf Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz -C /usr/local/
[root@master ~]# ll /usr/local/mycat/
总用量 12
drwxr-xr-x. 2 root root  190 9月  23 15:37 bin
drwxrwxrwx. 2 root root    6 10月 22 2019 catlet
drwxrwxrwx. 4 root root 4096 9月  23 15:37 conf
drwxr-xr-x. 2 root root 4096 9月  23 15:37 lib
drwxrwxrwx. 2 root root    6 1月   5 2020 logs
-rwxrwxrwx. 1 root root  227 1月   5 2020 version.txt

# 创建mycat用户,并设置密码
[root@master ~]# useradd mycat
[root@master ~]# echo 123456 | passwd --stdin mycat
更改用户 mycat 的密码 。
passwd:所有的身份验证令牌已经成功更新。

# 设置/usr/local/mycat目录的所有者和所属组为mycat
[root@master ~]# chown -R mycat:mycat /usr/local/mycat/
[root@master ~]# ll /usr/local/mycat/
总用量 12
drwxr-xr-x. 2 mycat mycat  190 9月  23 15:37 bin
drwxrwxrwx. 2 mycat mycat    6 10月 22 2019 catlet
drwxrwxrwx. 4 mycat mycat 4096 9月  23 15:37 conf
drwxr-xr-x. 2 mycat mycat 4096 9月  23 15:37 lib
drwxrwxrwx. 2 mycat mycat    6 1月   5 2020 logs
-rwxrwxrwx. 1 mycat mycat  227 1月   5 2020 version.txt

bin程序目录
conf配置文件目录
lib目录主要存放mycat依赖的一些jar文件
logs目录存放日志

# 查看版本信息 1.6.7.4-release
[root@master ~]# cd /usr/local/mycat/
[root@master mycat]# cat version.txt
BuildTime  2020-01-05 08:41:01
GitVersion   f929f96a16852869bc9dc63f4c0f192ee02818e0
MavenVersion 1.6.7.4-release
GitUrl https://github.com/MyCATApache/Mycat-Server.git
MyCatSite http://www.mycat.org.cn

②Instale una versión superior de JDK, al menos superior a JDK1.7:

# 卸载原来的openjdk,安装oracle-jdk
# oracle-jdk性能比openjdk好,兼容性大。
[root@master ~]# java -version
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)
[root@master ~]# yum remove -y java-* 

# 下载oracle-jdk,解压到/usr/local/下
[root@master ~]# tar xzvf /usr/local/src/jdk-8u191-linux-x64.tar.gz -C /usr/local/

# 在/usr/local目录下做一个软连接
[root@master ~]# cd /usr/local/
oot@master local]# ln -s jdk1.8.0_191 jdk1.8

# 配置环境变量
[root@master ~]# vim /etc/profile.d/jdk8.sh
[root@master ~]# cat /etc/profile.d/jdk8.sh 
export JAVA_HOME=/usr/local/jdk1.8.0_191
export CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/jar/tools.jar:$JAVA_HOME/jre/lib
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH

# 使环境变量生效
[root@master ~]# chmod +x /etc/profile.d/jdk8.sh
[root@master ~]# /etc/profile.d/jdk8.sh
[root@master ~]# bash

# 查看jdk版本
# 不配置环境变量的话可以做一个软连接
[root@master local]# ln -s /usr/local/jdk1.8/bin/java /usr/bin/java
[root@master local]# java -version
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)

③ Implemente mycat para comenzar:

# 配置环境变量,在/etc/profile.d目录下创建mycat.sh脚本,添加环境变量
[root@master ~]# vim /etc/profile.d/mycat.sh
[root@master ~]# cat /etc/profile.d/mycat.sh 
MYCAT_HOME=/usr/local/mycat
PATH=$MYCAT_HOME/bin:$PATH

# 使环境变量立即生效
[root@master ~]# chmod +x /etc/profile.d/mycat.sh
[root@master ~]# source /etc/profile.d/mycat.sh 

# 定义mycat集群中各服务器的IP地址和主机名的映射关系
[root@master ~]# vim /etc/hosts
192.168.10.10 master
192.168.10.20 slave

# 修改mycat的用户信息和授权信息
[root@master ~]# cd /usr/local/mycat/conf/
[root@master conf]# cp server.xml server.xml.bak
[root@master conf]# echo "" > server.xml
[root@master conf]# vim server.xml
# wabong用户用于写,rabong用户用于读
[root@master conf]# cat server.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://io.mycat/">
	<system>
	<property name="defaultSqlParser">druidparser</property>
	</system>
	<!--以下设置为应用访问帐号权限 -->
	<!--可写账号 -->
	<user name="wabong">
	<property name="password">123456</property>
	<property name="schemas">test</property>
	</user>
	<!--可读账号 -->
	<user name="rabong">    # 读库账号
	<property name="password">123456</property>
	<property name="schemas">test</property>
	<property name="readOnly">true</property>
	</user>
</mycat:server>

# 配置mycat架构
[root@master conf]# cp schema.xml schema.xml.bak
[root@master conf]# echo "" > schema.xml
[root@master conf]# vim schema.xml
[root@master conf]# cat schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
 <schema name="test" checkSQLschema="false" sqlMaxLimit="100" dataNode='dn1'> </schema>
 <dataNode name="dn1" dataHost="dthost" database="test"/>
 <dataHost name="dthost" maxCon="500" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="-1" slaveThreshold="100">
 <heartbeat>select user()</heartbeat>
 <!--后端写库信息 -->
 <writeHost host="master" url="192.168.10.10:3306" user="mycat" password="Abong123.">
	 <!--后端读库信息 -->
	<readHost host="slave" url="192.168.10.20:3306" user="mycat" password="Abong123." />
 </writeHost>
 </dataHost>
</mycat:schema>

# 启动mycat
[root@master ~]# mycat 
Usage: /usr/local/mycat/bin/mycat {
    
     console | start | stop | restart | status | dump }
[root@master ~]# mycat start
Starting Mycat-server...
[root@master ~]# cat /usr/local/mycat/logs/wrapper.log     # 看到这个日志记录,就说明已经启动成功了。
INFO   | jvm 1    | 2020/09/23 16:23:27 | MyCAT Server startup successfully. see logs in logs/mycat.log
[root@master ~]# mycat status;
Mycat-server is running (9115).

Configure la replicación síncrona maestro-esclavo de mysql:

# 配置主库/etc/my.cnf
[root@master ~]# vim /etc/my.cnf
log-bin=mysql-bin-master
server-id=1
binlog-do-db=test
binlog-ignore-db=mysql
[root@master ~]# systemctl restart mysqld

# 在主库上创建用户mycat,并授予all privileges的权限,允许在所有主机的客户端上使用mycat用户进行登录。
[root@master ~]# mysql -uroot -p
Enter password: 

mysql> grant all privileges on *.* to mycat@'%' identified by 'Abong123.';   # 用于读写分离的用户
Query OK, 0 rows affected, 1 warning (0.04 sec)

mysql> grant replication slave on *.* to [email protected] identified by 'Abong123.';   # 用于主从复制的用户
Query OK, 0 rows affected, 1 warning (0.03 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)

mysql> exit
Bye

# 主库上导出test数据库,并导入到从库。保持主从数据一致性。
[root@master ~]# mysqldump -uroot -p test > test.sql
Enter password: 
[root@master ~]# ll test.sql 
-rw-r--r--. 1 root root 6815 9月  24 11:24 test.sql
[root@master ~]# scp test.sql [email protected]:/root/

[root@slave ~]# mysql -uroot -p     # 导入test到从库前的检测
Enter password: 
mysql> show databases;    # 从库上没有test数据库,要先创建。
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> create database test;
Query OK, 1 row affected (0.00 sec)

mysql> exit
Bye
[root@slave ~]# mysql -uroot -p test < test.sql
Enter password: 

# 配置从库上的/etc/my.cnf
[root@slave ~]# vim /etc/my.cnf
[mysqld]
server-id=2
[root@slave ~]# systemctl restart mysqld

# 在从库上创建用户mycat,并授予all privileges的权限,允许在所有主机的客户端上使用mycat用户进行登录。
[root@slave ~]# mysql -uroot -p
Enter password: 

mysql> grant all privileges on *.* to mycat@'%' identified by 'Abong123.';
Query OK, 0 rows affected, 1 warning (0.04 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)

mysql> exit
Bye

# 开放主库上的3306端口号
[root@master ~]# firewall-cmd --permanent --zone=public --add-port=3306/tcp
success
[root@master ~]# firewall-cmd --reload 
success

# 建立主从关系
mysql> show master status\G;    # 主库信息
*************************** 1. row ***************************
             File: mysql-bin-master.000001
         Position: 154
     Binlog_Do_DB: test
 Binlog_Ignore_DB: mysql
Executed_Gtid_Set: 
1 row in set (0.00 sec)

ERROR: 
No query specified

# 配置从库对应主库信息:
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to master_host='192.168.10.10',master_port=3306,master_user='slave',master_password='Abong123.',master_log_file='mysql-bin-master.000001',master_log_pos= 154;
Query OK, 0 rows affected, 2 warnings (0.06 sec)

mysql> start slave;
Query OK, 0 rows affected (0.02 sec)

mysql> show slave status\G;    # 可以看到主从同步复制的关系已经建立起来。
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates

2.3 Prueba de separación de lectura y escritura de Mycat

Formato de inicio de sesión: mysql -urabong -p123456 -h 192.168.10.10 -P8066
mycat está instalado en 192.168.10.10, por lo que -h también es 192.168.10.10.
Para mycat, 8066 es el puerto de datos y 9066 es el puerto de administración. El
cliente inicia sesión en la base de datos mysql y usa el puerto 8066.
El cliente administra mycat mediante el puerto 9066.
①El usuario de solo lectura rabong inicia sesión en la base de datos mysql a través de mycat:

[root@master ~]# mysql -urabong -p123456 -h 192.168.10.10 -P8066
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.6.29-mycat-1.6.7.4-release-20200105164103 MyCat Server (OpenCloudDB)   # 可以看到是通过中间件mycat登录的

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use test;
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> desc emp;     # 可以看到rabong是没有写权限的,即只能执行select语句。
ERROR 1495 (HY000): User readonly
mysql> select * from emp;
+----+------+--------+
| id | name | deptno |
+----+------+--------+
|  1 | haha |     20 |
|  2 | xixi |     20 |
|  3 | ohoh |     20 |
|  5 | yeye |     20 |
+----+------+--------+
4 rows in set (0.10 sec)

②El usuario de escritura wabong inicia sesión en la base de datos mysql a través de mycat:

[root@master ~]# mysql -uwabong -p123456 -h 192.168.10.10 -P8066
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.6.29-mycat-1.6.7.4-release-20200105164103 MyCat Server (OpenCloudDB)   # 可以看到是通过中间件mycat登录的

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use test;
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> desc emp;      # 可以看到用户wabong是具有读写权限的
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | NO   | PRI | NULL    |       |
| name   | varchar(10) | YES  |     | NULL    |       |
| deptno | int(11)     | YES  | MUL | NULL    |       |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.40 sec)

mysql> select  * from emp;
+----+------+--------+
| id | name | deptno |
+----+------+--------+
|  1 | haha |     20 |
|  2 | xixi |     20 |
|  3 | ohoh |     20 |
|  5 | yeye |     20 |
+----+------+--------+
4 rows in set (0.01 sec)

③ Pruebe el impacto de la falla de la biblioteca principal o la biblioteca esclava en el acceso del usuario a la base de datos:

La biblioteca principal se ejecuta normalmente, detenga la biblioteca esclava:

[root@slave ~]# systemctl stop mysqld

[root@master ~]# mysql -uwabong -p123456 -h 192.168.10.10 -P8066
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.6.29-mycat-1.6.7.4-release-20200105164103 MyCat Server (OpenCloudDB)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use test;
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> desc emp;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | NO   | PRI | NULL    |       |
| name   | varchar(10) | YES  |     | NULL    |       |
| deptno | int(11)     | YES  | MUL | NULL    |       |
+--------+-------------+------+-----+---------+-------+
3 rows in set (1.66 sec)

mysql> select * from emp;
+----+------+--------+
| id | name | deptno |
+----+------+--------+
|  1 | haha |     20 |
|  2 | xixi |     20 |
|  3 | ohoh |     20 |
|  5 | yeye |     20 |
+----+------+--------+
4 rows in set (0.32 sec)

mysql> insert into emp values(4,'lala',20);
Query OK, 1 row affected (0.08 sec)

mysql> select * from emp;
+----+------+--------+
| id | name | deptno |
+----+------+--------+
|  1 | haha |     20 |
|  2 | xixi |     20 |
|  3 | ohoh |     20 |
|  4 | lala |     20 |
|  5 | yeye |     20 |
+----+------+--------+
5 rows in set (0.00 sec)

测试结果:主库上仍然可以进行读写操作。对主库的写操作会在从库恢复后再次进行同步复制,保证主从数据的一致性。

La biblioteca principal está colgada y la biblioteca esclava se ejecuta normalmente:

[root@master ~]# systemctl stop mysqld

[root@master ~]# mysql -uwabong -p123456 -h 192.168.10.10 -P8066
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.6.29-mycat-1.6.7.4-release-20200105164103 MyCat Server (OpenCloudDB)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use test;
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> desc emp;
ERROR 1184 (HY000): java.net.ConnectException: 拒绝连接
mysql> select * from emp;
+----+------+--------+
| id | name | deptno |
+----+------+--------+
|  1 | haha |     20 |
|  2 | xixi |     20 |
|  3 | ohoh |     20 |
|  4 | lala |     20 |
|  5 | yeye |     20 |
+----+------+--------+
5 rows in set (0.00 sec)

测试结果:由于主库挂了,故不再对主库进行写操作,为保持主从数据库的一致性,从库上也不能进行写操作,但是还可以进行读操作。

Supongo que te gusta

Origin blog.csdn.net/weixin_36522099/article/details/108724983
Recomendado
Clasificación