核心架构

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

部署

本次共3台机器,设置主机名及hosts | 配置每台服务 my.cnf 中report_host 字段,为自己的 hostname
192.168.10.123 db1
192.168.10.124 db2
192.168.10.125 db3
安装 mysql5.7.20 ,可以参考下面的安装基本,我搭建的时候就是用的这个。
https://blog.51cto.com/hequan/1982428
安装mysql-shell mysql-route
wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

yum install mysql57-community-release-el7-11.noarch.rpm
yum install mysql-shell -y
yum install mysql-router -y
设置相关用户的权限,生产环境 可以不是 root用户
grant all privileges on . to 'root'@'%' identified by '123456';
GRANT ALL PRIVILEGES ON mysql_innodb_cluster_metadata. TO root@'%' WITH GRANT OPTION;
GRANT RELOAD, SHUTDOWN, PROCESS, FILE, SUPER, REPLICATION SLAVE, REPLICATION CLIENT, \
CREATE USER ON
. TO root@'%' WITH GRANT OPTION;
GRANT SELECT ON
.* TO root@'%' WITH GRANT OPTION;
flush privileges;
mysqlsh

[root@db1 ~]# mysqlsh

检查mysql 配置文件 (3台主机都要操作此步骤)

dba.checkInstanceConfiguration('root@db1:3306')

+----------------------------------+---------------+----------------+--------------------------------------------------+
| Variable | Current Value | Required Value | Note |
+----------------------------------+---------------+----------------+--------------------------------------------------+
| binlog_checksum | CRC32 | NONE | Update the server variable or restart the server |
| binlog_format | MIXED | ROW | Update the server variable or restart the server |
| enforce_gtid_consistency | OFF | ON | Restart the server |
| gtid_mode | OFF | ON | Restart the server |
| log_slave_updates | 0 | ON | Restart the server |
| master_info_repository | FILE | TABLE | Restart the server |
| relay_log_info_repository | FILE | TABLE | Restart the server |
| transaction_write_set_extraction | OFF | XXHASH64 | Restart the server |
+----------------------------------+---------------+----------------+--------------------------------------------------+

修复mysql 配置文件, 必须用 root(3台主机都要操作此步骤)

dba.configureLocalInstance('root@db1:3306')

Please provide the password for 'root@db1:3306':
Detecting the configuration file...
Found configuration file at standard location: /etc/my.cnf
Do you want to modify this file? [Y|n]: [Y|n]: Y

重启mysql

重新检查 (3台主机都要操作此步骤)

dba.checkInstanceConfiguration('root@db1:3306')
Please provide the password for 'root@db1:3306':
Validating instance...

The instance 'db1:3306' is valid for Cluster usage
{
"status": "ok"
}

登陆

mysqlsh --uri root@db1:3306

创建集群 main

mysql-js> var cluster = dba.createCluster('main')
A new InnoDB cluster will be created on instance 'hequan@db1:3306'.

Creating InnoDB cluster 'main' on 'hequan@db1: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-js> cluster.addInstance('root@db2:3306')
mysql-js> cluster.addInstance('root@db3:3306')

查看节点信息

mysql-js> cluster.status()

将配置 持久化,写入到 my.cnf

mysql-js> \connect db1
mysql-js> dba.configureLocalInstance('db1:3306')

查看基本信息

mysql-js> cluster.describe();

退出之后,再查看节点信息

var cluster = dba.getCluster();
cluster.status();
Mysql-route 设置

此命令会更新 /etc/mysqlrouter/mysqlrouter.conf 中的配置信息, 可以是别的机器 这里选择的为db2

[root@db2 ~]# mysqlrouter --bootstrap root@db1:3306 --user mysqlrouter

Please enter MySQL password for root:
WARNING: The MySQL server does not have SSL configured and metadata used by the router may be transmitted unencrypted.

Bootstrapping system MySQL Router instance...
MySQL Router has now been configured for the InnoDB cluster 'main'.

The following connection information can be used to connect to the cluster.

Classic MySQL protocol connections to cluster 'main':

  • Read/Write Connections: localhost:6446 读写
  • Read/Only Connections: localhost:6447 只读

X protocol connections to cluster 'main':

  • Read/Write Connections: localhost:64460
  • Read/Only Connections: localhost:64470

Existing configurations backed up to /etc/mysqlrouter/mysqlrouter.conf.bak
[root@db2 ~]# systemctl start mysqlrouter

启动

systemctl start mysqlrouter
systemctl enable mysqlrouter

查看端口

[root@db2 ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:64460 0.0.0.0: LISTEN 2958/mysqlrouter
tcp 0 0 0.0.0.0:6446 0.0.0.0:
LISTEN 2958/mysqlrouter
tcp 0 0 0.0.0.0:6447 0.0.0.0: LISTEN 2958/mysqlrouter
tcp 0 0 0.0.0.0:64470 0.0.0.0:
LISTEN 2958/mysqlrouter

验证

mysql -u root -h 127.0.0.1 -P 6446 -p

select @@port;
select @@hostname;
故障模拟

##关闭 db1 数据库,自动切换如下:

"topology": {
"db1:3306": {
"address": "db1:3306",
"mode": "R/O",
"readReplicas": {},
"role": "HA",
"status": "(MISSING)"
},
"db2:3306": {
"address": "db2:3306",
"mode": "R/W",
"readReplicas": {},
"role": "HA",
"status": "ONLINE"
},
"db3:3306": {
"address": "db3:3306",
"mode": "R/O",
"readReplicas": {},
"role": "HA",
"status": "ONLINE"
}
##重启db2 ,执行命令

mysql> show databases;
ERROR 2013 (HY000): Lost connection to MySQL server during query
mysql> show databases;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 20
Current database: NONE
mysql> select @@hostname;
+------------+
| @@hostname |
+------------+
| db1 |
+------------+

##重启节点后,需要手动加入
"db2:3306": {
"address": "db2:3306",
"mode": "R/O",
"readReplicas": {},
"role": "HA",
"status": "(MISSING)"

cluster.rejoinInstance('root@db2:3306')
The instance 'db2:3306' was successfully added to the MySQL Cluster.

所有节点都重启了,重新加入

mysqlsh --uri root@db1:3306
mysql-js> var cluster = dba.rebootClusterFromCompleteOutage();

Reconfiguring the default cluster from complete outage...

The instance 'db2:3306' was part of the cluster configuration.
Would you like to rejoin it to the cluster? [y|N]: y

The instance 'db3:3306' was part of the cluster configuration.
Would you like to rejoin it to the cluster? [y|N]: y

The cluster was successfully rebooted.
报错总结:

##如果节点在加入集群前,执行了写操作,加入集群时会报错
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)

##登陆 db2 数据库 执行 reset master;

如果出现了 "status": "NO_QUORUM" 执行修复,重新加入

暂未测试

cluster.forceQuorumUsingPartitionOf("db1:3306")

mysql-js> cluster.rejoinInstance('root@db2:3306')
mysql-js> cluster.rejoinInstance('root@db3:3306')
附言:

官方文档: https://dev.mysql.com/doc/refman/5.7/en/mysql-innodb-cluster-userguide.html
节点有哪状态

* ONLINE  - 节点状态正常。
* OFFLINE  -   实例在运行,但没有加入任何Cluster。
* RECOVERING - 实例已加入Cluster,正在同步数据。
* ERROR  -  同步数据发生异常。
* UNREACHABLE -  与其他节点通讯中断,可能是网络问题,可能是节点crash。
* MISSING 节点已加入集群,但未启动group replication

集群有哪些状态

* OK – 所有节点处于online状态,有冗余节点。
* OK_PARTIAL – 有节点不可用,但仍有冗余节点。
* OK_NO_TOLERANCE – 有足够的online节点,但没有冗余,例如:两个节点的Cluster,其中一个挂了,集群就不可用了。
* NO_QUORUM – 有节点处于online状态,但达不到法定节点数,此状态下Cluster无法写入,只能读取。
* UNKNOWN – 不是online或recovering状态,尝试连接其他实例查看状态。
* UNAVAILABLE – 组内节点全是offline状态,但实例在运行,可能实例刚重启还没加入Cluster。

转载于:https://blog.51cto.com/13480211/2060178

MYSQL 的集群相关推荐

  1. 京东如何基于Vitess管理大型MySQL实例集群

    本文根据dbaplus社群第203期线上分享整理而成 讲师介绍 王云博 京东数据库高级研发工程师 2014年加入京东,一直致力于数据库运维自动化的发展: 2018年专注于京东弹性数据库研发.运维工作. ...

  2. 如何搭建一个 MySQL 分布式集群

    作者丨茶轴的青春 https://segmentfault.com/a/1190000016829266?utm_source=tag-newest 1.准备集群搭建环境 使用6台虚拟机来搭建 MyS ...

  3. Step By Step 搭建 MySql MHA 集群

    关于MHA    MHA(Master High Availability)是一款开源的mysql高可用程序,目前在mysql高可用方面是一个相对成熟的解决方案.MHA 搭建的前提是MySQL集群中已 ...

  4. LINUX 下 配置MySQL数据库集群

    LINUX 下 配置MySQL数据库集群 MySQL数据库集群进行正确配置步骤(1) 此文章主要向大家讲述的是对MySQL数据库集群进行正确配置的实际操作步骤,以及对其概念的讲述,如果你对其相关的实际 ...

  5. innodb 集群_部署MySQL InnoDB集群以实现高可用性

    innodb 集群 In this article, I am going to explain how we can deploy the MySQL InnoDB cluster. InnoDB ...

  6. mysql数据库集群 主主复制 原理_MySql搭建集群 之 主主复制(双主代从)MYSQL数据库...

    作者:VEPHP   时间 2017-09-27 <MySql搭建集群 之 主主复制(双主代从)MYSQL数据库>要点: 本文介绍了MySql搭建集群 之 主主复制(双主代从)MYSQL数 ...

  7. Mysql Cluster集群实现高可用

    MySQL集群搭建实现高可用 本节所讲内容: l MySQL 集群 概述 l 实战:MySQL 集群 搭建 1  MySQL集群概述和安装环境 MySQL Cluster是MySQL适合于分布式计算环 ...

  8. MySQL cluster集群/NDB集群学习

    公司主业务用的MySQL集群是官方自带的MySQL Cluster,MySQL自带的集群模式,这个集群的模式和MGR.PXC.主从 有点不一样. 一.存储引擎不是innodb,MySQL Cluste ...

  9. 使用Docker搭建高可用Mysql数据库集群

    文章目录 前言 正文 选择哪一种搭建集群的方式 PXC Replication 安装docker Docker操作的基本命令 安装PXC集群 负载均衡 双机热备 具体实现细节 安装keepalived ...

  10. MySQL数据库集群的原理与搭建

    [1.应用背景]     在最近做的项目中,因为有一个短时间内很多人访问服务,会频繁访问数据库,看到这里不免有些人会想起数据库集群.我们组长也想到了用数据库集群,于是就带着我们几个研究起来了mysql ...

最新文章

  1. YII2中分页组件的使用
  2. 以太坊区块和交易存储
  3. Servlet登陆功能的实现
  4. PW Live 直播 | 清华大学博士生岂凡超:义原知识库的应用和扩充
  5. OpenCV学习笔记之扫描图像
  6. arcengine遍历属性表_Redis源码解析四--跳跃表
  7. jquey的parent()和parents()的区别
  8. Android设置Spinner控件的文字居中显示
  9. VBA处理文件框架代码 【第七部分:程序页面布局】
  10. DB2 exception: Cannot create PoolableConnectionFactory SQLCODE=-142
  11. 一副对联,送给所有创业小公司
  12. 计算机毕业论文中期论文质量,计算机 毕业论文(设计)中期报告(1页)-原创力文档...
  13. UWB定位系统场景的分析
  14. 服务器对比个人计算机,服务器与一般电脑有何区别,为什么差价很大?
  15. 注塑模具与吹塑模具的区别
  16. 硬盘缓存作用是什么,缓存是不是越大越好?
  17. XToDoList 助你一臂之力
  18. 如何理解充分条件和必要条件
  19. vc_redist 又名VC runtime library,或MSCVRT
  20. 在两个已经排好序的数组里找出第K小的数

热门文章

  1. 作业四:灯泡异常问题
  2. 初步学习nodejs,业余用node写个一个自动创建目录和文件的小脚本,希望对需要的人有所帮助...
  3. [English] notes
  4. Err:error occurred at recursive SQL level 1
  5. 软件测试Bug管理规范
  6. 接口测试时,输入所有参数的参数值后,接口返回“参数错误:所有参数都不能为空”
  7. 外包员工午休起身喝水被正式员工大骂:脑子进“S”,打扰我休息
  8. 0基础可以学Web前端吗?如何快速掌握前端开发技能?
  9. 前端,后端,全栈哪个好找工作?
  10. 新手如何入门学习前端?