Clustering adds high availability to your database by distributing changes to different servers. In the event that one of the instances fails, others are quickly available to continue serving.
群集通过将更改分发到不同的服务器来增加数据库的高可用性。 万一其中一个实例发生故障,其他实例很快就可以继续使用。
Clusters come in two general configurations, active-passive and active-active. In active-passive clusters, all writes are done on a single active server and then copied to one or more passive servers that are poised to take over only in the event of an active server failure. Some active-passive clusters also allow SELECT
operations on passive nodes. In an active-active cluster, every node is read-write and a change made to one is replicated to all.
群集有两种常规配置, 主动-被动和主动-主动 。 在主动-被动群集中,所有写操作均在一台主动服务器上完成,然后复制到一个或多个被动服务器上,这些服务器准备仅在主动服务器发生故障时接管。 一些主动-被动群集还允许在被动节点上执行SELECT
操作。 在双活群集中,每个节点都是可读写的,并且对一个节点所做的更改将被复制到所有节点。
MariaDB is an open source relational database system that is fully compatible with the popular MySQL RDBMS system. You can read the official documentation for MariaDB at this page. Galera is a database clustering solution that enables you to set up multi-master clusters using synchronous replication. Galera automatically handles keeping the data on different nodes in sync while allowing you to send read and write queries to any of the nodes in the cluster. You can learn more about Galera at the official documentation page.
MariaDB是一个开放源代码关系数据库系统,与流行MySQL RDBMS系统完全兼容。 您可以在此页面上阅读MariaDB的官方文档。 Galera是一种数据库群集解决方案,使您可以使用同步复制来设置多主群集。 Galera自动处理使不同节点上的数据保持同步的过程,同时允许您向集群中的任何节点发送读写查询。 您可以在官方文档页面上了解有关Galera的更多信息。
In this guide, you will configure an active-active MariaDB Galera cluster. For demonstration purposes, you will configure and test three Ubuntu 18.04 Droplets that will act as nodes in the cluster. This is the smallest configurable cluster.
在本指南中,您将配置一个主动-主动MariaDB Galera集群。 出于演示目的,您将配置和测试三个将充当群集中节点的Ubuntu 18.04 Droplet。 这是最小的可配置群集。
To follow along, you will need a DigitalOcean account, in addition to the following:
要继续进行下去 ,除了以下内容之外,您还需要一个DigitalOcean帐户 :
For setting up private networking on the three Droplets, follow our Private Networking Quickstart guide.
要在三个Droplet上设置专用网络,请遵循我们的专用网络快速入门指南。
For assistance setting up a non-root user with sudo
privileges, follow our Initial Server Setup with Ubuntu 18.04 tutorial.
要获得设置具有sudo
特权的非root用户的帮助,请遵循我们的Ubuntu 18.04初始服务器设置教程。
Three Ubuntu 18.04 Droplets with private networking enabled, each with a non-root user with sudo
privileges.
三个启用了私有网络的Ubuntu 18.04 Droplet,每个Droplet都具有一个具有sudo
特权的非root用户。
While the steps in this tutorial have been written for and tested against DigitalOcean Droplets, much of them should also be applicable to non-DigitalOcean servers with private networking enabled.
尽管本教程中的步骤是针对DigitalOcean Droplet编写的,并已针对它们进行了测试,但其中许多步骤也应适用于启用了专用网络的非DigitalOcean服务器。
In this step, you will add the relevant MariaDB package repositories to each of your three servers so that you will be able to install the right version of MariaDB used in this tutorial. Once the repositories are updated on all three servers, you will be ready to install MariaDB.
在此步骤中,您将相关的MariaDB软件包存储库添加到三个服务器中的每一个中,以便能够安装本教程中使用的正确版本的MariaDB。 一旦所有三台服务器上的存储库都更新,您就可以安装MariaDB了。
One thing to note about MariaDB is that it originated as a drop-in replacement for MySQL, so in many configuration files and startup scripts, you’ll see mysql
rather than mariadb
. For consistency’s sake, we will use mysql
in this guide where either could work.
关于MariaDB的一件事要注意,它最初是MySQL的替代产品,因此在许多配置文件和启动脚本中,您将看到mysql
而不是mariadb
。 为了保持一致性,我们将在本指南中使用mysql
进行工作。
In this tutorial, you will use MariaDB version 10.4. Since this version isn’t included in the default Ubuntu repositories, you’ll start by adding the external Ubuntu repository maintained by the MariaDB project to all three of your servers.
在本教程中,您将使用MariaDB 10.4版 。 由于此版本未包含在默认的Ubuntu存储库中,因此您首先需要将由MariaDB项目维护的外部Ubuntu存储库添加到所有三个服务器中。
Note: MariaDB is a well-respected provider, but not all external repositories are reliable. Be sure to install only from trusted sources.
注意: MariaDB是受人尊敬的提供者,但并非所有外部存储库都是可靠的。 确保仅从受信任的来源进行安装。
First, you’ll add the MariaDB repository key with the apt-key
command, which the APT package manager will use to verify that the package is authentic:
首先,您将使用apt-key
命令添加MariaDB存储库密钥,APT软件包管理器将使用该命令来验证软件包是真实的:
Once you have the trusted key in the database, you can add the repository with the following command:
一旦在数据库中有了可信密钥,就可以使用以下命令添加存储库:
After adding the repository, run apt update
in order to include package manifests from the new repository:
添加存储库后,运行apt update
以便包含来自新存储库的软件包清单:
Once you have completed this step on your first server, repeat for your second and third servers.
在第一台服务器上完成此步骤后,请对第二台和第三台服务器重复上述步骤。
Now that you have successfully added the package repository on all three of your servers, you’re ready to install MariaDB in the next section.
既然您已经成功将软件包存储库添加到了所有三台服务器上,那么就可以在下一部分中安装MariaDB了。
In this step, you will install the actual MariaDB packages on your three servers.
在此步骤中,您将在三个服务器上安装实际的MariaDB软件包。
Beginning with version 10.1
, the MariaDB Server and MariaDB Galera Server packages are combined, so installing mariadb-server
will automatically install Galera and several dependencies:
从版本10.1
开始,MariaDB Server和MariaDB Galera Server软件包组合在一起,因此安装mariadb-server
将自动安装Galera和一些依赖项:
You will be asked to confirm whether you would like to proceed with the installation. Enter yes
to continue with the installation.
系统将要求您确认是否要继续安装。 输入yes
继续安装。
From MariaDB version 10.4
onwards, the root MariaDB user does not have a password by default. To set a password for the root user, start by logging into MariaDB:
从MariaDB 10.4
版开始,默认情况下, root MariaDB用户没有密码。 要为root用户设置密码,请先登录MariaDB:
Once you’re inside the MariaDB shell, change the password by executing the following statement:
进入MariaDB shell之后,通过执行以下语句更改密码:
set password = password("your_password");
设置密码=密码(“ your_password ”);
You will see the following output indicating that the password was set correctly:
您将看到以下输出,表明密码设置正确:
Output
Query OK, 0 rows affected (0.001 sec)
Exit the MariaDB shell by running the following command:
通过运行以下命令退出MariaDB Shell:
If you would like to learn more about SQL or need a quick refresher, check out our MySQL tutorial.
如果您想了解有关SQL的更多信息或需要快速复习,请查看我们的MySQL教程 。
You now have all of the pieces necessary to begin configuring the cluster, but since you’ll be relying on rsync
in later steps, make sure it’s installed:
现在,您具有开始配置集群所需的所有步骤,但是由于在以后的步骤中将依赖rsync
,因此请确保已安装它:
This will confirm that the newest version of rsync
is already available or prompt you to upgrade or install it.
这将确认最新版本的rsync
已经可用或提示您升级或安装它。
Once you have installed MariaDB and set the root password on your first server, repeat these steps for your other two servers.
安装MariaDB并在第一台服务器上设置root密码后,对其他两台服务器重复上述步骤。
Now that you have installed MariaDB successfully on each of the three servers, you can proceed to the configuration step in the next section.
既然您已经在三台服务器上成功安装了MariaDB,则可以继续下一节中的配置步骤。
In this step you will configure your first node. Each node in the cluster needs to have a nearly identical configuration. Because of this, you will do all of the configuration on your first machine, and then copy it to the other nodes.
在此步骤中,您将配置第一个节点。 集群中的每个节点都需要具有几乎相同的配置。 因此,您将在第一台计算机上进行所有配置,然后将其复制到其他节点。
By default, MariaDB is configured to check the /etc/mysql/conf.d
directory to get additional configuration settings from files ending in .cnf
. Create a file in this directory with all of your cluster-specific directives:
默认情况下,MariaDB配置为检查/etc/mysql/conf.d
目录,以从以.cnf
结尾的文件中获取其他配置设置。 使用所有特定于集群的指令在此目录中创建文件:
Add the following configuration into the file. The configuration specifies different cluster options, details about the current server and the other servers in the cluster, and replication-related settings. Note that the IP addresses in the configuration are the private addresses of your respective servers; replace the highlighted lines with the appropriate IP addresses.
将以下配置添加到文件中。 该配置指定不同的集群选项,有关集群中当前服务器和其他服务器的详细信息以及与复制相关的设置。 请注意,配置中的IP地址是您各自服务器的专用地址。 用适当的IP地址替换突出显示的行。
[mysqld]
binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
bind-address=0.0.0.0
# Galera Provider Configuration
wsrep_on=ON
wsrep_provider=/usr/lib/galera/libgalera_smm.so
# Galera Cluster Configuration
wsrep_cluster_name="test_cluster"
wsrep_cluster_address="gcomm://First_Node_IP,Second_Node_IP,Third_Node_IP"
# Galera Synchronization Configuration
wsrep_sst_method=rsync
# Galera Node Configuration
wsrep_node_address="This_Node_IP"
wsrep_node_name="This_Node_Name"
The first section modifies or re-asserts MariaDB/MySQL settings that will allow the cluster to function correctly. For example, Galera won’t work with MyISAM or similar non-transactional storage engines, and mysqld
must not be bound to the IP address for localhost
. You can learn about the settings in more detail on the Galera Cluster system configuration page.
第一部分修改或重新声明MariaDB / MySQL设置,这些设置将允许集群正常运行。 例如,Galera不能与MyISAM或类似的非事务性存储引擎一起使用,并且mysqld
不得绑定到localhost
的IP地址。 您可以在Galera Cluster 系统配置页面上详细了解设置。
The “Galera Provider Configuration” section configures the MariaDB components that provide a WriteSet replication API. This means Galera in your case, since Galera is a wsrep (WriteSet Replication) provider. You specify the general parameters to configure the initial replication environment. This doesn’t require any customization, but you can learn more about Galera configuration options.
“ Galera Provider Configuration”部分配置了提供WriteSet复制API的MariaDB组件。 在您的情况下,这意味着Galera,因为Galera是wsrep (WriteSet复制)提供程序。 您可以指定常规参数来配置初始复制环境。 这不需要任何自定义,但是您可以了解有关Galera配置选项的更多信息。
The “Galera Cluster Configuration” section defines the cluster, identifying the cluster members by IP address or resolvable domain name and creating a name for the cluster to ensure that members join the correct group. You can change the wsrep_cluster_name
to something more meaningful than test_cluster
or leave it as-is, but you must update wsrep_cluster_address
with the private IP addresses of your three servers.
“ Galera群集配置”部分定义群集,通过IP地址或可解析的域名标识群集成员,并为群集创建名称以确保成员加入正确的组。 您可以将wsrep_cluster_name
更改为比test_cluster
更有意义的test_cluster
或将其保留test_cluster
,但必须使用三个服务器的专用IP地址更新wsrep_cluster_address
。
The “Galera Synchronization Configuration” section defines how the cluster will communicate and synchronize data between members. This is used only for the state transfer that happens when a node comes online. For your initial setup, you are using rsync
, because it’s commonly available and does what you’ll need for now.
“ Galera同步配置”部分定义了群集如何在成员之间通信和同步数据。 这仅用于节点联机时发生的状态转移。 对于您的初始设置,您正在使用rsync
,因为它通常可用,并且可以满足您目前的需求。
The “Galera Node Configuration” section clarifies the IP address and the name of the current server. This is helpful when trying to diagnose problems in logs and for referencing each server in multiple ways. The wsrep_node_address
must match the address of the machine you’re on, but you can choose any name you want in order to help you identify the node in log files.
“ Galera节点配置”部分阐明了当前服务器的IP地址和名称。 在尝试诊断日志中的问题以及以多种方式引用每个服务器时,这很有用。 wsrep_node_address
必须与您所在计算机的地址匹配,但是您可以选择所需的任何名称,以帮助您在日志文件中标识该节点。
When you are satisfied with your cluster configuration file, copy the contents into your clipboard, save and close the file. With the nano text editor, you can do this by pressing CTRL+X
, typing y
, and pressing ENTER
.
对群集配置文件满意后,将内容复制到剪贴板中,保存并关闭文件。 使用nano文本编辑器,可以通过按CTRL+X
,键入y
并按ENTER
来执行此操作。
Now that you have configured your first node successfully, you can move on to configuring the remaining nodes in the next section.
现在您已经成功配置了第一个节点,接下来可以继续配置其余节点。
In this step, you will configure the remaining two nodes. On your second node, open the configuration file:
在此步骤中,您将配置其余两个节点。 在第二个节点上,打开配置文件:
Paste in the configuration you copied from the first node, then update the Galera Node Configuration
to use the IP address or resolvable domain name for the specific node you’re setting up. Finally, update its name, which you can set to whatever helps you identify the node in your log files:
粘贴您从第一个节点复制的配置,然后更新Galera Node Configuration
以将IP地址或可解析的域名用于您要设置的特定节点。 最后,更新其名称,您可以将其设置为可帮助您在日志文件中标识节点的任何名称:
. . .
# Galera Node Configuration
wsrep_node_address="This_Node_IP"
wsrep_node_name="This_Node_Name"
. . .
Save and exit the file.
保存并退出文件。
Once you have completed these steps, repeat them on the third node.
完成这些步骤后,请在第三个节点上重复这些步骤。
You’re almost ready to bring up the cluster, but before you do, make sure that the appropriate ports are open in your firewall.
您几乎已经准备好启动群集,但是在执行此操作之前,请确保在防火墙中打开了适当的端口。
In this step, you will configure your firewall so that the ports required for inter-node communication are open. On every server, check the status of the firewall by running:
在此步骤中,您将配置防火墙,以便打开节点间通信所需的端口。 在每台服务器上,通过运行以下命令检查防火墙的状态:
In this case, only SSH is allowed through:
在这种情况下,仅允许通过以下方式使用SSH:
Output
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Since only SSH traffic is permitted in this case, you’ll need to add rules for MySQL and Galera traffic. If you tried to start the cluster, it would fail because of firewall rules.
由于在这种情况下仅允许SSH通信,因此您需要为MySQL和Galera通信添加规则。 如果尝试启动群集,则由于防火墙规则,它将失败。
Galera can make use of four ports:
Galera可以使用四个端口:
3306
For MySQL client connections and State Snapshot Transfer that use the mysqldump
method.
3306
对于使用mysqldump
方法MySQL客户端连接和状态快照传输。
4567
For Galera Cluster replication traffic. Multicast replication uses both UDP transport and TCP on this port.
4567
对于Galera Cluster复制通信。 组播复制在此端口上同时使用UDP传输和TCP。
4568
For Incremental State Transfer.
4568
用于增量状态转移。
4444
For all other State Snapshot Transfer.
4444
对于所有其他状态快照传输。
In this example, you’ll open all four ports while you do your setup. Once you’ve confirmed that replication is working, you’d want to close any ports you’re not actually using and restrict traffic to just servers in the cluster.
在此示例中,您将在设置时打开所有四个端口。 确认复制正常后,您将要关闭所有未使用的端口,并将流量限制为仅在群集中的服务器。
Open the ports with the following command:
使用以下命令打开端口:
Note: Depending on what else is running on your servers you might want to restrict access right away. The UFW Essentials: Common Firewall Rules and Commands guide can help with this.
注意:根据服务器上其他正在运行的内容,您可能希望立即限制访问。 UFW Essentials:“通用防火墙规则和命令”指南可以提供帮助。
After you have configured your firewall on the first node, create the same firewall settings on the second and third node.
在第一个节点上配置防火墙之后,在第二个和第三个节点上创建相同的防火墙设置。
Now that you have configured the firewalls successfully, you’re ready to start the cluster in the next step.
既然您已经成功配置了防火墙,那么就可以在下一步中启动集群了。
In this step, you will start your MariaDB cluster. To begin, you need to stop the running MariaDB service so that you can bring your cluster online.
在此步骤中,您将启动MariaDB集群。 首先,您需要停止正在运行的MariaDB服务,以便可以使群集联机。
Use the following command on all three servers to stop MariaDB so that you can bring them back up in a cluster:
在所有三台服务器上使用以下命令来停止MariaDB,以便可以将它们备份到群集中:
systemctl
doesn’t display the outcome of all service management commands, so to be sure you succeeded, use the following command:
systemctl
不会显示所有服务管理命令的结果,因此为确保成功,请使用以下命令:
If the last line looks something like the following, the command was successful:
如果最后一行如下所示,则命令成功:
Output
. . .
Apr 26 03:34:23 galera-node-01 systemd[1]: Stopped MariaDB 10.4.4 database server.
Once you’ve shut down mysql
on all of the servers, you’re ready to proceed.
一旦关闭了所有服务器上的mysql
,就可以继续了。
To bring up the first node, you’ll need to use a special startup script. The way you’ve configured your cluster, each node that comes online tries to connect to at least one other node specified in its galera.cnf
file to get its initial state. Without using the galera_new_cluster
script that allows systemd to pass the --wsrep-new-cluster
parameter, a normal systemctl start mysql
would fail because there are no nodes running for the first node to connect with.
要启动第一个节点,您将需要使用特殊的启动脚本。 按照配置群集的方式,每个联机节点都会尝试连接到其galera.cnf
文件中指定的至少另一个节点,以获取其初始状态。 如果不使用允许systemd传递--wsrep-new-cluster
参数的galera_new_cluster
脚本,则正常的systemctl start mysql
将失败,因为第一个节点没有正在运行的节点可与之连接。
This command will not display any output on successful execution. When this script succeeds, the node is registered as part of the cluster, and you can see it with the following command:
成功执行后,此命令将不显示任何输出。 成功执行此脚本后,该节点将注册为集群的一部分,您可以使用以下命令查看它:
You will see the following output indicating that there is one node in the cluster:
您将看到以下输出,指示集群中有一个节点:
Output
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 1 |
+--------------------+-------+
On the remaining nodes, you can start mysql
normally. They will search for any member of the cluster list that is online, so when they find one, they will join the cluster.
在其余节点上,您可以正常启动mysql
。 他们将搜索群集列表中任何在线的成员,因此当他们找到一个成员时,他们将加入群集。
Now you can bring up the second node. Start mysql
:
现在可以启动第二个节点。 启动mysql
:
No output will be displayed on successful execution. You will see your cluster size increase as each node comes online:
成功执行后将不显示任何输出。 随着每个节点联机,您将看到群集大小增加:
You will see the following output indicating that the second node has joined the cluster and that there are two nodes in total.
您将看到以下输出,指示第二个节点已加入集群,并且总共有两个节点。
Output
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 2 |
+--------------------+-------+
It’s now time to bring up the third node. Start mysql
:
现在是时候提出第三个节点。 启动mysql
:
Run the following command to find the cluster size:
运行以下命令以查找集群大小:
You will see the following output, which indicates that the third node has joined the cluster and that the total number nodes in the cluster is three.
您将看到以下输出,该输出指示第三个节点已加入集群,并且集群中的节点总数为三个。
Output
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 3 |
+--------------------+-------+
At this point, the entire cluster is online and communicating successfully. Next, you can ensure the working setup by testing replication in the next section.
此时,整个群集都已联机并成功通信。 接下来,您可以通过在下一部分中测试复制来确保工作设置。
You’ve gone through the steps up to this point so that your cluster can perform replication from any node to any other node, known as active-active replication. Follow the steps below to test and see if the replication is working as expected.
至此,您已经完成了所有步骤,以便您的集群可以执行从任何节点到任何其他节点的复制,这称为主动-主动复制。 请按照以下步骤进行测试,以查看复制是否按预期工作。
You’ll start by making database changes on your first node. The following commands will create a database called playground
and a table inside of this database called equipment
.
您将从在第一个节点上进行数据库更改开始。 以下命令将创建一个名为“ playground
的数据库,并在该数据库内部创建一个名为equipment
的表。
In the previous command, the CREATE DATABASE
statement creates a database named playground
. The CREATE
statement creates a table named equipment
inside the playground
database having an auto-incrementing identifier column called id
and other columns. The type
column, quant
column, and color
column are defined to store the type, quantity, and color of the equipment respectively. The INSERT
statement inserts an entry of type slide
, quantity 2
and color blue
.
在上一个命令中, CREATE DATABASE
语句创建一个名为playground
的数据库。 CREATE
语句在playground
数据库内创建一个名为equipment
的表,该表具有一个称为id
的自动递增标识符列和其他列。 的type
栏, quant
列和color
柱被定义为分别存储类型,数量,和设备的颜色。 INSERT
语句将插入一个类型为slide
,数量2
和blue
的条目。
You now have one value in your table.
现在,表中有一个值。
Next, look at the second node to verify that replication is working:
接下来,查看第二个节点以验证复制是否有效:
If replication is working, the data you entered on the first node will be visible here on the second:
如果复制正常,则您在第一个节点上输入的数据将在第二个节点上可见:
Output
+----+-------+-------+-------+
| id | type | quant | color |
+----+-------+-------+-------+
| 1 | slide | 2 | blue |
+----+-------+-------+-------+
From this same node, you can write data to the cluster:
从同一节点,您可以将数据写入集群:
From the third node, you can read all of this data by querying the table again:
从第三个节点,您可以通过再次查询表来读取所有这些数据:
You will see the following output showing the two rows:
您将看到以下输出,显示两行:
Output
+----+-------+-------+--------+
| id | type | quant | color |
+----+-------+-------+--------+
| 1 | slide | 2 | blue |
| 2 | swing | 10 | yellow |
+----+-------+-------+--------+
Again, you can add another value from this node:
同样,您可以从该节点添加另一个值:
Back on the first node, you can verify that your data is available everywhere:
回到第一个节点,您可以验证数据在任何地方都可用:
You will see the following output which indicates that the rows are available on the first node.
您将看到以下输出,指示行在第一个节点上可用。
Output
+----+--------+-------+--------+
| id | type | quant | color |
+----+--------+-------+--------+
| 1 | slide | 2 | blue |
| 2 | swing | 10 | yellow |
| 3 | seesaw | 3 | green |
+----+--------+-------+--------+
You’ve verified successfully that you can write to all of the nodes and that replication is being performed properly.
您已经成功验证可以写入所有节点并且复制正在正确执行。
At this point, you have a working three-node Galera test cluster configured. If you plan on using a Galera cluster in a production situation, it’s recommended that you begin with no fewer than five nodes.
至此,您已经配置了一个有效的三节点Galera测试集群。 如果计划在生产环境中使用Galera群集,则建议您至少从五个节点开始。
Before production use, you may want to take a look at some of the other state snapshot transfer (sst) agents like xtrabackup, which allows you to set up new nodes very quickly and without large interruptions to your active nodes. This does not affect the actual replication, but is a concern when nodes are being initialized.
在生产环境投入使用之前,您可能需要看一下其他一些状态快照传输(sst)代理,例如xtrabackup ,它使您可以非常快速地设置新节点,而不会对活动节点造成大的干扰。 这不会影响实际的复制,但是在初始化节点时会引起关注。
You might also be interested in other clustering solutions such as MySQL cluster, in which case you can check out our tutorial How To Create a Multi-Node MySQL Cluster on Ubuntu 18.04 tutorial.
您可能还对其他群集解决方案(例如MySQL群集)感兴趣,在这种情况下,您可以查看我们的教程如何在Ubuntu 18.04上创建多节点MySQL群集教程。
翻译自: https://www.digitalocean.com/community/tutorials/how-to-configure-a-galera-cluster-with-mariadb-on-ubuntu-18-04-servers