如何在 Ubuntu 18.04 上创建多节点 MySQL 群集

介绍

MySQL 集群分布式数据库为您的 MySQL 数据库管理系统提供高可用性和输出量. MySQL 集群由一个或多个管理节点(ndb_mgmd)组成,这些节点存储集群的配置并控制集群数据的节点(ndbd),其中集群数据存储。

在 MySQL 集群中,通常没有数据复制,而是数据节点同步。 为此,必须使用一个特殊的数据引擎 - NDBCluster (NDB)。

MySQL Cluster在共享-无所事事的环境中工作最好. 理想的情况是,任何两个组件都不应共享相同的硬件. 为了简单和示范的目的,我们将仅限于使用三个服务器. 我们将设置两个服务器作为数据节点,在它们之间同步数据。 第三台服务器将用于集群管理器,同时也用于MySQL服务器/客户端. 如果您旋转了额外的服务器,可以在集群中添加更多的数据节点,将集群管理器从 MySQL 服务器/客户端中解开,并配置更多的服务器作为集群管理器和 MySQL 服务器/客户端.

前提条件

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

在相同的 DigitalOcean 数据中心中,创建以下 Droplets 与 私人网络启用 :

请记住您的三个Dropplets的 私人 IP 地址. 在本教程中,我们的集群节点有以下私人 IP 地址:

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

一旦你把你的 Droplets 旋转,配置了非根用户,并记下了 3 个节点的 IP 地址,你已经准备好开始这个教程。

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

我们将首先通过下载和安装MySQL集群管理器,‘ndb_mgmd’开始。

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

从此页面,在 选择操作系统 下,选择 ** Ubuntu Linux** .然后,在 ** 选择 OS 版本** 下,选择 ** Ubuntu Linux 18.04 (x86, 64 位)** 。

向下滚动,直到你看到 DEB 包,NDB 管理服务器 ,然后点击 ** 下载** 链接,其中包含 dbgsym(除非你需要调试符号)。你将被带到一个 ** 开始你的下载** 页面。

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

1cd ~
2wget 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:

1sudo 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 目录:

1sudo mkdir /var/lib/mysql-cluster

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

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

将以下文本插入您的编辑器:

 1[label  /var/lib/mysql-cluster/config.ini]
 2[ndbd default]
 3# Options affecting ndbd processes on all data nodes:
 4NoOfReplicas=2	# Number of replicas
 5
 6[ndb_mgmd]
 7# Management process options:
 8hostname=198.51.100.2 # Hostname of the manager
 9datadir=/var/lib/mysql-cluster 	# Directory for the log files
10
11[ndbd]
12hostname=198.51.100.0 # Hostname/IP of the first data node
13NodeId=2			# Node ID for this data node
14datadir=/usr/local/mysql/data	# Remote directory for the data files
15
16[ndbd]
17hostname=198.51.100.1 # Hostname/IP of the second data node
18NodeId=3			# Node ID for this data node
19datadir=/usr/local/mysql/data	# Remote directory for the data files
20
21[mysqld]
22# SQL node options:
23hostname=198.51.100.2 # In our case the MySQL server/client is on the same Droplet as the cluster manager

在插入此文本后,请确保以您配置的 Droplets 的正确 IP 地址替换上述主机名值,设置此主机名参数是防止其他服务器连接到集群管理器的重要安全措施。

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

这是 MySQL 集群的最小配置文件。您应该根据您的生产需求定制此文件中的参数。 对于完全配置的 ndb_mgmd 配置文件的样本,请参阅 MySQL 集群 文档

在上面的文件中,您可以添加额外的组件,如数据节点(ndbd)或MySQL服务器节点(mysqld)通过附加实例到相应的部分。

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

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

你应该看到以下结果:

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

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

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

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

1sudo pkill -f ndb_mgmd

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

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

用以下代码填写:

 1[label /etc/systemd/system/ndb_mgmd.service]
 2[Unit]
 3Description=MySQL NDB Cluster Management Server
 4After=network.target auditd.service
 5
 6[Service]
 7Type=forking
 8ExecStart=/usr/sbin/ndb_mgmd -f /var/lib/mysql-cluster/config.ini
 9ExecReload=/bin/kill -HUP $MAINPID
10KillMode=process
11Restart=on-failure
12
13[Install]
14WantedBy=multi-user.target

在这里,我们添加了一组最小的选项,指示 systemd 如何启动、停止和重新启动 `ndb_mgmd' 过程. 有关此单位配置中使用的选项,请参阅 systemd 手册

保存并关闭文件。

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

1sudo systemctl daemon-reload

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

1sudo systemctl enable ndb_mgmd

最后,我们将开始服务:

1sudo systemctl start ndb_mgmd

您可以验证 NDB 集群管理服务正在运行:

1sudo systemctl status ndb_mgmd

你应该看到以下结果:

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

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

设置集群管理器的最后一步是允许从私人网络上的其他MySQL集群节点接入连接。

如果您在设置此 Droplet 时没有配置ufw防火墙,则可以跳到下一节。

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

1sudo ufw allow from 198.51.100.0
2sudo ufw allow from 198.51.100.1

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

1[secondary_label Output]
2Rule added

集群管理器现在应该处于运行状态,并能够通过私人网络与其他集群节点进行通信。

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

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

在此步骤中,我们将安装ndbd MySQL 集群数据节点对象,并配置节点,以便它们可以与集群管理器进行通信。

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

从此页面,在 选择操作系统 下,选择 ** Ubuntu Linux** .然后,在 ** 选择 OS 版本** 下,选择 ** Ubuntu Linux 18.04 (x86, 64 位)** 。

向下滚动,直到你看到 DEB 包,NDB 数据节点二进制 ,然后点击 ** 下载** 链接,其中没有 ** 不包含 dbgsym(除非你需要调试符号)。你将被带到一个 ** 开始下载** 页面。

现在,登录您的第一个数据节点Droplet(在本教程中,198.51.100.0),然后下载这个deb文件:

1[environment second]
2cd ~
3wget 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’:

1[environment second]
2sudo apt update
3sudo apt install libclass-methodmaker-perl

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

1[environment second]
2sudo dpkg -i mysql-cluster-community-data-node_7.6.6-1ubuntu18.04_amd64.deb

数据节点将其配置从MySQL的标准位置, /etc/my.cnf. 使用您最喜欢的文本编辑器创建此文件并开始编辑:

1[environment second]
2sudo nano /etc/my.cnf

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

1[label  /etc/my.cnf]
2[environment second]
3[mysql_cluster]
4# Options for NDB Cluster processes:
5ndb-connectstring=198.51.100.2  # location of cluster manager

指定集群管理器节点的位置是ndbd启动所需的唯一配置,其余的配置将直接从管理器中拉出。

保存和退出文件。

在我们的示例中,数据节点会发现其数据目录为/usr/local/mysql/data,根据管理员的配置。

1[environment second]
2sudo mkdir -p /usr/local/mysql/data

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

1[environment second]
2sudo ndbd

你应该看到以下结果:

1[secondary_label Output]
2[environment second]
32018-07-18 19:48:21 [ndbd] INFO     -- Angel connected to '198.51.100.2:1186'
42018-07-18 19:48:21 [ndbd] INFO     -- Angel allocated nodeid: 2

NDB 数据节点 DAEMON 已成功安装,现在在您的服务器上运行。

我们还需要允许通过私人网络从其他MySQL集群节点接入连接。

如果您在设置此 Droplet 时没有配置ufw防火墙,您可以跳过设置ndbd的 systemd 服务。

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

1[environment second]
2sudo ufw allow from 198.51.100.0
3sudo ufw allow from 198.51.100.2

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

1[secondary_label Output]
2[environment second]
3Rule added

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

最后,我们还希望数据节点大门在服务器启动时自动启动,我们将遵循用于集群管理器的相同程序,并创建一个 systemd 服务。

在我们创建服务之前,我们将杀死运行的ndbd过程:

1[environment second]
2sudo pkill -f ndbd

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

1[environment second]
2sudo nano /etc/systemd/system/ndbd.service

用以下代码填写:

 1[label /etc/systemd/system/ndbd.service]
 2[environment second]
 3[Unit]
 4Description=MySQL NDB Data Node Daemon
 5After=network.target auditd.service
 6
 7[Service]
 8Type=forking
 9ExecStart=/usr/sbin/ndbd
10ExecReload=/bin/kill -HUP $MAINPID
11KillMode=process
12Restart=on-failure
13
14[Install]
15WantedBy=multi-user.target

在这里,我们添加了一组最小的选项,指示系统d如何启动、停止和重新启动ndbd过程. 有关此单位配置中使用的选项,请参阅系统d 手册

保存并关闭文件。

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

1[environment second]
2sudo systemctl daemon-reload

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

1[environment second]
2sudo systemctl enable ndbd

最后,我们将开始服务:

1[environment second]
2sudo systemctl start ndbd

您可以验证 NDB 集群管理服务正在运行:

1[environment second]
2sudo systemctl status ndbd

你应该看到以下结果:

 1[environment second]
 2[secondary_label Output]
 3 ndbd.service - MySQL NDB Data Node Daemon
 4   Loaded: loaded (/etc/systemd/system/ndbd.service; enabled; vendor preset: enabled)
 5   Active: active (running) since Thu 2018-07-26 20:56:29 UTC; 8s ago
 6  Process: 11972 ExecStart=/usr/sbin/ndbd (code=exited, status=0/SUCCESS)
 7 Main PID: 11984 (ndbd)
 8    Tasks: 46 (limit: 4915)
 9   CGroup: /system.slice/ndbd.service
10           ├─11984 /usr/sbin/ndbd
11           └─11987 /usr/sbin/ndbd

这表明ndbd MySQL 集群数据节点对象现在作为 systemd 服务运行,您的数据节点现在应该完全功能,并能够连接到 MySQL 集群管理器。

一旦您完成了第一个数据节点的设置,请重复本节中的其他数据节点的步骤(本教程中的 198.51.100.1)。

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

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

我们将再次从官方MySQL集群(下载页面)(http://dev.mysql.com/downloads/cluster/)中获取MySQL集群服务器二进制。

从此页面,在 选择操作系统 下,选择 ** Ubuntu Linux** .然后,在 ** 选择 OS 版本** 下,选择 ** Ubuntu Linux 18.04 (x86, 64 位)** 。

向下滚动,直到你看到 DEB Bundle ,然后点击 ** Download** 链接(它应该是列表中的第一个)。你将被带到一个 ** Begin Your Download** 页面。

现在,登录到 Cluster Manager Droplet (在本教程中, 198.51.100.2),并下载这个 .tar 档案(请记住,我们在与我们的 Cluster Manager 相同的节点上安装 MySQL Server - 在生产设置中,您应该在不同的节点上运行这些 daemons):

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

我们现在将此档案提取到名为安装的目录中,首先创建目录:

1mkdir install

现在将档案提取到此目录中:

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

进入此目录,其中包含提取的 MySQL 集群组件二进制:

1cd install

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

1sudo apt update
2sudo apt install libaio1 libmecab2

现在,我们需要安装 MySQL 集群依赖,并在我们刚刚提取的tar档案中:

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

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

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

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

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

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

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

1sudo nano /etc/mysql/my.cnf

你应该看到以下文本:

 1[label /etc/mysql/my.cnf]
 2# Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
 3#
 4# This program is free software; you can redistribute it and/or modify
 5# it under the terms of the GNU General Public License as published by
 6# the Free Software Foundation; version 2 of the License.
 7#
 8# This program is distributed in the hope that it will be useful,
 9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program; if not, write to the Free Software
15# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16
17#
18# The MySQL Cluster Community Server configuration file.
19#
20# For explanations see
21# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
22
23# * IMPORTANT: Additional settings that can override those from this file!
24#   The files must end with '.cnf', otherwise they'll be ignored.
25#
26!includedir /etc/mysql/conf.d/
27!includedir /etc/mysql/mysql.conf.d/

将以下配置附加到它:

1[label /etc/mysql/my.cnf]
2. . .
3[mysqld]
4# Options for mysqld process:
5ndbcluster                      # run NDB storage engine
6
7[mysql_cluster]
8# Options for NDB Cluster processes:
9ndb-connectstring=198.51.100.2  # location of management server

保存和退出文件。

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

1sudo systemctl restart mysql

默认情况下,MySQL 应该在重新启动服务器时自动启动,如果没有,以下命令应该修复此问题:

1sudo systemctl enable mysql

SQL 服务器现在应该在您的 Cluster Manager / MySQL Server Droplet 上运行。

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

第4步:验证MySQL集群安装

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

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

1mysql -u root -p

请在提示时输入您的密码,然后点击ENTER

你应该看到一个类似的输出:

 1[secondary_label Output]
 2Welcome to the MySQL monitor. Commands end with ; or \g.
 3Your MySQL connection id is 3
 4Server version: 5.7.22-ndb-7.6.6 MySQL Cluster Community Server (GPL)
 5
 6Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
 7
 8Oracle is a registered trademark of Oracle Corporation and/or its
 9affiliates. Other names may be trademarks of their respective
10owners.
11
12Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
13
14mysql>

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

1SHOW ENGINE NDB STATUS \G

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

1[secondary_label Output]
2
3*************************** 1. row ***************************
4  Type: ndbcluster
5  Name: connection
6Status: 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
7. . .

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

请注意这里的ready_data_nodes的数量: 2. 这种冗余性允许您的MySQL集群即使其中一个数据节点失败,也能继续运作。

您可以尝试关闭其中一个数据节点来测试集群稳定性. 最简单的测试是重新启动数据节点Droplet,以便完全测试恢复过程。

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

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

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

1ndb_mgm

你应该看到以下结果:

1[secondary_label Output]
2-- NDB Cluster -- Management Client --
3ndb_mgm>

一旦进入控制台,请输入SHOW命令并按ENTER:

1SHOW

你应该看到以下结果:

 1[secondary_label Output]
 2Connected to Management Server at: 198.51.100.2:1186
 3Cluster Configuration
 4---------------------
 5[ndbd(NDB)]	2 node(s)
 6id=2	@198.51.100.0  (mysql-5.7.22 ndb-7.6.6, Nodegroup: 0, *)
 7id=3	@198.51.100.1  (mysql-5.7.22 ndb-7.6.6, Nodegroup: 0)
 8
 9[ndb_mgmd(MGM)]	1 node(s)
10id=1	@198.51.100.2  (mysql-5.7.22 ndb-7.6.6)
11
12[mysqld(API)]	1 node(s)
13id=4	@198.51.100.2  (mysql-5.7.22 ndb-7.6.6)

上面的数据表明,有两个数据节点连接到node-id 2 和 3。还有一个管理节点与node-id 1 和一个 MySQL 服务器与node-id 4。

12 STATUS

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

1[secondary_label Output]
2Node 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.

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:

1CREATE DATABASE clustertest;

Next, switch to the new database:

1USE clustertest;

Now, create a simple table called test_table like this:

1CREATE 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:

1INSERT INTO test_table (name,value) VALUES('some_name','some_value');

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

1SELECT * 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.

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.

Published At
Categories with 技术
comments powered by Disqus