当前位置:   article > 正文

MySQL InnoDB Replication部署方案与实践_mysql innodb replicaset

mysql innodb replicaset

1. 概述

MySQL Innodb ReplicaSet 是 MySQL 团队在 2020 年推出的一款产品,用来帮助用户快速部署和管理主从复制,在数据库层仍然使用的是主从复制技术。

ReplicaSet 主要包含三个组件:MySQL Router、MySQL Server 以及 MySQL Shell 高级客户端。

MySQL Shell 负责管理 ReplicaSet 包括部署、切换、节点加入等,都可以通过内置 AdminAPI 自动化完成。

MySQL Router 是一款轻量级中间件,可在应用程序和 ReplicaSet 之间提供透明路由和读写分离功能。

2. 适用场景

InnoDB ReplicaSet至少由两台MySQL服务器实例组成,提供MySQL主从复制功能。InnoDB ReplicaSet基于异步的主从复制实现,因此适用于用户对高可用性要求不高的环境。可以通过MySQL Shell快速搭建及管理主从复制,避免了搭建主从复制时大量的手动操作。

InnoDB ReplicaSet的不足之处有:

  • 不支持自动故障转移。主库不可用时,需要通过AdminAPI手动发起故障转移。

  • 发生计划外的服务不可用时,可能会丢失部分数据。由于主备之间是异步复制,主库发生故障时,未提交的事务会丢失。

  • 发生计划外的服务不可用时,可能会产生数据不一致。例如由于网络原因导致主库连不上,将备库提升为主库后,可能会同时存在两个主库,即发生“脑裂”。

  • 不支持多主模式,即同一时刻只有一个主库可写。

  • 读扩展受限,不能像组复制那样对流量进行控制。

  • 所有的备库都从同一个主库复制数据。在有大量的小更新时,可能会对主库造成影响。

  • 仅支持MySQL 8.0及其以后的版本。

  • 仅支持基于GTID的日志复制。

  • 仅支持基于行的日志复制(Row-Based Replication, RBR),不支持基于SQL语句的复制(Statement-Based Replication, SBR)。

  • 不支持复制过滤。

  • RS为一个主库加多个从库的架构。需要通过MySQL Router监视RS中的实例,因此从库的数量不能无限制增加。

  • 必须通过MySQL Shell配置和管理,包括复制用户的创建。

3. 安装部署

IProlehostname版本OS
192.168.10.31Mastermysql-rs-1MySQL 8.2Centos_7.9_x86_x64
192.168.10.32Secondarymysql-rs-2MySQL 8.2Centos_7.9_x86_x64

3.1. 安装MySQL servers

分别在两个MySQL服务器下载MySQL Server安装包,并进行安装。

  1. # 配置主机名
  2. echo "192.168.10.31   mysql-rs-1" >> /etc/hosts
  3. echo "192.168.10.32   mysql-rs-2" >> /etc/hosts
  4. # 192.168.10.31执行
  5. hostnamectl set-hostname mysql-rs-1
  6. bash
  7. # 192.168.10.32执行
  8. hostnamectl set-hostname mysql-rs-2
  9. bash
  10. # 下载rpm安装包
  11. wget https://cdn.mysql.com//Downloads/MySQL-8.2/mysql-8.2.0-1.el7.x86_64.rpm-bundle.tar
  12. # 解压
  13. tar -xvf mysql-8.2.0-1.el7.x86_64.rpm-bundle.tar
  14. # 卸载mariadb
  15. rpm -e mariadb-libs-5.5.68-1.el7.x86_64 --nodeps
  16. # 安装MySQL
  17. yum localinstall -y *.rpm

分别配置root密码:

  1. [root@mysql-rs-2 server]# mysql -uroot -p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 8
  5. Server version: 8.2.0
  6. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. mysql>
  12. mysql>
  13. mysql> alter user root@'localhost' identified by 'Abcd@1234';
  14. Query OK, 0 rows affected (0.00 sec)
  15. mysql> create user root@'%' identified by 'Abcd@1234';
  16. Query OK, 0 rows affected (0.01 sec)

3.2. 安装mysqlsh

  1. # 下载mysqlshell
  2. wget https://cdn.mysql.com//Downloads/MySQL-Shell/mysql-shell-8.2.0-1.el7.x86_64.rpm
  3. # 安装mysqlshell
  4. rpm -ivh mysql-shell-8.2.0-1.el7.x86_64.rpm

验证mysql shell链接数据库

  1. [root@mysql-rs-1 software]# mysqlsh --mysql -u root -h localhost -C
  2. Please provide the password for 'root@localhost': ************
  3. Save password for 'root@localhost'? [Y]es/[N]o/Ne[v]er (default No):
  4. Error during auto-completion cache update: You must reset your password using ALTER USER statement before executing this statement.
  5. MySQL Shell 8.2.0
  6. Copyright (c) 2016, 2023, Oracle and/or its affiliates.
  7. Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
  8. Other names may be trademarks of their respective owners.
  9. Type '\help' or '\?' for help; '\quit' to exit.
  10. Creating a Classic session to 'root@localhost?compression=REQUIRED'
  11. Your MySQL connection id is 8
  12. No default schema selected; type \use <schema> to set one.
  13. MySQL localhost JS >

3.3. 配置实例

在其中一个主机上启动mysqlsh连接

  1. [root@mysql-rs-1 software]# mysqlsh root@mysql-rs-1:3306
  2. MySQL Shell 8.2.0
  3. Copyright (c) 2016, 2023, Oracle and/or its affiliates.
  4. Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
  5. Other names may be trademarks of their respective owners.
  6. Type '\help' or '\?' for help; '\quit' to exit.
  7. Creating a session to 'root@mysql-rs-1:3306'
  8. Fetching schema names for auto-completion... Press ^C to stop.
  9. Your MySQL connection id is 21
  10. Server version: 8.2.0 MySQL Community Server - GPL
  11. No default schema selected; type \use <schema> to set one.

分别给root用户授权:

  1. GRANT CLONE_ADMIN, CONNECTION_ADMIN, CREATE USER, EXECUTE, FILE, GROUP_REPLICATION_ADMIN, PERSIST_RO_VARIABLES_ADMIN, PROCESS, RELOAD, REPLICATION CLIENT, REPLICATION SLAVE, REPLICATION_APPLIER, REPLICATION_SLAVE_ADMIN, ROLE_ADMIN, SELECT, SHUTDOWN, SYSTEM_VARIABLES_ADMIN ON *.* TO 'root'@'%' WITH GRANT OPTION;
  2. GRANT DELETE, INSERT, UPDATE ON mysql.* TO 'root'@'%' WITH GRANT OPTION;
  3. GRANT ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE TEMPORARY TABLES, CREATE VIEW, DELETE, DROP, EVENT, EXECUTE, INDEX, INSERT, LOCK TABLES, REFERENCES, SHOW VIEW, TRIGGER, UPDATE ON mysql_innodb_cluster_metadata.* TO 'root'@'%' WITH GRANT OPTION;
  4. GRANT ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE TEMPORARY TABLES, CREATE VIEW, DELETE, DROP, EVENT, EXECUTE, INDEX, INSERT, LOCK TABLES, REFERENCES, SHOW VIEW, TRIGGER, UPDATE ON mysql_innodb_cluster_metadata_bkp.* TO 'root'@'%' WITH GRANT OPTION;
  5. GRANT ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE TEMPORARY TABLES, CREATE VIEW, DELETE, DROP, EVENT, EXECUTE, INDEX, INSERT, LOCK TABLES, REFERENCES, SHOW VIEW, TRIGGER, UPDATE ON mysql_innodb_cluster_metadata_previous.* TO 'root'@'%' WITH GRANT OPTION;

在MySQL Shell中执行:

配置mysql-rs-1:

  1. MySQL mysql-rs-1:3306 ssl JS > dba.configureReplicaSetInstance('root@mysql-rs-1:3306', {clusterAdmin: "rs_admin"})
  2. Configuring local MySQL instance listening at port 3306 for use in an InnoDB ReplicaSet...
  3. This instance reports its own address as mysql-rs-1:3306
  4. Clients and other cluster members will communicate with it through this address by default. If this is not correct, the report_host MySQL system variable should be changed.
  5. Assuming full account name 'rs_admin'@'%' for rs_admin
  6. Password for new account: *********
  7. Confirm password: *********
  8. applierWorkerThreads will be set to the default value of 4.
  9. NOTE: Some configuration options need to be fixed:
  10. +----------------------------------------+---------------+----------------+--------------------------------------------------+
  11. | Variable                               | Current Value | Required Value | Note                                             |
  12. +----------------------------------------+---------------+----------------+--------------------------------------------------+
  13. | binlog_transaction_dependency_tracking | COMMIT_ORDER | WRITESET       | Update the server variable                       |
  14. | enforce_gtid_consistency               | OFF           | ON             | Update read-only variable and restart the server |
  15. | gtid_mode                             | OFF           | ON             | Update read-only variable and restart the server |
  16. | server_id                             | 1             | <unique ID>   | Update read-only variable and restart the server |
  17. +----------------------------------------+---------------+----------------+--------------------------------------------------+
  18. Some variables need to be changed, but cannot be done dynamically on the server.
  19. Do you want to perform the required configuration changes? [y/n]: y
  20. Do you want to restart the instance after configuring it? [y/n]: y
  21. Creating user rs_admin@%.
  22. Account rs_admin@% was successfully created.
  23. Configuring instance...
  24. WARNING: '@@binlog_transaction_dependency_tracking' is deprecated and will be removed in a future release. (Code 1287).
  25. The instance 'mysql-rs-1:3306' was configured to be used in an InnoDB ReplicaSet.
  26. Restarting MySQL...
  27. NOTE: MySQL server at mysql-rs-1:3306 was restarted.

配置mysql-rs-2:

  1. MySQL mysql-rs-1:3306 ssl JS > dba.configureReplicaSetInstance('root@mysql-rs-2:3306', {clusterAdmin: "rs_admin"})
  2. Please provide the password for 'root@mysql-rs-2:3306': *********
  3. Save password for 'root@mysql-rs-2:3306'? [Y]es/[N]o/Ne[v]er (default No): Y
  4. Configuring MySQL instance at mysql-rs-2:3306 for use in an InnoDB ReplicaSet...
  5. This instance reports its own address as mysql-rs-2:3306
  6. Clients and other cluster members will communicate with it through this address by default. If this is not correct, the report_host MySQL system variable should be changed.
  7. Assuming full account name 'rs_admin'@'%' for rs_admin
  8. Password for new account: *********
  9. Confirm password: *********
  10. applierWorkerThreads will be set to the default value of 4.
  11. NOTE: Some configuration options need to be fixed:
  12. +----------------------------------------+---------------+----------------+--------------------------------------------------+
  13. | Variable                               | Current Value | Required Value | Note                                             |
  14. +----------------------------------------+---------------+----------------+--------------------------------------------------+
  15. | binlog_transaction_dependency_tracking | COMMIT_ORDER | WRITESET       | Update the server variable                       |
  16. | enforce_gtid_consistency               | OFF           | ON             | Update read-only variable and restart the server |
  17. | gtid_mode                             | OFF           | ON             | Update read-only variable and restart the server |
  18. | server_id                             | 1             | <unique ID>   | Update read-only variable and restart the server |
  19. +----------------------------------------+---------------+----------------+--------------------------------------------------+
  20. Some variables need to be changed, but cannot be done dynamically on the server.
  21. Do you want to perform the required configuration changes? [y/n]: y
  22. Do you want to restart the instance after configuring it? [y/n]: y
  23. Creating user rs_admin@%.
  24. Account rs_admin@% was successfully created.
  25. Configuring instance...
  26. WARNING: '@@binlog_transaction_dependency_tracking' is deprecated and will be removed in a future release. (Code 1287).
  27. The instance 'mysql-rs-2:3306' was configured to be used in an InnoDB ReplicaSet.
  28. Restarting MySQL...
  29. NOTE: MySQL server at mysql-rs-2:3306 was restarted.

3.4. 创建ReplicationSet

创建ReplicationSet,默认会将当前进入的实例为主库实例:

  1. MySQL mysql-rs-1:3306 ssl JS > dba.createReplicaSet('ReplicaSet')
  2. A new replicaset with instance 'mysql-rs-1:3306' will be created.
  3. * Checking MySQL instance at mysql-rs-1:3306
  4. This instance reports its own address as mysql-rs-1:3306
  5. mysql-rs-1:3306: Instance configuration is suitable.
  6. * Checking connectivity and SSL configuration...
  7. * Updating metadata...
  8. ReplicaSet object successfully created for mysql-rs-1:3306.
  9. Use rs.addInstance() to add more asynchronously replicated instances to this replicaset and rs.status() to check its status.
  10. <ReplicaSet:ReplicaSet>

3.5. 将另一个实例加入到创建的 ReplicaSet

  1. MySQL mysql-rs-1:3306 ssl JS > var rs = dba.getReplicaSet()
  2. You are connected to a member of replicaset 'ReplicaSet'.
  3. MySQL mysql-rs-1:3306 ssl JS > rs.addInstance('mysql-rs-2:3306')
  4. Adding instance to the replicaset...
  5. * Performing validation checks
  6. This instance reports its own address as mysql-rs-2:3306
  7. mysql-rs-2:3306: Instance configuration is suitable.
  8. * Checking async replication topology...
  9. * Checking connectivity and SSL configuration...
  10. * Checking transaction state of the instance...
  11. NOTE: The target instance 'mysql-rs-2:3306' has not been pre-provisioned (GTID set is empty). The Shell is unable to decide whether replication can completely recover its state.
  12. The safest and most convenient way to provision a new instance is through automatic clone provisioning, which will completely overwrite the state of 'mysql-rs-2:3306' with a physical snapshot from an existing replicaset member. To use this method by default, set the 'recoveryMethod' option to 'clone'.
  13. WARNING: It should be safe to rely on replication to incrementally recover the state of the new instance if you are sure all updates ever executed in the replicaset were done with GTIDs enabled, there are no purged transactions and the new instance contains the same GTID set as the replicaset or a subset of it. To use this method by default, set the 'recoveryMethod' option to 'incremental'.
  14. Please select a recovery method [C]lone/[I]ncremental recovery/[A]bort (default Clone):
  15. * Updating topology
  16. Monitoring Clone based state recovery of the new member. Press ^C to abort the operation.
  17. Clone based state recovery is now in progress.
  18. NOTE: A server restart is expected to happen as part of the clone process. If the
  19. server does not support the RESTART command or does not come back after a
  20. while, you may need to manually start it back.
  21. * Waiting for clone to finish...
  22. NOTE: mysql-rs-2:3306 is being cloned from mysql-rs-1:3306
  23. ** Stage DROP DATA: Completed
  24. ** Clone Transfer  
  25.   FILE COPY  ############################################################ 100% Completed
  26.   PAGE COPY  ############################################################ 100% Completed
  27.   REDO COPY  ############################################################ 100% Completed
  28. * Clone process has finished: 76.80 MB transferred in about 1 second (~76.80 MB/s)
  29. ** Changing replication source of mysql-rs-2:3306 to mysql-rs-1:3306
  30. ** Waiting for new instance to synchronize with PRIMARY...
  31. ** Transactions replicated  ############################################################ 100%
  32. The instance 'mysql-rs-2:3306' was added to the replicaset and is replicating from mysql-rs-1:3306.
  33. * Waiting for instance 'mysql-rs-2:3306' to synchronize the Metadata updates with the PRIMARY...
  34. ** Transactions replicated  ############################################################ 100%

3.6. 查看副本集状态

  1. MySQL mysql-rs-1:3306 ssl JS > var rs = dba.getReplicaSet()
  2. You are connected to a member of replicaset 'ReplicaSet'.
  3. MySQL mysql-rs-1:3306 ssl JS > rs.status()
  4. {
  5.    "replicaSet": {
  6.        "name": "ReplicaSet",
  7.        "primary": "mysql-rs-1:3306",
  8.        "status": "AVAILABLE",
  9.        "statusText": "All instances available.",
  10.        "topology": {
  11.            "mysql-rs-1:3306": {
  12.                "address": "mysql-rs-1:3306",
  13.                "instanceRole": "PRIMARY",
  14.                "mode": "R/W",
  15.                "status": "ONLINE"
  16.           },
  17.            "mysql-rs-2:3306": {
  18.                "address": "mysql-rs-2:3306",
  19.                "instanceRole": "SECONDARY",
  20.                "mode": "R/O",
  21.                "replication": {
  22.                    "applierStatus": "APPLIED_ALL",
  23.                    "applierThreadState": "Waiting for an event from Coordinator",
  24.                    "applierWorkerThreads": 4,
  25.                    "receiverStatus": "ON",
  26.                    "receiverThreadState": "Waiting for source to send event",
  27.                    "replicationLag": null,
  28.                    "replicationSsl": "ECDHE-RSA-AES128-GCM-SHA256 TLSv1.2",
  29.                    "replicationSslMode": "REQUIRED"
  30.               },
  31.                "status": "ONLINE"
  32.           }
  33.       },
  34.        "type": "ASYNC"
  35.   }
  36. }

3.7. 配置MySQL Router

  1. # 下载MySQL Router
  2. wget https://cdn.mysql.com//Downloads/MySQL-Router/mysql-router-community-8.2.0-1.el7.x86_64.rpm
  3. # 安装
  4. rpm -ivh mysql-router-community-8.2.0-1.el7.x86_64.rpm
  5. MySQL mysql-rs-1:3306 ssl JS > var rs = dba.getReplicaSet()
  6. You are connected to a member of replicaset 'ReplicaSet'.
  7. MySQL mysql-rs-1:3306 ssl JS > rs.setupRouterAccount('rs_router1')
  8. Missing the password for new account rs_router1@%. Please provide one.
  9. Password for new account: *********
  10. Confirm password: *********
  11. Creating user rs_router1@%.
  12. Account rs_router1@% was successfully created.

3.8. 引导启动MySQL Router

  1. [root@mysql-rs-1 software]# mysqlrouter --bootstrap root@mysql-rs-1:3306 --user mysqlrouter
  2. Please enter MySQL password for root:
  3. # Bootstrapping system MySQL Router 8.2.0 (MySQL Community - GPL) instance...
  4. - Creating account(s) (only those that are needed, if any)
  5. - Verifying account (using it to run SQL queries that would be run by Router)
  6. - Storing account in keyring
  7. - Adjusting permissions of generated files
  8. - Creating configuration /etc/mysqlrouter/mysqlrouter.conf
  9. Existing configuration backed up to '/etc/mysqlrouter/mysqlrouter.conf.bak'
  10. # MySQL Router configured for the InnoDB ReplicaSet 'ReplicaSet'
  11. After this MySQL Router has been started with the generated configuration
  12.    $ /etc/init.d/mysqlrouter restart
  13. or
  14.    $ systemctl start mysqlrouter
  15. or
  16.    $ mysqlrouter -c /etc/mysqlrouter/mysqlrouter.conf
  17. InnoDB ReplicaSet 'ReplicaSet' can be reached by connecting to:
  18. ## MySQL Classic protocol
  19. - Read/Write Connections: localhost:6446
  20. - Read/Only Connections: localhost:6447
  21. - Read/Write Split Connections: localhost:6450
  22. ## MySQL X protocol
  23. - Read/Write Connections: localhost:6448
  24. - Read/Only Connections: localhost:6449

3.9. 读写分离验证

检查ReplicaSet,可以看到主节点(读写)为mysql-rs-1:3306,从节点(只读)为mysql-rs-2:3306

  1. MySQL mysql-rs-1:3306 ssl JS > rs.status()
  2. {
  3.    "replicaSet": {
  4.        "name": "ReplicaSet",
  5.        "primary": "mysql-rs-1:3306",
  6.        "status": "AVAILABLE",
  7.        "statusText": "All instances available.",
  8.        "topology": {
  9.            "mysql-rs-1:3306": {
  10.                "address": "mysql-rs-1:3306",
  11.                "instanceRole": "PRIMARY",
  12.                "mode": "R/W",
  13.                "status": "ONLINE"
  14.           },
  15.            "mysql-rs-2:3306": {
  16.                "address": "mysql-rs-2:3306",
  17.                "instanceRole": "SECONDARY",
  18.                "mode": "R/O",
  19.                "replication": {
  20.                    "applierStatus": "APPLIED_ALL",
  21.                    "applierThreadState": "Waiting for an event from Coordinator",
  22.                    "applierWorkerThreads": 4,
  23.                    "receiverStatus": "ON",
  24.                    "receiverThreadState": "Waiting for source to send event",
  25.                    "replicationLag": null,
  26.                    "replicationSsl": "ECDHE-RSA-AES128-GCM-SHA256 TLSv1.2",
  27.                    "replicationSslMode": "REQUIRED"
  28.               },
  29.                "status": "ONLINE"
  30.           }
  31.       },
  32.        "type": "ASYNC"
  33.   }
  34. }

检查mysqlrouter,可以看到MySQL Router读写端口为6450

  1. MySQL mysql-rs-1:3306 ssl JS > rs.listRouters()
  2. {
  3.    "replicaSetName": "ReplicaSet",
  4.    "routers": {
  5.        "mysql-rs-1::system": {
  6.            "hostname": "mysql-rs-1",
  7.            "lastCheckIn": "2023-11-14 11:40:21",
  8.            "roPort": "6447",
  9.            "roXPort": "6449",
  10.            "rwPort": "6446",
  11.            "rwSplitPort": "6450",
  12.            "rwXPort": "6448",
  13.            "version": "8.2.0"
  14.       }
  15.   }
  16. }

下面通过MySQL Router的读写端口6450连接mysql数据库:

  1. [root@mysql-rs-1 ~]# mysqlsh mysql://root@mysql-rs-1:6450 --sql
  2. MySQL Shell 8.2.0
  3. Copyright (c) 2016, 2023, Oracle and/or its affiliates.
  4. Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
  5. Other names may be trademarks of their respective owners.
  6. Type '\help' or '\?' for help; '\quit' to exit.
  7. Creating a Classic session to 'root@mysql-rs-1:6450'
  8. Fetching global names for auto-completion... Press ^C to stop.
  9. Your MySQL connection id is 0
  10. Server version: 8.2.0 MySQL Community Server - GPL
  11. No default schema selected; type \use <schema> to set one.
  12. MySQL mysql-rs-1:6450 ssl SQL >

查询数据,发现当前节点为从节点:

  1. MySQL mysql-rs-1:6450 ssl SQL > select @@hostname, @@port;
  2. +------------+--------+
  3. | @@hostname | @@port |
  4. +------------+--------+
  5. | mysql-rs-2 |   3306 |
  6. +------------+--------+
  7. 1 row in set (0.0035 sec)
  8. Statement ID: 13700

开启事务,会自动切换到主节点:

  1. MySQL mysql-rs-1:6450 ssl SQL > start transaction;
  2. Query OK, 0 rows affected (0.0031 sec)
  3. Statement ID: 20477
  4. MySQL mysql-rs-1:6450 ssl ★ SQL > select @@hostname, @@port;
  5. +------------+--------+
  6. | @@hostname | @@port |
  7. +------------+--------+
  8. | mysql-rs-1 |   3306 |
  9. +------------+--------+
  10. 1 row in set (0.0007 sec)
  11. Statement ID: 20563

提交或者回滚事务后,自动切换到从节点:

  1. MySQL mysql-rs-1:6450 ssl ★ SQL > rollback;
  2. Query OK, 0 rows affected (0.0006 sec)
  3. Statement ID: 21909
  4. MySQL mysql-rs-1:6450 ssl SQL > select @@hostname, @@port;
  5. +------------+--------+
  6. | @@hostname | @@port |
  7. +------------+--------+
  8. | mysql-rs-2 |   3306 |
  9. +------------+--------+
  10. 1 row in set (0.0020 sec)
  11. Statement ID: 15489

只读事务中,也会切换到从节点:

  1. MySQL mysql-rs-1:6450 ssl SQL > start transaction read only;
  2. Query OK, 0 rows affected (0.0029 sec)
  3. Statement ID: 16970
  4. MySQL mysql-rs-1:6450 ssl ☆ SQL > select @@hostname, @@port;
  5. +------------+--------+
  6. | @@hostname | @@port |
  7. +------------+--------+
  8. | mysql-rs-2 |   3306 |
  9. +------------+--------+
  10. 1 row in set (0.0010 sec)
  11. Statement ID: 16996

用户还可以在会话中使用ROUTER SET access_mode=命令来定义应用要连接的实例类型:

  1. MySQL mysql-rs-1:6450 ssl SQL > router set access_mode='read_write';
  2. Query OK, 0 rows affected (0.0004 sec)
  3. MySQL mysql-rs-1:6450 ssl  - SQL > select @@hostname, @@port;
  4. +------------+--------+
  5. | @@hostname | @@port |
  6. +------------+--------+
  7. | mysql-rs-1 |   3306 |
  8. +------------+--------+
  9. 1 row in set (0.0017 sec)
  10. Statement ID: 25535
  11. MySQL mysql-rs-1:6450 ssl SQL > router set access_mode='read_only';
  12. Query OK, 0 rows affected (0.0006 sec)
  13. MySQL mysql-rs-1:6450 ssl  - SQL > select @@hostname, @@port;
  14. +------------+--------+
  15. | @@hostname | @@port |
  16. +------------+--------+
  17. | mysql-rs-2 |   3306 |
  18. +------------+--------+
  19. 1 row in set (0.0043 sec)
  20. Statement ID: 18508

4. 总结

总之,MySQL Router 8.2支持读写分离。这是一项有价值的功能,可以优化数据库性能和可扩展性,而无需在应用程序中进行任何更改。这种配置使您能够将所有读流量引导到只读实例,将所有写流量引导到读写实例。这不仅增强了用户的整体体验,还简化了数据库管理和部署。

读写实例是主实例或源实例。只读实例是副本(InnoDB集群的非主实例、ReplicaSet的非主实例或复制集群中的非主实例)。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/1006212
推荐阅读
相关标签
  

闽ICP备14008679号