当前位置:Gxlcms > 数据库问题 > MySQL InnoDB Cluser | Mysql 5.7 集群

MySQL InnoDB Cluser | Mysql 5.7 集群

时间:2021-07-01 10:21:17 帮助过:6人阅读

  • 目前 本集群 应用在自己的 django demo 环境上,暂时稳定运行。
  • 欢迎加群 620176501 讨论 Mysql 集群的应用。

核心架构

  1. <code>* MySQL 5.7 引入了 Group Replication 功能,可以在一组 MySQL 服务器之间实现自动主机选举,形成一主多从结构。经过高级配置后,可以实现多主多从结构。
  2. * MySQL Router 是一个轻量级透明中间件,可以自动获取上述集群的状态,规划 SQL 语句,分配到合理的 MySQL 后端进行执行。
  3. * MySQL Shell 是一个同时支持 JavaScript 和 SQL 的交互程序,可以快速配置 InnoDB Cluster。</code>

技术分享图片


部署

  • 本次共3台机器,设置主机名及hosts | 配置每台服务 my.cnf 中report_host 字段,为自己的 hostname

    1. <code>192.168.10.123 db1
    2. 192.168.10.124 db2
    3. 192.168.10.125 db3</code>
  • 安装 mysql5.7.20 ,可以参考下面的安装基本,我搭建的时候就是用的这个。

    1. <code>http://blog.51cto.com/hequan/1982428</code>
  • 安装mysql-shell mysql-route
  1. <code>wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
  2. yum install mysql57-community-release-el7-11.noarch.rpm
  3. yum install mysql-shell -y
  4. yum install mysql-router -y</code>
  • 设置相关用户的权限,生产环境 可以不是 root用户
  1. <code>grant all privileges on *.* to ‘root‘@‘%‘ identified by ‘123456‘;
  2. GRANT ALL PRIVILEGES ON mysql_innodb_cluster_metadata.* TO root@‘%‘ WITH GRANT OPTION;
  3. GRANT RELOAD, SHUTDOWN, PROCESS, FILE, SUPER, REPLICATION SLAVE, REPLICATION CLIENT, CREATE USER ON *.* TO root@‘%‘ WITH GRANT OPTION;
  4. GRANT SELECT ON *.* TO root@‘%‘ WITH GRANT OPTION;
  5. flush privileges;
  6. </code>

mysqlsh

  1. <code>[root@db1 ~]# mysqlsh
  2. ## 检查mysql 配置文件 (3台主机都要操作此步骤)
  3. dba.checkInstanceConfiguration(‘root@db1:3306‘)
  4. +----------------------------------+---------------+----------------+--------------------------------------------------+
  5. | Variable | Current Value | Required Value | Note |
  6. +----------------------------------+---------------+----------------+--------------------------------------------------+
  7. | binlog_checksum | CRC32 | NONE | Update the server variable or restart the server |
  8. | binlog_format | MIXED | ROW | Update the server variable or restart the server |
  9. | enforce_gtid_consistency | OFF | ON | Restart the server |
  10. | gtid_mode | OFF | ON | Restart the server |
  11. | log_slave_updates | 0 | ON | Restart the server |
  12. | master_info_repository | FILE | TABLE | Restart the server |
  13. | relay_log_info_repository | FILE | TABLE | Restart the server |
  14. | transaction_write_set_extraction | OFF | XXHASH64 | Restart the server |
  15. +----------------------------------+---------------+----------------+--------------------------------------------------+
  16. ## 修复mysql 配置文件, 必须用 root(3台主机都要操作此步骤)
  17. dba.configureLocalInstance(‘root@db1:3306‘)
  18. Please provide the password for ‘root@db1:3306‘:
  19. Detecting the configuration file...
  20. Found configuration file at standard location: /etc/my.cnf
  21. Do you want to modify this file? [Y|n]: [Y|n]: Y
  22. ## 重启mysql
  23. ## 重新检查 (3台主机都要操作此步骤)
  24. dba.checkInstanceConfiguration(‘root@db1:3306‘)
  25. Please provide the password for ‘root@db1:3306‘:
  26. Validating instance...
  27. The instance ‘db1:3306‘ is valid for Cluster usage
  28. {
  29. "status": "ok"
  30. }
  31. </code>
  1. <code>## 登陆
  2. mysqlsh --uri root@db1:3306
  3. ## 创建集群 main
  4. mysql-js> var cluster = dba.createCluster(‘main‘)
  5. A new InnoDB cluster will be created on instance ‘hequan@db1:3306‘.
  6. Creating InnoDB cluster ‘main‘ on ‘hequan@db1:3306‘...
  7. Adding Seed Instance...
  8. Cluster successfully created. Use Cluster.addInstance() to add MySQL instances.
  9. At least 3 instances are needed for the cluster to be able to withstand up to
  10. one server failure.
  11. ## 添加子节点
  12. mysql-js> cluster.addInstance(‘root@db2:3306‘)
  13. mysql-js> cluster.addInstance(‘root@db3:3306‘)
  14. ## 查看节点信息
  15. mysql-js> cluster.status()
  16. ## 将配置 持久化,写入到 my.cnf
  17. mysql-js> \connect db1
  18. mysql-js> dba.configureLocalInstance(‘db1:3306‘)
  19. ## 查看基本信息
  20. mysql-js> cluster.describe();
  21. ## 退出之后,再查看节点信息
  22. var cluster = dba.getCluster();
  23. cluster.status();
  24. </code>

Mysql-route 设置

  1. <code>## 此命令会更新 /etc/mysqlrouter/mysqlrouter.conf 中的配置信息, 可以是别的机器 这里选择的为db2
  2. [root@db2 ~]# mysqlrouter --bootstrap root@db1:3306 --user mysqlrouter
  3. Please enter MySQL password for root:
  4. WARNING: The MySQL server does not have SSL configured and metadata used by the router may be transmitted unencrypted.
  5. Bootstrapping system MySQL Router instance...
  6. MySQL Router has now been configured for the InnoDB cluster ‘main‘.
  7. The following connection information can be used to connect to the cluster.
  8. Classic MySQL protocol connections to cluster ‘main‘:
  9. - Read/Write Connections: localhost:6446 读写
  10. - Read/Only Connections: localhost:6447 只读
  11. X protocol connections to cluster ‘main‘:
  12. - Read/Write Connections: localhost:64460
  13. - Read/Only Connections: localhost:64470
  14. Existing configurations backed up to /etc/mysqlrouter/mysqlrouter.conf.bak
  15. [root@db2 ~]# systemctl start mysqlrouter
  16. ## 启动
  17. systemctl start mysqlrouter
  18. systemctl enable mysqlrouter
  19. ## 查看端口
  20. [root@db2 ~]# netstat -lntup
  21. Active Internet connections (only servers)
  22. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  23. tcp 0 0 0.0.0.0:64460 0.0.0.0:* LISTEN 2958/mysqlrouter
  24. tcp 0 0 0.0.0.0:6446 0.0.0.0:* LISTEN 2958/mysqlrouter
  25. tcp 0 0 0.0.0.0:6447 0.0.0.0:* LISTEN 2958/mysqlrouter
  26. tcp 0 0 0.0.0.0:64470 0.0.0.0:* LISTEN 2958/mysqlrouter
  27. ## 验证
  28. mysql -u root -h 127.0.0.1 -P 6446 -p
  29. select @@port;
  30. select @@hostname;
  31. </code>

故障模拟

  1. <code>##关闭 db1 数据库,自动切换如下:
  2. "topology": {
  3. "db1:3306": {
  4. "address": "db1:3306",
  5. "mode": "R/O",
  6. "readReplicas": {},
  7. "role": "HA",
  8. "status": "(MISSING)"
  9. },
  10. "db2:3306": {
  11. "address": "db2:3306",
  12. "mode": "R/W",
  13. "readReplicas": {},
  14. "role": "HA",
  15. "status": "ONLINE"
  16. },
  17. "db3:3306": {
  18. "address": "db3:3306",
  19. "mode": "R/O",
  20. "readReplicas": {},
  21. "role": "HA",
  22. "status": "ONLINE"
  23. }
  24. </code>
  1. <code>##重启db2 ,执行命令
  2. mysql> show databases;
  3. ERROR 2013 (HY000): Lost connection to MySQL server during query
  4. mysql> show databases;
  5. ERROR 2006 (HY000): MySQL server has gone away
  6. No connection. Trying to reconnect...
  7. Connection id: 20
  8. Current database: *** NONE ***
  9. mysql> select @@hostname;
  10. +------------+
  11. | @@hostname |
  12. +------------+
  13. | db1 |
  14. +------------+
  15. ##重启节点后,需要手动加入
  16. "db2:3306": {
  17. "address": "db2:3306",
  18. "mode": "R/O",
  19. "readReplicas": {},
  20. "role": "HA",
  21. "status": "(MISSING)"
  22. cluster.rejoinInstance(‘root@db2:3306‘)
  23. The instance ‘db2:3306‘ was successfully added to the MySQL Cluster.
  24. </code>
  1. <code>## 所有节点都重启了,重新加入
  2. mysqlsh --uri root@db1:3306
  3. mysql-js> var cluster = dba.rebootClusterFromCompleteOutage();
  4. Reconfiguring the default cluster from complete outage...
  5. The instance ‘db2:3306‘ was part of the cluster configuration.
  6. Would you like to rejoin it to the cluster? [y|N]: y
  7. The instance ‘db3:3306‘ was part of the cluster configuration.
  8. Would you like to rejoin it to the cluster? [y|N]: y
  9. The cluster was successfully rebooted.
  10. </code>

报错总结:

  1. <code>##如果节点在加入集群前,执行了写操作,加入集群时会报错
  2. ERROR: Error joining instance to cluster: ‘db2:3306‘ - Query failed. MySQL Error (3092): The server is not configured properly to be an active member of the group. Please see more details on error log.. Query: START group_replication (RuntimeError)
  3. ##登陆 db2 数据库 执行 reset master;
  4. </code>
  1. <code>## 如果出现了 "status": "NO_QUORUM" 执行修复,重新加入
  2. ## 暂未测试
  3. cluster.forceQuorumUsingPartitionOf("db1:3306")
  4. mysql-js> cluster.rejoinInstance(‘root@db2:3306‘)
  5. mysql-js> cluster.rejoinInstance(‘root@db3:3306‘)
  6. </code>

附言:

官方文档: https://dev.mysql.com/doc/refman/5.7/en/mysql-innodb-cluster-userguide.html

  1. <code>节点有哪状态
  2. * ONLINE - 节点状态正常。
  3. * OFFLINE - 实例在运行,但没有加入任何Cluster。
  4. * RECOVERING - 实例已加入Cluster,正在同步数据。
  5. * ERROR - 同步数据发生异常。
  6. * UNREACHABLE - 与其他节点通讯中断,可能是网络问题,可能是节点crash。
  7. * MISSING 节点已加入集群,但未启动group replication
  8. 集群有哪些状态
  9. * OK – 所有节点处于online状态,有冗余节点。
  10. * OK_PARTIAL – 有节点不可用,但仍有冗余节点。
  11. * OK_NO_TOLERANCE – 有足够的online节点,但没有冗余,例如:两个节点的Cluster,其中一个挂了,集群就不可用了。
  12. * NO_QUORUM – 有节点处于online状态,但达不到法定节点数,此状态下Cluster无法写入,只能读取。
  13. * UNKNOWN – 不是online或recovering状态,尝试连接其他实例查看状态。
  14. * UNAVAILABLE – 组内节点全是offline状态,但实例在运行,可能实例刚重启还没加入Cluster。</code>

MySQL InnoDB Cluser | Mysql 5.7 集群

标签:net   png   持久化   分享图片   name   ref   data   may   部署   

人气教程排行