如何在Ubuntu18.04上创建多节点MySQL集群

来自菜鸟教程
跳转至:导航、​搜索

介绍

MySQL Cluster 分布式数据库为您的 MySQL 数据库管理系统提供高可用性和吞吐量。 MySQL 集群由一个或多个管理节点 (ndb_mgmd) 组成,这些节点存储集群的配置并控制存储集群数据的数据节点 (ndbd)。 与管理节点通信后,客户端(MySQL 客户端、服务器或本机 API)直接连接到这些数据节点。

使用 MySQL Cluster,通常没有数据复制,而是数据节点同步。 为此,必须使用特殊的数据引擎——NDBCluster (NDB)。 将集群视为具有冗余组件的单个逻辑 MySQL 环境会很有帮助。 因此,一个 MySQL 集群可以参与与其他 MySQL 集群的复制。

MySQL Cluster 在无共享环境中工作得最好。 理想情况下,任何两个组件都不应该共享相同的硬件。 为了简单和演示目的,我们将限制自己只使用三个服务器。 我们将设置两台服务器作为数据节点,在它们之间同步数据。 第三台服务器将用于集群管理器以及 MySQL 服务器/客户端。 如果您启动额外的服务器,您可以向集群添加更多数据节点,将集群管理器与 MySQL 服务器/客户端分离,并将更多服务器配置为集群管理器和 MySQL 服务器/客户端。

先决条件

要完成本教程,您总共需要三台服务器:两台服务器用于冗余 MySQL 数据节点 (ndbd),一台服务器用于集群管理器 (ndb_mgmd) 和 MySQL 服务器/客户端(mysqldmysql)。

相同的 DigitalOcean 数据中心 中,创建以下启用了 私有网络的 Droplet

请务必记下您的三个 Droplet 的 私有 IP 地址。 在本教程中,我们的集群节点具有以下私有 IP 地址:

  • 198.51.100.0 将是第一个 MySQL Cluster 数据节点
  • 198.51.100.1 将是第二个数据节点
  • 198.51.100.2将是集群管理器和 MySQL 服务器节点

启动 Droplet、配置非 root 用户并记下 3 个节点的 IP 地址后,您就可以开始本教程了。

第 1 步 — 安装和配置集群管理器

我们将首先下载并安装 MySQL Cluster Manager,ndb_mgmd

要安装 Cluster Manager,我们首先需要从官方 MySQL Cluster 下载页面 获取相应的 .deb 安装程序文件。

在此页面中,在 选择操作系统 下,选择 Ubuntu Linux。 然后,在 Select OS Version 下,选择 Ubuntu Linux 18.04 (x86, 64-bit)

向下滚动,直到看到 DEB 包、NDB 管理服务器 ,然后单击 下载 链接以获取 not 包含 dbgsym 的链接(除非您需要调试符号)。 您将被带到 开始下载 页面。 在这里,右键单击 不,谢谢,开始我的下载。 并将链接复制到 .deb 文件。

现在,登录到您的集群管理器 Droplet(在本教程中为 198.51.100.2),并下载此 .deb 文件:

cd ~
wget https://dev.mysql.com/get/Downloads/MySQL-Cluster-7.6/mysql-cluster-community-management-server_7.6.6-1ubuntu18.04_amd64.deb

使用 dpkg 安装 ndb_mgmd

sudo dpkg -i mysql-cluster-community-management-server_7.6.6-1ubuntu18.04_amd64.deb

我们现在需要在第一次运行之前配置ndb_mgmd; 正确的配置将确保数据节点之间的正确同步和负载分配。

Cluster Manager 应该是任何 MySQL 集群中启动的第一个组件。 它需要一个配置文件,作为其可执行文件的参数传入。 我们将创建并使用以下配置文件:/var/lib/mysql-cluster/config.ini

在 Cluster Manager Droplet 上,创建该文件所在的 /var/lib/mysql-cluster 目录:

sudo mkdir /var/lib/mysql-cluster

然后使用您喜欢的文本编辑器创建和编辑配置文件:

sudo nano /var/lib/mysql-cluster/config.ini

将以下文本粘贴到您的编辑器中:

/var/lib/mysql-cluster/config.ini

[ndbd default]
# Options affecting ndbd processes on all data nodes:
NoOfReplicas=2  # Number of replicas

[ndb_mgmd]
# Management process options:
hostname=198.51.100.2 # Hostname of the manager
datadir=/var/lib/mysql-cluster  # Directory for the log files

[ndbd]
hostname=198.51.100.0 # Hostname/IP of the first data node
NodeId=2            # Node ID for this data node
datadir=/usr/local/mysql/data   # Remote directory for the data files

[ndbd]
hostname=198.51.100.1 # Hostname/IP of the second data node
NodeId=3            # Node ID for this data node
datadir=/usr/local/mysql/data   # Remote directory for the data files

[mysqld]
# SQL node options:
hostname=198.51.100.2 # In our case the MySQL server/client is on the same Droplet as the cluster manager

粘贴此文本后,确保将上面的 hostname 值替换为您配置的 Droplet 的正确 IP 地址。 设置此 hostname 参数是一项重要的安全措施,可防止其他服务器连接到集群管理器。

保存文件并关闭文本编辑器。

这是 MySQL 集群的精简配置文件。 您应该根据生产需要自定义此文件中的参数。 有关完全配置的 ndb_mgmd 配置文件的示例,请参阅 MySQL 集群 文档

在上面的文件中,您可以通过将实例附加到适当的部分来添加其他组件,例如数据节点 (ndbd) 或 MySQL 服务器节点 (mysqld)。

我们现在可以通过执行 ndb_mgmd 二进制文件并使用 -f 标志指定其配置文件来启动管理器:

sudo ndb_mgmd -f /var/lib/mysql-cluster/config.ini

您应该看到以下输出:

OutputMySQL Cluster Management Server mysql-5.7.22 ndb-7.6.6
2018-07-25 21:48:39 [MgmtSrvr] INFO     -- The default config directory '/usr/mysql-cluster' does not exist. Trying to create it...
2018-07-25 21:48:39 [MgmtSrvr] INFO     -- Successfully created config directory

这表明 MySQL 集群管理服务器已成功安装并且现在正在您的 Droplet 上运行。

理想情况下,我们希望在启动时自动启动集群管理服务器。 为此,我们将创建并启用一个 systemd 服务。

在我们创建服务之前,我们需要杀死正在运行的服务器:

sudo pkill -f ndb_mgmd

现在,使用您喜欢的编辑器打开并编辑以下 systemd Unit 文件:

sudo nano /etc/systemd/system/ndb_mgmd.service

粘贴以下代码:

/etc/systemd/system/ndb_mgmd.service

[Unit]
Description=MySQL NDB Cluster Management Server
After=network.target auditd.service

[Service]
Type=forking
ExecStart=/usr/sbin/ndb_mgmd -f /var/lib/mysql-cluster/config.ini
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure

[Install]
WantedBy=multi-user.target

在这里,我们添加了一组最小的选项来指导 systemd 如何启动、停止和重新启动 ndb_mgmd 进程。 要了解有关此单元配置中使用的选项的更多信息,请参阅 systemd 手册

保存并关闭文件。

现在,使用 daemon-reload 重新加载 systemd 的管理器配置:

sudo systemctl daemon-reload

我们将启用我们刚刚创建的服务,以便 MySQL 集群管理器在重新启动时启动:

sudo systemctl enable ndb_mgmd

最后,我们将启动服务:

sudo systemctl start ndb_mgmd

您可以验证 NDB Cluster Management 服务是否正在运行:

sudo systemctl status ndb_mgmd

您应该看到以下输出:

● ndb_mgmd.service - MySQL NDB Cluster Management Server
   Loaded: loaded (/etc/systemd/system/ndb_mgmd.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2018-07-26 21:23:37 UTC; 3s ago
  Process: 11184 ExecStart=/usr/sbin/ndb_mgmd -f /var/lib/mysql-cluster/config.ini (code=exited, status=0/SUCCESS)
 Main PID: 11193 (ndb_mgmd)
    Tasks: 11 (limit: 4915)
   CGroup: /system.slice/ndb_mgmd.service
           └─11193 /usr/sbin/ndb_mgmd -f /var/lib/mysql-cluster/config.ini

这表明 ndb_mgmd MySQL 集群管理服务器现在作为 systemd 服务运行。

设置集群管理器的最后一步是允许来自我们专用网络上其他 MySQL 集群节点的传入连接。

如果您在设置此 Droplet 时未配置 ufw 防火墙,则可以跳至下一部分。

我们将添加规则以允许来自两个数据节点的本地传入连接:

sudo ufw allow from 198.51.100.0
sudo ufw allow from 198.51.100.1

输入这些命令后,您应该会看到以下输出:

OutputRule added

集群管理器现在应该已启动并运行,并且能够通过专用网络与其他集群节点通信。

第 2 步 — 安装和配置数据节点

注意: 本节中的所有命令都应在两个数据节点上执行。


在这一步中,我们将安装 ndbd MySQL Cluster 数据节点守护程序,并配置节点以便它们可以与 Cluster Manager 通信。

要安装数据节点二进制文件,我们首先需要从官方 MySQL 下载页面 获取适当的 .deb 安装程序文件。

在此页面中,在 选择操作系统 下,选择 Ubuntu Linux。 然后,在 Select OS Version 下,选择 Ubuntu Linux 18.04 (x86, 64-bit)

向下滚动,直到看到 DEB 包,NDB 数据节点二进制文件 ,然后单击 下载 链接以获取 包含 dbgsym 的链接(除非您需要调试符号)。 您将被带到 开始下载 页面。 在这里,右键单击 不,谢谢,开始我的下载。 并将链接复制到 .deb 文件。

现在,登录到您的第一个数据节点 Droplet(在本教程中为 198.51.100.0),并下载此 .deb 文件:

cd ~
wget https://dev.mysql.com/get/Downloads/MySQL-Cluster-7.6/mysql-cluster-community-data-node_7.6.6-1ubuntu18.04_amd64.deb

在我们安装数据节点二进制文件之前,我们需要安装一个依赖项,libclass-methodmaker-perl

sudo apt update
sudo apt install libclass-methodmaker-perl

我们现在可以使用 dpkg 安装数据注释二进制文件:

sudo dpkg -i mysql-cluster-community-data-node_7.6.6-1ubuntu18.04_amd64.deb

数据节点从 MySQL 的标准位置 /etc/my.cnf 中提取它们的配置。 使用您喜欢的文本编辑器创建此文件并开始编辑它:

sudo nano /etc/my.cnf

将以下配置参数添加到文件中:

/etc/my.cnf

[mysql_cluster]
# Options for NDB Cluster processes:
ndb-connectstring=198.51.100.2  # location of cluster manager

指定 Cluster Manager 节点的位置是 ndbd 启动所需的唯一配置。 其余配置将直接从管理器中提取。

保存并退出文件。

在我们的示例中,根据管理器的配置,数据节点将发现其数据目录为 /usr/local/mysql/data。 在启动守护进程之前,我们将在节点上创建这个目录:

sudo mkdir -p /usr/local/mysql/data

现在我们可以使用以下命令启动数据节点:

sudo ndbd

您应该看到以下输出:

Output2018-07-18 19:48:21 [ndbd] INFO     -- Angel connected to '198.51.100.2:1186'
2018-07-18 19:48:21 [ndbd] INFO     -- Angel allocated nodeid: 2

NDB 数据节点守护程序已成功安装,现在正在您的服务器上运行。

我们还需要允许来自其他 MySQL Cluster 节点的传入连接通过专用网络。

如果您在设置此 Droplet 时未配置 ufw 防火墙,则可以跳至为 ndbd 设置 systemd 服务。

我们将添加规则以允许来自集群管理器和其他数据节点的传入连接:

sudo ufw allow from 198.51.100.0
sudo ufw allow from 198.51.100.2

输入这些命令后,您应该会看到以下输出:

OutputRule added

您的 MySQL 数据节点 Droplet 现在可以通过专用网络与集群管理器和其他数据节点进行通信。

最后,我们还希望数据节点守护进程在服务器启动时自动启动。 我们将遵循与集群管理器相同的过程,并创建一个 systemd 服务。

在我们创建服务之前,我们将终止正在运行的 ndbd 进程:

sudo pkill -f ndbd

现在,使用您喜欢的编辑器打开并编辑以下 systemd Unit 文件:

sudo nano /etc/systemd/system/ndbd.service

粘贴以下代码:

/etc/systemd/system/ndbd.service

[Unit]
Description=MySQL NDB Data Node Daemon
After=network.target auditd.service

[Service]
Type=forking
ExecStart=/usr/sbin/ndbd
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure

[Install]
WantedBy=multi-user.target

在这里,我们添加了一组最小的选项来指导 systemd 如何启动、停止和重新启动 ndbd 进程。 要了解有关此单元配置中使用的选项的更多信息,请参阅 systemd 手册

保存并关闭文件。

现在,使用 daemon-reload 重新加载 systemd 的管理器配置:

sudo systemctl daemon-reload

我们现在将启用我们刚刚创建的服务,以便数据节点守护程序在重新启动时启动:

sudo systemctl enable ndbd

最后,我们将启动服务:

sudo systemctl start ndbd

您可以验证 NDB Cluster Management 服务是否正在运行:

sudo systemctl status ndbd

您应该看到以下输出:

Output● ndbd.service - MySQL NDB Data Node Daemon
   Loaded: loaded (/etc/systemd/system/ndbd.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2018-07-26 20:56:29 UTC; 8s ago
  Process: 11972 ExecStart=/usr/sbin/ndbd (code=exited, status=0/SUCCESS)
 Main PID: 11984 (ndbd)
    Tasks: 46 (limit: 4915)
   CGroup: /system.slice/ndbd.service
           ├─11984 /usr/sbin/ndbd
           └─11987 /usr/sbin/ndbd

这表明 ndbd MySQL Cluster 数据节点守护程序现在作为 systemd 服务运行。 您的数据节点现在应该功能齐全,并且能够连接到 MySQL 集群管理器。

完成第一个数据节点的设置后,在另一个数据节点(本教程中的 198.51.100.1)上重复本节中的步骤。

第 3 步 — 配置和启动 MySQL 服务器和客户端

标准 MySQL 服务器,例如 Ubuntu 的 APT 存储库中可用的服务器,不支持 MySQL 集群引擎 NDB。 这意味着我们需要安装与我们在本教程中安装的其他 MySQL Cluster 软件一起打包的自定义 SQL 服务器。

我们将再次从官方 MySQL Cluster 下载页面 获取 MySQL Cluster Server 二进制文件。

在此页面中,在 选择操作系统 下,选择 Ubuntu Linux。 然后,在 Select OS Version 下,选择 Ubuntu Linux 18.04 (x86, 64-bit)

向下滚动直到看到 DEB Bundle,然后单击 Download 链接(它应该是列表中的第一个)。 您将被带到 开始下载 页面。 在这里,右键单击 不,谢谢,开始我的下载。 并将链接复制到 .tar 存档。

现在,登录到集群管理器 Droplet(在本教程中,198.51.100.2),并下载这个 .tar 存档(回想一下,我们将 MySQL 服务器安装在与集群管理器相同的节点上——在一个生产设置,你应该在不同的节点上运行这些守护进程):

cd ~
wget https://dev.mysql.com/get/Downloads/MySQL-Cluster-7.6/mysql-cluster_7.6.6-1ubuntu18.04_amd64.deb-bundle.tar

我们现在将这个存档解压缩到一个名为 install 的目录中。 首先,创建目录:

mkdir install

现在将存档解压缩到此目录中:

tar -xvf mysql-cluster_7.6.6-1ubuntu18.04_amd64.deb-bundle.tar -C install/

移至此目录,其中包含提取的 MySQL Cluster 组件二进制文件:

cd install

在我们安装 MySQL 服务器二进制文件之前,我们需要安装几个依赖项:

sudo apt update
sudo apt install libaio1 libmecab2

现在,我们需要安装 MySQL 集群依赖项,捆绑在我们刚刚提取的 tar 存档中:

sudo dpkg -i mysql-common_7.6.6-1ubuntu18.04_amd64.deb
sudo dpkg -i mysql-cluster-community-client_7.6.6-1ubuntu18.04_amd64.deb
sudo dpkg -i mysql-client_7.6.6-1ubuntu18.04_amd64.deb
sudo dpkg -i mysql-cluster-community-server_7.6.6-1ubuntu18.04_amd64.deb

安装 mysql-cluster-community-server 时,会出现配置提示,要求您为 MySQL 数据库的 root 帐户设置密码。 选择一个强大、安全的密码,然后点击' . 重新输入这个提示时输入密码,然后点击'再次完成安装。

我们现在可以使用 dpkg 安装 MySQL 服务器二进制文件:

sudo dpkg -i mysql-server_7.6.6-1ubuntu18.04_amd64.deb

我们现在需要配置这个 MySQL 服务器安装。

MySQL 服务器的配置存储在默认的 /etc/mysql/my.cnf 文件中。

使用您喜欢的编辑器打开此配置文件:

sudo nano /etc/mysql/my.cnf

您应该看到以下文本:

/etc/mysql/my.cnf

# Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

#
# The MySQL Cluster Community Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

将以下配置附加到它:

/etc/mysql/my.cnf

. . .
[mysqld]
# Options for mysqld process:
ndbcluster                      # run NDB storage engine

[mysql_cluster]
# Options for NDB Cluster processes:
ndb-connectstring=198.51.100.2  # location of management server

保存并退出文件。

重新启动 MySQL 服务器以使这些更改生效:

sudo systemctl restart mysql

默认情况下,MySQL 应在服务器重新启动时自动启动。 如果没有,下面的命令应该解决这个问题:

sudo systemctl enable mysql

SQL 服务器现在应该在您的集群管理器/MySQL 服务器 Droplet 上运行。

在下一步中,我们将运行一些命令来验证我们的 MySQL 集群安装是否按预期运行。

第 4 步 — 验证 MySQL 集群安装

要验证您的 MySQL 集群安装,请登录到您的集群管理器/SQL Server 节点。

我们将从命令行打开 MySQL 客户端并通过输入以下命令连接到我们刚刚配置的 root 帐户:

mysql -u root -p 

出现提示时输入您的密码,然后点击 ENTER

您应该看到类似于以下内容的输出:

OutputWelcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.22-ndb-7.6.6 MySQL Cluster Community Server (GPL)

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

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

进入 MySQL 客户端后,运行以下命令:

SHOW ENGINE NDB STATUS \G

您现在应该看到有关 NDB 集群引擎的信息,从连接参数开始:

Output
*************************** 1. row ***************************
  Type: ndbcluster
  Name: connection
Status: cluster_node_id=4, connected_host=198.51.100.2, connected_port=1186, number_of_data_nodes=2, number_of_ready_data_nodes=2, connect_count=0
. . .

这表明您已成功连接到 MySQL 集群。

注意这里 ready_data_nodes 的数量:2。 即使其中一个数据节点发生故障,这种冗余也允许您的 MySQL 集群继续运行。 这也意味着您的 SQL 查询将在两个数据节点之间进行负载平衡。

您可以尝试关闭其中一个数据节点以测试集群稳定性。 最简单的测试是重新启动数据节点 Droplet 以全面测试恢复过程。 您应该看到 number_of_ready_data_nodes 的值更改为 1 并在节点重新启动并重新连接到集群管理器时再次备份到 2

要退出 MySQL 提示,只需键入 quit 或按 CTRL-D

这是第一个表明 MySQL 集群、服务器和客户端正常工作的测试。 我们现在将进行额外的测试以确认集群是否正常运行。

使用以下命令打开集群管理控制台 ndb_mgm

ndb_mgm

您应该看到以下输出:

Output-- NDB Cluster -- Management Client --
ndb_mgm>

进入控制台后,输入命令 SHOW 并点击 ENTER

SHOW

您应该看到以下输出:

OutputConnected to Management Server at: 198.51.100.2:1186
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2    @198.51.100.0  (mysql-5.7.22 ndb-7.6.6, Nodegroup: 0, *)
id=3    @198.51.100.1  (mysql-5.7.22 ndb-7.6.6, Nodegroup: 0)

[ndb_mgmd(MGM)] 1 node(s)
id=1    @198.51.100.2  (mysql-5.7.22 ndb-7.6.6)

[mysqld(API)]   1 node(s)
id=4    @198.51.100.2  (mysql-5.7.22 ndb-7.6.6)

上图显示有两个数据节点与node-id的2和3相连。 还有一个node-id 1的管理节点和一个node-id 4的MySQL服务器。 您可以通过使用命令 STATUS 键入其编号来显示有关每个 id 的更多信息,如下所示:

2 STATUS
``` 

The above command shows you the status, MySQL version, and NDB version of node 2:

```
[secondary_label Output]
Node 2: started (mysql-5.7.22 ndb-7.6.6)
```

To exit the management console type `quit`, and then hit `ENTER`.

The management console is very powerful and gives you many other options for administering the cluster and its data, including creating an online backup. For more information consult the [official MySQL documentation](https://dev.mysql.com/doc/refman/5.7/en/mysql-cluster-management.html).

At this point, you’ve fully tested your MySQL Cluster installation. The concluding step of this guide shows you how to create and insert test data into this MySQL Cluster.

## Step 5 — Inserting Data into MySQL Cluster

To demonstrate the cluster’s functionality, let's create a new table using the NDB engine and insert some sample data into it. Note that in order to use cluster functionality, the engine must be specified explicitly as **NDB**. If you use InnoDB (default) or any other engine, you will not make use of the cluster.

First, let's create a database called `clustertest` with the command:

```custom_prefix(mysql>)
CREATE DATABASE clustertest;
```

Next, switch to the new database:

```custom_prefix(mysql>)
USE clustertest;
```

Now, create a simple table called `test_table` like this:

```custom_prefix(mysql>)
CREATE TABLE test_table (name VARCHAR(20), value VARCHAR(20)) ENGINE=ndbcluster;
```

We have explicitly specified the engine `ndbcluster` in order to make use of the cluster. 

Now, we can start inserting data using this SQL query:

```custom_prefix(mysql>)
INSERT INTO test_table (name,value) VALUES('some_name','some_value');
```

To verify that the data has been inserted, run the following select query:

```custom_prefix(mysql>)
SELECT * FROM test_table;
```

When you insert data into and select data from an `ndbcluster` table, the cluster load balances queries between all the available data nodes. This improves the stability and performance of your MySQL database installation.

You can also set the default storage engine to `ndbcluster` in the `my.cnf` file that we edited previously. If you do this, you won’t need to specify the `ENGINE` option when creating tables. To learn more, consult the MySQL [Reference Manual](https://dev.mysql.com/doc/refman/5.7/en/storage-engine-setting.html).

## Conclusion

In this tutorial, we’ve demonstrated how to set up and configure a MySQL Cluster on Ubuntu 18.04 servers. It’s important to note that this is a minimal, pared-down architecture used to demonstrate the installation procedure, and there are many advanced options and features worth learning about before deploying MySQL Cluster in production (for example, performing backups). To learn more, consult the official [MySQL Cluster  documentation](https://dev.mysql.com/doc/refman/5.7/en/mysql-cluster.html).