生产环境部署InnoDB集群
当在生产环境中工作时,组成InnoDB集群的MySQL服务器实例在多台主机上作为网络的一部分运行,而不是在单台机器上。在执行这些步骤之前 ,用户必须为每台作为服务器实例添加到集群中的机器安装所需的软件。
与沙盒部署不同的是,所有的实例都部署在本地的一台机器上,在生产部署中,你必须连接到每台机器并在本地运行MySQL Shell,然后在实例 上执行dba.configureLocalInstance()。这确保了任何配置更改都持久化到实例的mysql.cfg文件中。这还要求用户有访问服务器的权限和执 行MySQL Shell所需的权限。
用户权限
用于管理实例的用户帐户不一定是root帐户,但是除了MySQL管理员的权限(SUPER、GRANT OPTION、CREATE、DROP等)之外,还需要给用户分配 InnoDB集群元数据表的完全读写权限。给用户ic权限来管理InnoDB集群问题:
mysql> create user 'ic'@'%' identified by "123456"; Query OK, 0 rows affected (1.31 sec) mysql> GRANT ALL PRIVILEGES ON mysql_innodb_cluster_metadata.* TO ic@'%' WITH GRANT OPTION; Query OK, 0 rows affected (0.07 sec) mysql> GRANT RELOAD, SHUTDOWN, PROCESS, FILE, SUPER, REPLICATION SLAVE, REPLICATION CLIENT,CREATE USER ON *.* TO ic@'%' WITH GRANT OPTION; Query OK, 0 rows affected (1.00 sec) mysql> GRANT SELECT ON performance_schema.* TO ic@'%' WITH GRANT OPTION; Query OK, 0 rows affected (0.00 sec) mysql> GRANT SELECT ON *.* TO ic@'%' WITH GRANT OPTION; Query OK, 0 rows affected (0.00 sec) mysql> GRANT SELECT, INSERT, UPDATE, DELETE ON mysql.* TO ic@'%' WITH GRANT OPTION; Query OK, 0 rows affected (0.00 sec) mysql> GRANT SELECT, INSERT, UPDATE, DELETE ON mysql.* TO ic@'%' WITH GRANT OPTION; Query OK, 0 rows affected (0.00 sec)
如果只需要读操作(例如用于监控),则可以使用具有更多限制权限的帐户。给用户your_user权限来监控InnoDB集群问题:
mysql> GRANT SELECT ON performance_schema.global_status TO ic@'%'; Query OK, 0 rows affected (0.08 sec) mysql> GRANT SELECT ON performance_schema.replication_applier_configuration TO ic@'%'; Query OK, 0 rows affected (0.00 sec) mysql> GRANT SELECT ON performance_schema.replication_applier_status TO ic@'%'; Query OK, 0 rows affected (0.01 sec) mysql> GRANT SELECT ON performance_schema.replication_applier_status_by_coordinator TO ic@'%'; Query OK, 0 rows affected (0.00 sec) mysql> GRANT SELECT ON performance_schema.replication_applier_status_by_worker TO ic@'%'; Query OK, 0 rows affected (0.00 sec) mysql> GRANT SELECT ON performance_schema.replication_connection_configuration TO ic@'%'; Query OK, 0 rows affected (0.00 sec) mysql> GRANT SELECT ON performance_schema.replication_connection_status TO ic@'%'; Query OK, 0 rows affected (0.01 sec) mysql> GRANT SELECT ON performance_schema.replication_group_member_stats TO ic@'%'; Query OK, 0 rows affected (0.00 sec) mysql> GRANT SELECT ON performance_schema.replication_group_members TO ic@'%'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.07 sec)
对于每个实例重复上面的操作
配置主机名
组成集群的生产实例在不同的机器上运行,因此每台机器必须有唯一的主机名,并且能够解析集群中运行服务器实例的其他机器的主机名。如果 不是这样,你可以:
.配置每台机器,将每台机器的IP映射到主机名。详情请参阅操作系统文档。这是推荐的解决方案。
.设置DNS服务
.在每个实例的MySQL配置中配置report_host变量
下面对三台服务器进行配置
[root@localhost ~]# hostnamectl set-hostname mic1 [root@mic1 ~]# vi /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 10.18.13.238 mic3 10.18.13.237 mic2 10.18.13.236 mic1 [root@localhost ~]# hostnamectl set-hostname mic2 [root@mic2 ~]# vi /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 10.18.13.238 mic3 10.18.13.237 mic2 10.18.13.236 mic1 [root@localhost mysql]# hostnamectl set-hostname mic3 [root@localhost mysql]# Last login: Fri Mar 28 09:43:16 2025 from 10.18.13.242 [root@mic3 ~]# vi /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 10.18.13.238 mic3 10.18.13.237 mic2 10.18.13.236 mic1 [root@mic1 ~]# ping mic3 PING mic3 (10.18.13.238) 56(84) bytes of data. 64 bytes from mic3 (10.18.13.238): icmp_seq=1 ttl=64 time=0.830 ms 64 bytes from mic3 (10.18.13.238): icmp_seq=2 ttl=64 time=0.418 ms ^C --- mic3 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1000ms rtt min/avg/max/mdev = 0.418/0.624/0.830/0.206 ms [root@mic1 ~]# ping mic2 PING mic2 (10.18.13.237) 56(84) bytes of data. 64 bytes from mic2 (10.18.13.237): icmp_seq=1 ttl=64 time=0.473 ms 64 bytes from mic2 (10.18.13.237): icmp_seq=2 ttl=64 time=0.307 ms 64 bytes from mic2 (10.18.13.237): icmp_seq=3 ttl=64 time=0.300 ms ^C --- mic2 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2000ms rtt min/avg/max/mdev = 0.300/0.360/0.473/0.079 ms [root@mic1 ~]# ping mic1 PING mic1 (10.18.13.236) 56(84) bytes of data. 64 bytes from mic1 (10.18.13.236): icmp_seq=1 ttl=64 time=0.097 ms 64 bytes from mic1 (10.18.13.236): icmp_seq=2 ttl=64 time=0.062 ms ^C --- mic1 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 999ms rtt min/avg/max/mdev = 0.062/0.079/0.097/0.019 ms [mysql@mic1 mysql]$ vi my.cnf report_host=10.18.13.236 [mysql@mic2 mysql]$ vi my.cnf report_host=10.18.13.237 [mysql@mic3 mysql]$ vi my.cnf report_host=10.18.13.238 [root@mic1 /]# service mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL.. SUCCESS! [root@mic2 /]# service mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL.. SUCCESS! [root@mic3 /]# service mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL.. SUCCESS!
详细日志
当使用生产部署时,为MySQL Shell配置详细的日志记录是很有用的,日志中的信息可以帮助您发现和解决在准备服务器实例作为InnoDB集群的 一部分时可能发生的任何问题。要用详细的日志级别启动MySQL Shell,使用–log-level选项:
[root@mic1 /]# mysqlsh --log-level=DEBUG3 MySQL Shell 8.0.41 Copyright (c) 2016, 2025, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type '\help' or '\?' for help; '\quit' to exit. MySQL JS >
建议使用DEBUG3,更多信息请参见–log-level。当设置了DEBUG3时,MySQL Shell日志文件包含这样的行:Debug: execute_sql(…)其中包含 作为每个AdminAPI调用的一部分执行的SQL查询。MySQL Shell生成的日志文件在unix系统中位于~/.mysqlsh/mysqlsh.log;在Microsoft Windows系统中,它位于%APPDATA%\MySQL\mysqlsh\mysqlsh.log中。
除了启用MySQL Shell日志级别之外,您还可以配置AdminAPI在MySQL Shell中每次调用API后提供的输出数量。要启用AdminAPI输出的数量,请 在MySQL Shell中发出:
MySQL JS > dba.verbose=2 2 MySQL JS >
这将启用AdminAPI调用的最大输出。可用的输出级别如下:
.默认值为0或OFF。这提供了最小输出,在不进行故障排除时是推荐的级别。
.1或ON添加每次对AdminAPI调用的详细输出。
.2在详细输出中添加调试输出,提供每次调用AdminAPI执行的全部信息。
检查实例配置
在服务器实例创建生产部署之前,需要使用dba.checkInstanceConfiguration()函数检查每个实例上的MySQL是否正确配置。下面演示了如何 在MySQL Shell中执行这个命令:
MySQL JS > dba.checkInstanceConfiguration('ic@mic1:3306') Please provide the password for 'ic@mic1:3306': ****** Save password for 'ic@mic1:3306'? [Y]es/[N]o/Ne[v]er (default No): WARNING: Support for AdminAPI operations in MySQL version 5.7 is deprecated and will be removed in a future release of MySQL Shell Validating local MySQL instance listening at port 3306 for use in an InnoDB cluster... ERROR: The account 'ic'@'%' is missing privileges required to manage an InnoDB cluster: 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 'ic'@'%' WITH GRANT OPTION; 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 'ic'@'%' WITH GRANT OPTION; For more information, see the online documentation. Dba.checkInstanceConfiguration: The account 'ic'@'%' is missing privileges required to manage an InnoDB cluster. (RuntimeError)
上面提示缺少相关权限,在三个实例上执行上面的授权语句:
mysql> 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 'ic'@'%' WITH GRANT OPTION; Query OK, 0 rows affected (0.00 sec) mysql> 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 'ic'@'%' WITH GRANT OPTION; Query OK, 0 rows affected (0.00 sec)
授权后再次执行检测实例的命令
MySQL JS > dba.checkInstanceConfiguration('ic@mic1:3306') Please provide the password for 'ic@mic1:3306': ****** Save password for 'ic@mic1:3306'? [Y]es/[N]o/Ne[v]er (default No): WARNING: Support for AdminAPI operations in MySQL version 5.7 is deprecated and will be removed in a future release of MySQL Shell Validating local MySQL instance listening at port 3306 for use in an InnoDB cluster... This instance reports its own address as 10.18.13.236:3306 Checking whether existing tables comply with Group Replication requirements... ERROR: The following tables do not have a Primary Key or equivalent column: jycs.t_cs Group Replication requires tables to use InnoDB and have a PRIMARY KEY or PRIMARY KEY Equivalent (non-null unique key). Tables that do not follow these requirements will be readable but not updateable when used with Group Replication. If your applications make updates (INSERT, UPDATE or DELETE) to these tables, ensure they use the InnoDB storage engine and have a PRIMARY KEY or PRIMARY KEY Equivalent. If you can't change the tables structure to include an extra visible key to be used as PRIMARY KEY, you can make use of the INVISIBLE COLUMN feature available since 8.0.23: https://dev.mysql.com/doc/refman/8.0/en/invisible-columns.html Checking instance configuration... NOTE: Some configuration options need to be fixed: +----------------------------------+---------------+----------------+------------------------------------------------+ | Variable | Current Value | Required Value | Note | +----------------------------------+---------------+----------------+------------------------------------------------+ | binlog_checksum | CRC32 | NONE | Update the server variable and the config file | | binlog_format | MIXED | ROW | Update the server variable and the config file | | enforce_gtid_consistency | OFF | ON | Update the config file and restart the server | | gtid_mode | OFF | ON | Update the config file and restart the server | | log_slave_updates | OFF | ON | Update the config file and restart the server | | transaction_write_set_extraction | OFF | XXHASH64 | Update the config file and restart the server | +----------------------------------+---------------+----------------+------------------------------------------------+ Some variables need to be changed, but cannot be done dynamically on the server: an option file is required. NOTE: Please use the dba.configureInstance() command to repair these issues. { "config_errors": [ { "action": "server_update+config_update", "current": "CRC32", "option": "binlog_checksum", "required": "NONE" }, { "action": "server_update+config_update", "current": "MIXED", "option": "binlog_format", "required": "ROW" }, { "action": "config_update+restart", "current": "OFF", "option": "enforce_gtid_consistency", "required": "ON" }, { "action": "config_update+restart", "current": "OFF", "option": "gtid_mode", "required": "ON" }, { "action": "config_update+restart", "current": "OFF", "option": "log_slave_updates", "required": "ON" }, { "action": "config_update+restart", "current": "OFF", "option": "transaction_write_set_extraction", "required": "XXHASH64" } ], "status": "error" } MySQL JS >
检查实例给出了要修改的参数,下面对每个实例修改相关参数
[mysql@mic1 ~]$ vi /mysqlsoft/mysql/my.cnf binlog_checksum=NONE binlog_format = row enforce_gtid_consistency=ON gtid_mode=ON log_slave_updates=ON transaction_write_set_extraction=XXHASH64 [mysql@mic2 ~]$ vi /mysqlsoft/mysql/my.cnf binlog_checksum=NONE binlog_format = row enforce_gtid_consistency=ON gtid_mode=ON log_slave_updates=ON transaction_write_set_extraction=XXHASH64 [mysql@mic3 ~]$ vi /mysqlsoft/mysql/my.cnf binlog_checksum=NONE binlog_format = row enforce_gtid_consistency=ON gtid_mode=ON log_slave_updates=ON transaction_write_set_extraction=XXHASH64 [root@mic1 ~]# service mysqld restart Shutting down MySQL. SUCCESS! Starting MySQL.. SUCCESS! [root@mic2 ~]# service mysqld restart Shutting down MySQL. SUCCESS! Starting MySQL.. SUCCESS! [root@mic3 ~]# service mysqld restart Shutting down MySQL. SUCCESS! Starting MySQL.. SUCCESS!
再次执行检测实例命令
MySQL JS > dba.checkInstanceConfiguration('ic@mic1:3306') Please provide the password for 'ic@mic1:3306': ****** Save password for 'ic@mic1:3306'? [Y]es/[N]o/Ne[v]er (default No): WARNING: Support for AdminAPI operations in MySQL version 5.7 is deprecated and will be removed in a future release of MySQL Shell Validating local MySQL instance listening at port 3306 for use in an InnoDB cluster... This instance reports its own address as 10.18.13.236:3306 Checking whether existing tables comply with Group Replication requirements... ERROR: The following tables do not have a Primary Key or equivalent column: jycs.t_cs Group Replication requires tables to use InnoDB and have a PRIMARY KEY or PRIMARY KEY Equivalent (non-null unique key). Tables that do not follow these requirements will be readable but not updateable when used with Group Replication. If your applications make updates (INSERT, UPDATE or DELETE) to these tables, ensure they use the InnoDB storage engine and have a PRIMARY KEY or PRIMARY KEY Equivalent. If you can't change the tables structure to include an extra visible key to be used as PRIMARY KEY, you can make use of the INVISIBLE COLUMN feature available since 8.0.23: https://dev.mysql.com/doc/refman/8.0/en/invisible-columns.html Checking instance configuration... { "status": "error" }
上面提示是说jycs.t_cs表没有主键,下面对jycs._t_cs表添加主键
mysql> ALTER TABLE jycs.t_cs MODIFY COLUMN id INT PRIMARY KEY; Query OK, 0 rows affected (1.06 sec) Records: 0 Duplicates: 0 Warnings: 0
再次执行检测实例命令
MySQL JS > dba.checkInstanceConfiguration('ic@mic1:3306') Please provide the password for 'ic@mic1:3306': ****** Save password for 'ic@mic1:3306'? [Y]es/[N]o/Ne[v]er (default No): WARNING: Support for AdminAPI operations in MySQL version 5.7 is deprecated and will be removed in a future release of MySQL Shell Validating local MySQL instance listening at port 3306 for use in an InnoDB cluster... This instance reports its own address as 10.18.13.236:3306 Checking whether existing tables comply with Group Replication requirements... No incompatible tables detected Checking instance configuration... Instance configuration is compatible with InnoDB cluster The instance '10.18.13.236:3306' is valid to be used in an InnoDB cluster. { "status": "ok" }
检测通过,下面对另外两个实例执行检测命令
[root@mic2 ~]# mysqlsh --log-level=DEBUG3 MySQL Shell 8.0.41 Copyright (c) 2016, 2025, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type '\help' or '\?' for help; '\quit' to exit. MySQL JS > dba.checkInstanceConfiguration('ic@mic2:3306') Please provide the password for 'ic@mic2:3306': ****** Save password for 'ic@mic2:3306'? [Y]es/[N]o/Ne[v]er (default No): WARNING: Support for AdminAPI operations in MySQL version 5.7 is deprecated and will be removed in a future release of MySQL Shell Validating local MySQL instance listening at port 3306 for use in an InnoDB cluster... This instance reports its own address as 10.18.13.237:3306 Checking whether existing tables comply with Group Replication requirements... No incompatible tables detected Checking instance configuration... Instance configuration is compatible with InnoDB cluster The instance '10.18.13.237:3306' is valid to be used in an InnoDB cluster. { "status": "ok" } [root@mic3 ~]# mysqlsh --log-level=DEBUG3 MySQL Shell 8.0.41 Copyright (c) 2016, 2025, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type '\help' or '\?' for help; '\quit' to exit. MySQL JS > dba.checkInstanceConfiguration('ic@mic3:3306') Please provide the password for 'ic@mic3:3306': ****** Save password for 'ic@mic3:3306'? [Y]es/[N]o/Ne[v]er (default No): WARNING: Support for AdminAPI operations in MySQL version 5.7 is deprecated and will be removed in a future release of MySQL Shell Validating local MySQL instance listening at port 3306 for use in an InnoDB cluster... This instance reports its own address as 10.18.13.238:3306 Checking whether existing tables comply with Group Replication requirements... No incompatible tables detected Checking instance configuration... Instance configuration is compatible with InnoDB cluster The instance '10.18.13.238:3306' is valid to be used in an InnoDB cluster. { "status": "ok" }
检测全部通过。
配置实例
如果在针对实例运行dba.checkInstanceConfiguration()生成的报告中发现了配置问题,请连接到计算机并重新配置服务器实例。AdminAPI提 供了dba.configureLocalInstance()函数来查找MySQL服务器的配置文件并修改它,以确保该实例是正确配置InnoDB集群的。或者,根据报告 中的信息手动更改实例的选项文件。无论您以何种方式更改配置,都可能需要重新启动MySQL以确保检测到配置更改。
推荐的方法是登录远程机器,以root用户运行MySQL Shell,然后连接到本地MySQL服务器:
[root@mic1 ~]# mysqlsh --log-level=DEBUG3
dba.configureLocalInstance()方法验证一个适合集群使用的用户,该用户用于集群成员之间的连接。默认情况下,root用户不能进行远程登 录,因此您有三个选项来继续配置:为root用户启用远程连接、创建新用户或以上两个选项都不使用。下面的示例演示了第二个选项,即为集群 创建一个新用户。
MySQL JS > dba.configureLocalInstance('ic@mic1:3306') WARNING: This function is deprecated and will be removed in a future release of MySQL Shell, use dba.configureInstance() instead. Please provide the password for 'ic@mic1:3306': ****** Save password for 'ic@mic1:3306'? [Y]es/[N]o/Ne[v]er (default No): WARNING: Support for AdminAPI operations in MySQL version 5.7 is deprecated and will be removed in a future release of MySQL Shell Configuring local MySQL instance listening at port 3306 for use in an InnoDB cluster... This instance reports its own address as 10.18.13.236:3306 The instance '10.18.13.236:3306' is valid to be used in an InnoDB cluster. The instance '10.18.13.236:3306' is already ready to be used in an InnoDB cluster. MySQL JS > dba.configureLocalInstance('ic@mic2:3306') WARNING: This function is deprecated and will be removed in a future release of MySQL Shell, use dba.configureInstance() instead. Please provide the password for 'ic@mic2:3306': ****** Save password for 'ic@mic2:3306'? [Y]es/[N]o/Ne[v]er (default No): WARNING: Support for AdminAPI operations in MySQL version 5.7 is deprecated and will be removed in a future release of MySQL Shell Configuring local MySQL instance listening at port 3306 for use in an InnoDB cluster... This instance reports its own address as 10.18.13.237:3306 The instance '10.18.13.237:3306' is valid to be used in an InnoDB cluster. The instance '10.18.13.237:3306' is already ready to be used in an InnoDB cluster. MySQL JS > dba.configureLocalInstance('ic@mic3:3306') WARNING: This function is deprecated and will be removed in a future release of MySQL Shell, use dba.configureInstance() instead. Please provide the password for 'ic@mic3:3306': ****** Save password for 'ic@mic3:3306'? [Y]es/[N]o/Ne[v]er (default No): WARNING: Support for AdminAPI operations in MySQL version 5.7 is deprecated and will be removed in a future release of MySQL Shell Configuring local MySQL instance listening at port 3306 for use in an InnoDB cluster... This instance reports its own address as 10.18.13.238:3306 The instance '10.18.13.238:3306' is valid to be used in an InnoDB cluster. The instance '10.18.13.238:3306' is already ready to be used in an InnoDB cluster.
创建群集
准备好实例后,请使用dba。createCluster函数,用于创建集群。运行MySQL Shell的机器被用作集群的种子实例。seed实例会被复制到你添加 到集群中的其他实例上,使它们成为seed实例的副本。登录实例并在本地运行MySQL Shell。
[root@mic1 /]# mysqlsh --uri ic@mic1:3306 Please provide the password for 'ic@mic1:3306': ****** Save password for 'ic@mic1:3306'? [Y]es/[N]o/Ne[v]er (default No): MySQL Shell 8.0.41 Copyright (c) 2016, 2025, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type '\help' or '\?' for help; '\quit' to exit. Creating a session to 'ic@mic1:3306' Fetching schema names for auto-completion... Press ^C to stop. Your MySQL connection id is 8 Server version: 5.7.26-log Source distribution No default schema selected; type \useto set one. MySQL mic1:3306 JS >
使用dba.createCluster(name)函数创建集群,并将返回的集群分配给一个名为cluster的变量:
MySQL mic1:3306 JS > var cluster = dba.createCluster('prodCluster') WARNING: Support for AdminAPI operations in MySQL version 5.7 is deprecated and will be removed in a future release of MySQL Shell A new InnoDB Cluster will be created on instance '10.18.13.236:3306'. Validating instance configuration at mic1:3306... This instance reports its own address as 10.18.13.236:3306 Instance configuration is suitable. NOTE: TLS not available at '10.18.13.236:3306', assuming memberSslMode to be DISABLED NOTE: Group Replication will communicate with other members using '10.18.13.236:33061'. Use the localAddress option to override. * Checking connectivity and SSL configuration... WARNING: Instance '10.18.13.236:3306' cannot persist Group Replication configuration since MySQL version 5.7.26 does not support the SET PERSIST command (MySQL version >= 8.0.11 required). Please use the dba.configureLocalInstance() command locally to persist the changes. Creating InnoDB Cluster 'prodCluster' on '10.18.13.236:3306'... Adding Seed Instance... Cluster successfully created. Use Cluster.addInstance() to add MySQL instances. At least 3 instances are needed for the cluster to be able to withstand up to one server failure. MySQL mic1:3306 JS >
如果遇到与元数据不可访问相关的错误,可能是配置了环回网络接口。为了保证InnoDB集群的正确使用,请关闭环回接口。
要检查集群是否已创建,请使用集群实例的status()函数
MySQL mic1:3306 JS > cluster.status() WARNING: Support for AdminAPI operations in MySQL version 5.7 is deprecated and will be removed in a future release of MySQL Shell { "clusterName": "prodCluster", "defaultReplicaSet": { "name": "default", "primary": "10.18.13.236:3306", "ssl": "DISABLED", "status": "OK_NO_TOLERANCE", "statusText": "Cluster is NOT tolerant to any failures.", "topology": { "10.18.13.236:3306": { "address": "10.18.13.236:3306", "memberRole": "PRIMARY", "mode": "R/W", "readReplicas": {}, "role": "HA", "status": "ONLINE", "version": "5.7.26" } }, "topologyMode": "Single-Primary" }, "groupInformationSourceMember": "10.18.13.236:3306" } MySQL mic1:3306 JS >
一旦服务器实例属于一个集群,重要的是只使用MySQL Shell和AdminAPI管理它们。不支持在实例添加到集群后手动更改组复制的配置。类似地 ,不支持在AdminAPI配置实例后修改对InnoDB集群至关重要的服务器变量,如server_uuid。
使用cluster. addinstance (instance)函数向集群添加更多实例,其中instance是连接到本地实例的URI类型字符串。这些实例必须已经配置 为集群使用。集群中至少需要3个实例才能容忍其中一个实例的故障。添加更多的实例可以提高对实例失败的容忍度。添加一个实例到集群问题 :
MySQL mic1:3306 JS > cluster.addInstance('ic@mic2:3306'); WARNING: Support for AdminAPI operations in MySQL version 5.7 is deprecated and will be removed in a future release of MySQL Shell ERROR: Unable to connect to the target instance 'mic2:3306'. Please verify the connection settings, make sure the instance is available and try again. Cluster.addInstance: Could not open connection to 'mic2:3306': Can't connect to MySQL server on 'mic2:3306' (113) (MySQL Error 2003) MySQL mic1:3306 JS > cluster.addInstance('ic@mic2:3306'); WARNING: Support for AdminAPI operations in MySQL version 5.7 is deprecated and will be removed in a future release of MySQL Shell The safest and most convenient way to provision a new instance is through automatic clone provisioning, which will completely overwrite the state of '10.18.13.237:3306' with a physical snapshot from an existing cluster member. To use this method by default, set the 'recoveryMethod' option to 'clone'. The incremental state recovery may be safely used if you are sure all updates ever executed in the cluster were done with GTIDs enabled, there are no purged transactions and the new instance contains the same GTID set as the cluster or a subset of it. To use this method by default, set the 'recoveryMethod' option to 'incremental'. Incremental state recovery was selected because it seems to be safely usable. Validating instance configuration at mic2:3306... This instance reports its own address as 10.18.13.237:3306 Instance configuration is suitable. ERROR: RuntimeError: Cannot add an instance with the same server UUID (684e1f7d-6f47-11ef-a6d5-005056a3a162) of an active member of the cluster '10.18.13.236:3306'. Please change the server UUID of the instance to add, all members must have a unique server UUID. Cluster.addInstance: Cannot add an instance with the same server UUID (684e1f7d-6f47-11ef-a6d5-005056a3a162) of an active member of the cluster '10.18.13.236:3306'. Please change the server UUID of the instance to add, all members must have a unique server UUID. (RuntimeError)
在${data_dir}/auto.cnf中的server-uuid值是一样的,要处理该问题只需要删除auto.cnf文件,就会自动创建和生成新的server-uuid值。
[root@mic2 mysql]# cat auto.cnf [auto] server-uuid=684e1f7d-6f47-11ef-a6d5-005056a3a162 [root@mic2 mysql]# rm -rf auto.cnf [root@mic2 mysql]# service mysqld restart Shutting down MySQL.... SUCCESS! Starting MySQL.. SUCCESS! [root@mic2 mysql]# cat auto.cnf [auto] server-uuid=c9f76f9e-0de1-11f0-9924-005056b9de82 [root@mic3 mysql]# rm -rf auto.cnf [root@mic3 mysql]# service mysqld restart Shutting down MySQL.... SUCCESS! Starting MySQL.. SUCCESS! [root@mic3 mysql]# cat auto.cnf [auto] server-uuid=ccdd7fe7-0de1-11f0-9436-005056b9890f MySQL mic1:3306 JS > cluster.addInstance('ic@mic2:3306'); WARNING: Support for AdminAPI operations in MySQL version 5.7 is deprecated and will be removed in a future release of MySQL Shell The safest and most convenient way to provision a new instance is through automatic clone provisioning, which will completely overwrite the state of '10.18.13.237:3306' with a physical snapshot from an existing cluster member. To use this method by default, set the 'recoveryMethod' option to 'clone'. The incremental state recovery may be safely used if you are sure all updates ever executed in the cluster were done with GTIDs enabled, there are no purged transactions and the new instance contains the same GTID set as the cluster or a subset of it. To use this method by default, set the 'recoveryMethod' option to 'incremental'. Incremental state recovery was selected because it seems to be safely usable. Validating instance configuration at mic2:3306... This instance reports its own address as 10.18.13.237:3306 Instance configuration is suitable. NOTE: Group Replication will communicate with other members using '10.18.13.237:33061'. Use the localAddress option to override. * Checking connectivity and SSL configuration... A new instance will be added to the InnoDB Cluster. Depending on the amount of data on the cluster this might take from a few seconds to several hours. WARNING: Instance '10.18.13.237:3306' cannot persist Group Replication configuration since MySQL version 5.7.26 does not support the SET PERSIST command (MySQL version >= 8.0.11 required). Please use the dba.configureLocalInstance() command locally to persist the changes. Adding instance to the cluster... Monitoring recovery process of the new cluster member. Press ^C to stop monitoring and let it continue in background. Incremental state recovery is now in progress. * Waiting for distributed recovery to finish... NOTE: '10.18.13.237:3306' is being recovered from '10.18.13.236:3306' * Distributed recovery has finished WARNING: Instance '10.18.13.236:3306' cannot persist configuration since MySQL version 5.7.26 does not support the SET PERSIST command (MySQL version >= 8.0.11 required). Please use the dba.configureLocalInstance() command locally to persist the changes. The instance '10.18.13.237:3306' was successfully added to the cluster. MySQL mic1:3306 JS > cluster.addInstance('ic@mic3:3306'); WARNING: Support for AdminAPI operations in MySQL version 5.7 is deprecated and will be removed in a future release of MySQL Shell The safest and most convenient way to provision a new instance is through automatic clone provisioning, which will completely overwrite the state of '10.18.13.238:3306' with a physical snapshot from an existing cluster member. To use this method by default, set the 'recoveryMethod' option to 'clone'. The incremental state recovery may be safely used if you are sure all updates ever executed in the cluster were done with GTIDs enabled, there are no purged transactions and the new instance contains the same GTID set as the cluster or a subset of it. To use this method by default, set the 'recoveryMethod' option to 'incremental'. Incremental state recovery was selected because it seems to be safely usable. Validating instance configuration at mic3:3306... This instance reports its own address as 10.18.13.238:3306 Instance configuration is suitable. NOTE: Group Replication will communicate with other members using '10.18.13.238:33061'. Use the localAddress option to override. * Checking connectivity and SSL configuration... A new instance will be added to the InnoDB Cluster. Depending on the amount of data on the cluster this might take from a few seconds to several hours. WARNING: Instance '10.18.13.238:3306' cannot persist Group Replication configuration since MySQL version 5.7.26 does not support the SET PERSIST command (MySQL version >= 8.0.11 required). Please use the dba.configureLocalInstance() command locally to persist the changes. Adding instance to the cluster... Monitoring recovery process of the new cluster member. Press ^C to stop monitoring and let it continue in background. Incremental state recovery is now in progress. * Waiting for distributed recovery to finish... NOTE: '10.18.13.238:3306' is being recovered from '10.18.13.236:3306' * Distributed recovery has finished WARNING: Instance '10.18.13.236:3306' cannot persist configuration since MySQL version 5.7.26 does not support the SET PERSIST command (MySQL version >= 8.0.11 required). Please use the dba.configureLocalInstance() command locally to persist the changes. WARNING: Instance '10.18.13.237:3306' cannot persist configuration since MySQL version 5.7.26 does not support the SET PERSIST command (MySQL version >= 8.0.11 required). Please use the dba.configureLocalInstance() command locally to persist the changes. The instance '10.18.13.238:3306' was successfully added to the cluster.
在这个阶段,服务器实例已经被添加到集群中,但是对InnoDB集群元数据的更改只在您当前连接的实例上进行。为了让集群中所有实例的配置更 改持久化,用户必须连接到每个实例,并在添加的每个实例上本地运行dba.configureLocalInstance()。这对于确保实例在离开集群时重新加 入集群是至关重要的。
要将InnoDB集群的元数据持久化到所有的实例中,请登录到你添加到集群中的每个实例,并在本地运行MySQL Shell。
MySQL mic1:3306 JS > dba.checkInstanceConfiguration('ic@mic1:3306') Please provide the password for 'ic@mic1:3306': ****** Save password for 'ic@mic1:3306'? [Y]es/[N]o/Ne[v]er (default No): WARNING: Support for AdminAPI operations in MySQL version 5.7 is deprecated and will be removed in a future release of MySQL Shell Validating local MySQL instance listening at port 3306 for use in an InnoDB cluster... This instance reports its own address as 10.18.13.236:3306 Checking whether existing tables comply with Group Replication requirements... No incompatible tables detected Checking instance configuration... Instance configuration is compatible with InnoDB cluster The instance '10.18.13.236:3306' is valid to be used in an InnoDB cluster. { "status": "ok" } MySQL mic1:3306 JS > [root@mic2 ~]# mysqlsh --log-level=DEBUG3 MySQL Shell 8.0.41 Copyright (c) 2016, 2025, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type '\help' or '\?' for help; '\quit' to exit. MySQL JS > dba.checkInstanceConfiguration('ic@mic2:3306') Please provide the password for 'ic@mic2:3306': ****** Save password for 'ic@mic2:3306'? [Y]es/[N]o/Ne[v]er (default No): WARNING: Support for AdminAPI operations in MySQL version 5.7 is deprecated and will be removed in a future release of MySQL Shell Validating local MySQL instance listening at port 3306 for use in an InnoDB cluster... This instance reports its own address as 10.18.13.237:3306 Checking whether existing tables comply with Group Replication requirements... No incompatible tables detected Checking instance configuration... Instance configuration is compatible with InnoDB cluster The instance '10.18.13.237:3306' is valid to be used in an InnoDB cluster. { "status": "ok" } [root@mic3 ~]# mysqlsh --log-level=DEBUG3 MySQL Shell 8.0.41 Copyright (c) 2016, 2025, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type '\help' or '\?' for help; '\quit' to exit. MySQL JS > dba.checkInstanceConfiguration('ic@mic3:3306') Please provide the password for 'ic@mic3:3306': ****** Save password for 'ic@mic3:3306'? [Y]es/[N]o/Ne[v]er (default No): WARNING: Support for AdminAPI operations in MySQL version 5.7 is deprecated and will be removed in a future release of MySQL Shell Validating local MySQL instance listening at port 3306 for use in an InnoDB cluster... This instance reports its own address as 10.18.13.238:3306 Checking whether existing tables comply with Group Replication requirements... No incompatible tables detected Checking instance configuration... Instance configuration is compatible with InnoDB cluster The instance '10.18.13.238:3306' is valid to be used in an InnoDB cluster. { "status": "ok" }
对添加到集群中的每个服务器实例重复此过程。类似地,如果你修改了集群的结构,例如改变了实例的数量,你需要对每个服务器实例重复这个 过程,以相应地更新集群中每个实例的InnoDB集群元数据。
要检查集群是否已创建,请使用集群实例的status()函数。
MySQL mic1:3306 JS > cluster.status() WARNING: Support for AdminAPI operations in MySQL version 5.7 is deprecated and will be removed in a future release of MySQL Shell { "clusterName": "prodCluster", "defaultReplicaSet": { "name": "default", "primary": "10.18.13.236:3306", "ssl": "DISABLED", "status": "OK", "statusText": "Cluster is ONLINE and can tolerate up to ONE failure.", "topology": { "10.18.13.236:3306": { "address": "10.18.13.236:3306", "memberRole": "PRIMARY", "mode": "R/W", "readReplicas": {}, "role": "HA", "status": "ONLINE", "version": "5.7.26" }, "10.18.13.237:3306": { "address": "10.18.13.237:3306", "memberRole": "SECONDARY", "mode": "R/O", "readReplicas": {}, "role": "HA", "status": "ONLINE", "version": "5.7.26" }, "10.18.13.238:3306": { "address": "10.18.13.238:3306", "memberRole": "SECONDARY", "mode": "R/O", "readReplicas": {}, "role": "HA", "status": "ONLINE", "version": "5.7.26" } }, "topologyMode": "Single-Primary" }, "groupInformationSourceMember": "10.18.13.236:3306" }
下面来测试一个集群的同步功能
在主库上向jycs库的t_cs表插入记录
mysql> insert into t_cs values(6,'hi'); Query OK, 1 row affected (0.00 sec) mysql> select * from t_cs; +----+------+ | id | name | +----+------+ | 1 | cs | | 2 | ab | | 3 | cd | | 4 | ef | | 5 | gh | | 6 | hi | +----+------+ 6 rows in set (0.00 sec)
查看第二、第三个实例是否已经同步
MySQL mic2:3306 JS > \sql Switching to SQL mode... Commands end with ; Fetching global names for auto-completion... Press ^C to stop. MySQL mic2:3306 SQL > select * from jycs.t_cs; +----+------+ | id | name | +----+------+ | 1 | cs | | 2 | ab | | 3 | cd | | 4 | ef | | 5 | gh | | 6 | hi | +----+------+ 6 rows in set (0.0006 sec) MySQL mic2:3306 SQL > MySQL mic3:3306 JS > \sql Switching to SQL mode... Commands end with ; Fetching global names for auto-completion... Press ^C to stop. MySQL mic3:3306 SQL > select * from jycs.t_cs; +----+------+ | id | name | +----+------+ | 1 | cs | | 2 | ab | | 3 | cd | | 4 | ef | | 5 | gh | | 6 | hi | +----+------+ 6 rows in set (0.0008 sec) MySQL mic3:3306 SQL >
至此使用MySQL Shell部署生产环境InnoDB集群完成。