本文介绍用 MySQL Shell 搭建 MGR 的详细过程。

1、使用前,关掉防火墙,包括 selinux,firewalld,或者 MySQL 企业版的firewall(如果用了企业版的话)

2、两台机器:(4 台 MySQL 实例)
  1. 192.168.2.219 centos-ytt57-1 3311/3312

  2. 192.168.2.229 centos-ytt57-2 3311/3312

3、安装 MySQL(两台都装), MySQL Shell(任意一台), mysqlrouter(任意一台,官方建议和应用程序装在一个服务器上)
  1. yum install mysql-community-server mysql-shell mysql-router-community

4、搭建 InnoDB-Cluster(两台都装)

1. 配置文件如下:
  1. shell>vi /etc/my.cnf

  2. master-info-repository=table

  3. relay-log-info-repository=table

  4. gtid_mode=ON

  5. enforce_gtid_consistency=ON

  6. binlog_checksum=NONE

  7. log_slave_updates=ON

  8. binlog_format=ROW

  9. transaction_write_set_extraction=XXHASH64

2. 系统 HOSTS 配置(两台都配)

  1. shell>vi /etc/hosts


  2. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

  3. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

  4. 192.168.2.219 centos-ytt57-2

  5. 192.168.2.229 centos-ytt57-3

用 MySQL coalesce 函数确认下 report-host 是否设置正确
  1. (root@localhost) : [(none)] >SELECT coalesce(@@report_host, @@hostname) as r;

  2. +----------------+

  3. | r |

  4. +----------------+

  5. | centos-ytt57-1 |

  6. +----------------+

  7. 1 row in set (0.00 sec)

3. 创建管理员用户搭建 GROUP REPLICATION (四个节点都要)
  1. create user root identified by 'Root@123';

  2. grant all on *.* to root with grant option;

  3. flush privileges;

4. MySQLsh 连接其中一台节点:
  1. [root@centos-ytt57-1 mysql]# mysqlsh root@localhost:3321

5. 检查配置正确性:(如果这里不显示 OK,把对应的参数加到配置文件重启 MySQL 再次检查) dba.checkInstanceConfiguration(“root@centos-ytt57-2:3311”); dba.checkInstanceConfiguration(“root@centos-ytt57-2:3312”); dba.checkInstanceConfiguration(“root@centos-ytt57-3:3311”); dba.checkInstanceConfiguration(“root@centos-ytt57-3:3312”);

mysqlsh 执行检测
  1. [root@centos-ytt57-1 mysql]# mysqlsh --log-level=8 root@localhost:3311


  2. MySQL localhost:3311 ssl JS > dba.checkInstanceConfiguration("root@centos-ytt57-2:3311")

  3. {

  4. "status": "ok"

  5. }

6. 创建集群,节点 1 上创建。(结果显示 Cluster successfully created 表示成功)
  1. MySQL localhost:3311 ssl JS > var cluster = dba.createCluster('ytt_mgr');


  2. Cluster successfully created. Use Cluster.addInstance() to add MySQL instances.

  3. At least 3 instances are needed for the cluster to be able to withstand up to

  4. one server failure.

7. 添加节点 2,3,4(全部显示 OK,表示添加成功)

  1. MySQL localhost:3311 ssl JS > cluster.addInstance('root@centos-ytt57-2:3312');

  2. MySQL localhost:3311 ssl JS > cluster.addInstance('root@centos-ytt57-3:3311');

  3. MySQL localhost:3311 ssl JS > cluster.addInstance('root@centos-ytt57-3:3312');

8. 查看拓扑图:(describe 简单信息,status 详细描述)

  1. MySQL localhost:3311 ssl JS > cluster.describe()

  2. {

  3. "clusterName": "ytt_mgr",

  4. "defaultReplicaSet": {

  5. "name": "default",

  6. "topology": [

  7. {

  8. "address": "centos-ytt57-2:3311",

  9. "label": "centos-ytt57-2:3311",

  10. "role": "HA",

  11. "version": "8.0.17"

  12. },

  13. {

  14. "address": "centos-ytt57-2:3312",

  15. "label": "centos-ytt57-2:3312",

  16. "role": "HA",

  17. "version": "8.0.17"

  18. },

  19. {

  20. "address": "centos-ytt57-3:3311",

  21. "label": "centos-ytt57-3:3311",

  22. "role": "HA",

  23. "version": "8.0.17"

  24. },

  25. {

  26. "address": "centos-ytt57-3:3312",

  27. "label": "centos-ytt57-3:3312",

  28. "role": "HA",

  29. "version": "8.0.17"

  30. }

  31. ],

  32. "topologyMode": "Single-Primary"

  33. }

  34. }


  35. MySQL localhost:3311 ssl JS > cluster.status()


  36. "clusterName": "ytt_mgr",

  37. "defaultReplicaSet": {

  38. "name": "default",

  39. "primary": "centos-ytt57-2:3311",

  40. "ssl": "REQUIRED",

  41. "status": "OK",

  42. "statusText": "Cluster is ONLINE and can tolerate up to ONE failure.",

  43. "topology": {

  44. "centos-ytt57-2:3311": {

  45. "address": "centos-ytt57-2:3311",

  46. "mode": "R/W",

  47. "readReplicas": {},

  48. "role": "HA",

  49. "status": "ONLINE",

  50. "version": "8.0.17"

  51. },

  52. "centos-ytt57-2:3312": {

  53. "address": "centos-ytt57-2:3312",

  54. "mode": "R/O",

  55. "readReplicas": {},

  56. "role": "HA",

  57. "status": "ONLINE",

  58. "version": "8.0.17"

  59. },

  60. "centos-ytt57-3:3311": {

  61. "address": "centos-ytt57-3:3311",

  62. "mode": "R/O",

  63. "readReplicas": {},

  64. "role": "HA",

  65. "status": "ONLINE",

  66. "version": "8.0.17"

  67. },

  68. "centos-ytt57-3:3312": {

  69. "address": "centos-ytt57-3:3312",

  70. "mode": "R/O",

  71. "readReplicas": {},

  72. "role": "HA",

  73. "status": "ONLINE",

  74. "version": "8.0.17"

  75. }

  76. },

  77. "topologyMode": "Single-Primary"

  78. },

  79. "groupInformationSourceMember": "centos-ytt57-2:3311"

9. 简单测试下数据是否同步
  1. (root@localhost) : [(none)] >create database ytt;

  2. Query OK, 1 row affected (0.03 sec)


  3. (root@localhost) : [(none)] >use ytt;

  4. Database changed

  5. (root@localhost) : [ytt] >create table p1(id int primary key, log_time datetime);

  6. Query OK, 0 rows affected (0.08 sec)


  7. (root@localhost) : [ytt] >insert into p1 values (1,now());

  8. Query OK, 1 row affected (0.04 sec)


  9. (root@localhost) : [ytt] >show master status;

  10. +---------------+----------+--------------+------------------+-------------------------------------------+

  11. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

  12. +---------------+----------+--------------+------------------+-------------------------------------------+

  13. | mysql0.000001 | 25496 | | | 6c7bb9db-b759-11e9-a9c0-0800276cf0fc:1-41 |

  14. +---------------+----------+--------------+------------------+-------------------------------------------+

  15. 1 row in set (0.00 sec)

查看其他三个节点
  1. (root@localhost) : [ytt] >show tables;

  2. +---------------+

  3. | Tables_in_ytt |

  4. +---------------+

  5. | p1 |

  6. +---------------+

  7. 1 row in set (0.00 sec)


  8. (root@localhost) : [ytt] >select * from p1;

  9. +----+---------------------+

  10. | id | log_time |

  11. +----+---------------------+

  12. | 1 | 2019-08-05 16:44:20 |

  13. +----+---------------------+

  14. 1 row in set (0.00 sec)

停掉主节点:
  1. [root@centos-ytt57-2 mysql0]# systemctl stop mysqld@0

现在查看,主节点已经变为本机 3312节点
  1. "centos-ytt57-2:3312": {

  2. "address": "centos-ytt57-2:3312",

  3. "mode": "R/W",

  4. "readReplicas": {},

  5. "role": "HA",

  6. "status": "ONLINE"

  7. }

10. 报错处理

错误日志里显示
  1. 2019-08-05T09:01:35.125591Z 0 [ERROR] Plugin group_replication reported: 'The group name option is mandatory'

  2. 2019-08-05T09:01:35.125622Z 0 [ERROR] Plugin group_replication reported: 'Unable to start Group Replication on boot'

同时用 cluster.rescan() 扫描,发现
  1. The instance 'centos-ytt57-2:3311' is no longer part of the ReplicaSet.

重新加入此节点到集群:
  1. cluster.rejoinInstance('centos-ytt57-2:3311')

再次执行cluster.status()查看集群状态:”status”: “OK”,
11. 移除和加入
  1. cluster.removeInstance("centos-ytt57-3:3312");

  2. The instance 'centos-ytt57-3:3312' was successfully removed from the cluster.

  3. cluster.addInstance("centos-ytt57-3:3312");

  4. The instance 'centos-ytt57-3:3312' was successfully added to the cluster.

12. 用 mysqlrouter 生成连接 MGR 相关信息。
涉及到两个用户:
–user=mysqlrouter 是使用mysqlrouter的系统用户
自动创建的MySQL 用户是用来与MGR通信的用户。如果想查看这个用户的用户名以及密码,就加上
–force-password-validation,不过一般也没有必要查看。
  1. [root@centos-ytt57-2 ytt]# mysqlrouter --bootstrap root@centos-ytt57-2:3311 --user=mysqlrouter --force-password-validation --report-host centos-ytt57-2

  2. Please enter MySQL password for root:


  3. # Reconfiguring system MySQL Router instance...


  4. - Checking for old Router accounts

  5. - Found old Router accounts, removing

  6. - Creating mysql account mysql_router1_rdr89tx20r0a@'%' for cluster management

  7. - Storing account in keyring

  8. - Adjusting permissions of generated files

  9. - Creating configuration /etc/mysqlrouter/mysqlrouter.conf


  10. # MySQL Router configured for the InnoDB cluster 'ytt_mgr'


  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. the cluster 'ytt_mgr' can be reached by connecting to:


  18. ## MySQL Classic protocol


  19. - Read/Write Connections: centos-ytt57-2:6446

  20. - Read/Only Connections: centos-ytt57-2:6447


  21. ## MySQL X protocol


  22. - Read/Write Connections: centos-ytt57-2:64460

  23. - Read/Only Connections: centos-ytt57-2:64470

13. 启动 mysqlrouter,生效对应服务。

  1. [root@centos-ytt57-2 mysqlrouter]# systemctl start mysqlrouter

14. 使用 ROUTER 连接 MySQL

创建一个普通用户使用 ROUTER (四个节点都建立,具体权限看个人)

  1. [root@centos-ytt57-2 mysqlrouter]# /home/ytt/enter_mysql 80

  2. mysql: [Warning] Using a password on the command line interface can be insecure.

  3. Welcome to the MySQL monitor. Commands end with ; or \g.

  4. Your MySQL connection id is 1779

  5. Server version: 8.0.17 MySQL Community Server - GPL


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


  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. (root@centos-ytt57-2) : [(none)] >create user ytt_mysqlrouter;

  12. Query OK, 0 rows affected (0.12 sec)


  13. (root@centos-ytt57-2) : [(none)] >alter user ytt_mysqlrouter identified by 'mysql_router';

  14. Query OK, 0 rows affected (0.09 sec)


  15. (root@centos-ytt57-2) : [(none)] >grant all on ytt.* to ytt_mysqlrouter;

  16. Query OK, 0 rows affected (0.05 sec)


  17. (root@centos-ytt57-2) : [(none)] >flush privileges;

  18. Query OK, 0 rows affected (0.07 sec)

14.1 写入端口连接:
  1. [root@centos-ytt57-2 mysqlrouter]# mysql -uytt_mysqlrouter -pmysql_router -hcentos-ytt57-2 -P6446

  2. mysql: [Warning] Using a password on the command line interface can be insecure.

  3. Welcome to the MySQL monitor. Commands end with ; or \g.

  4. Your MySQL connection id is 2030

  5. Server version: 8.0.17 MySQL Community Server - GPL


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


  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. (ytt_mysqlrouter@centos-ytt57-2) : [(none)] >use ytt

  12. Reading table information for completion of table and column names

  13. You can turn off this feature to get a quicker startup with -A


  14. Database changed

  15. (ytt_mysqlrouter@centos-ytt57-2) : [ytt] >insert into p1 values (200,now());

  16. Query OK, 1 row affected (0.04 sec)


  17. (ytt_mysqlrouter@centos-ytt57-2) : [ytt] >select * from p1;

  18. +-----+---------------------+

  19. | id | log_time |

  20. +-----+---------------------+

  21. | 1 | 2019-08-05 23:15:38 |

  22. | 2 | 2019-08-05 23:15:42 |

  23. | 100 | 2019-08-05 23:52:58 |

  24. | 200 | 2019-08-05 23:56:23 |

  25. +-----+---------------------+

  26. 4 rows in set (0.00 sec)


  27. (ytt_mysqlrouter@centos-ytt57-2) : [ytt] >\q

  28. Bye

  29. [root@centos-ytt57-2 mysqlrouter]#

14.2 读取端口连接:(默认循环选择读服务节点)
  1. [root@centos-ytt57-2 mysqlrouter]# mysql -uytt_mysqlrouter -pmysql_router -hcentos-ytt57-2 -P6447

  2. mysql: [Warning] Using a password on the command line interface can be insecure.

  3. Welcome to the MySQL monitor. Commands end with ; or \g.

  4. Your MySQL connection id is 472

  5. Server version: 8.0.17 MySQL Community Server - GPL


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


  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. (ytt_mysqlrouter@centos-ytt57-2) : [(none)] >use ytt

  12. Reading table information for completion of table and column names

  13. You can turn off this feature to get a quicker startup with -A


  14. Database changed

  15. (ytt_mysqlrouter@centos-ytt57-2) : [ytt] >insert into p1 values (300,now());

  16. ERROR 1290 (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement

  17. (ytt_mysqlrouter@centos-ytt57-2) : [ytt] >select * from p1;

  18. +-----+---------------------+

  19. | id | log_time |

  20. +-----+---------------------+

  21. | 1 | 2019-08-05 23:15:38 |

  22. | 2 | 2019-08-05 23:15:42 |

  23. | 100 | 2019-08-05 23:52:58 |

  24. | 200 | 2019-08-05 23:56:23 |

  25. +-----+---------------------+

  26. 4 rows in set (0.00 sec)


  27. ## (ytt_mysqlrouter@centos-ytt57-2) : [ytt] >\s


  28. mysql Ver 8.0.17 for Linux on x86_64 (MySQL Community Server - GPL)


  29. Connection id: 472

  30. Current database: ytt

  31. Current user: ytt_mysqlrouter@centos-ytt57-2

  32. SSL: Cipher in use is DHE-RSA-AES128-GCM-SHA256

  33. Current pager: stdout

  34. Using outfile: ''

  35. Using delimiter: ;

  36. Server version: 8.0.17 MySQL Community Server - GPL

  37. Protocol version: 10

  38. Connection: centos-ytt57-2 via TCP/IP

  39. Server characterset: utf8mb4

  40. Db characterset: utf8mb4

  41. Client characterset: utf8mb4

  42. Conn. characterset: utf8mb4

  43. TCP port: 6447

  44. Uptime: 45 min 10 sec


  45. ## Threads: 5 Questions: 188 Slow queries: 0 Opens: 257 Flush tables: 3 Open tables: 161 Queries per second avg: 0.069




  46. (ytt_mysqlrouter@centos-ytt57-2) : [ytt] >select @@server_id;

  47. +-------------+

  48. | @@server_id |

  49. +-------------+

  50. | 2 |

  51. +-------------+

  52. 1 row in set (0.01 sec)


  53. (ytt_mysqlrouter@centos-ytt57-2) : [ytt] >\q

  54. Bye

15. 机器全挂了,重启 MGR

  1. MySQL localhost:3311 ssl JS > dba.rebootClusterFromCompleteOutage()

  2. Reconfiguring the default cluster from complete outage...


  3. The instance 'centos-ytt57-2:3312' was part of the cluster configuration.

  4. Would you like to rejoin it to the cluster? [y/N]: y


  5. The instance 'centos-ytt57-3:3311' was part of the cluster configuration.

  6. Would you like to rejoin it to the cluster? [y/N]: y


  7. The instance 'centos-ytt57-3:3312' was part of the cluster configuration.

  8. Would you like to rejoin it to the cluster? [y/N]: y


  9. The safest and most convenient way to provision a new instance is through

  10. automatic clone provisioning, which will completely overwrite the state of

  11. 'localhost:3311' with a physical snapshot from an existing cluster member. To

  12. use this method by default, set the 'recoveryMethod' option to 'clone'.


  13. The incremental distributed state recovery may be safely used if you are sure

  14. all updates ever executed in the cluster were done with GTIDs enabled, there

  15. are no purged transactions and the new instance contains the same GTID set as

  16. the cluster or a subset of it. To use this method by default, set the

  17. 'recoveryMethod' option to 'incremental'.


  18. Incremental distributed state recovery was selected because it seems to be safely usable.


  19. ^C

  20. The cluster was successfully rebooted.


  21. Script execution interrupted by user.

  22. MySQL localhost:3311 ssl JS > var cluster = dba.getCluster('ytt_mgr');

  23. MySQL localhost:3311 ssl JS > cluste.status();

  24. ReferenceError: cluste is not defined

  25. MySQL localhost:3311 ssl JS > cluster.status();

  26. {

  27. "clusterName": "ytt_mgr",

  28. "defaultReplicaSet": {

  29. "name": "default",

  30. "primary": "centos-ytt57-2:3311",

  31. "ssl": "REQUIRED",

  32. "status": "OK",

  33. "statusText": "Cluster is ONLINE and can tolerate up to ONE failure.",

  34. "topology": {

  35. "centos-ytt57-2:3311": {

  36. "address": "centos-ytt57-2:3311",

  37. "mode": "R/W",

  38. "readReplicas": {},

  39. "role": "HA",

  40. "status": "ONLINE",

  41. "version": "8.0.17"

  42. },

  43. "centos-ytt57-2:3312": {

  44. "address": "centos-ytt57-2:3312",

  45. "mode": "R/O",

  46. "readReplicas": {},

  47. "role": "HA",

  48. "status": "ONLINE",

  49. "version": "8.0.17"

  50. },

  51. "centos-ytt57-3:3311": {

  52. "address": "centos-ytt57-3:3311",

  53. "mode": "R/O",

  54. "readReplicas": {},

  55. "role": "HA",

  56. "status": "ONLINE",

  57. "version": "8.0.17"

  58. },

  59. "centos-ytt57-3:3312": {

  60. "address": "centos-ytt57-3:3312",

  61. "mode": "R/O",

  62. "readReplicas": {},

  63. "role": "HA",

  64. "status": "ONLINE",

  65. "version": "8.0.17"

  66. }

  67. },

  68. "topologyMode": "Single-Primary"

  69. },

  70. "groupInformationSourceMember": "centos-ytt57-2:3311"

  71. }

社区近期动态