如何在Ubuntu 18.04服务器上使用MySQL配置Galera群集

news/2024/7/7 10:05:52

介绍 (Introduction)

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操作。 在双活群集中,每个节点都是可读写的,并且对一个节点所做的更改将被复制到所有节点。

MySQL is an open source relational database management system that is a popular choice for SQL databases. 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.

MySQL是一种开放源代码关系数据库管理系统,是SQL数据库的流行选择。 Galera是一种数据库群集解决方案,使您可以使用同步复制来设置多主群集。 Galera自动处理使不同节点上的数据保持同步的过程,同时允许您向集群中的任何节点发送读写查询。 您可以在官方文档页面上了解有关Galera的更多信息。

In this guide, you will configure an active-active MySQL Galera cluster. For demonstration purposes, you will configure and test three Ubuntu 18.04 Droplets that will act as nodes in the cluster. This amount of nodes is the smallest configurable cluster.

在本指南中,您将配置双活MySQL Galera集群。 出于演示目的,您将配置和测试三个将充当群集中节点的Ubuntu 18.04 Droplet 。 此数量的节点是最小的可配置群集。

先决条件 (Prerequisites)

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 will also be applicable to non-DigitalOcean servers with private networking enabled.

尽管本教程中的步骤是针对DigitalOcean Droplet编写的,并已针对它们进行了测试,但其中许多步骤也将适用于启用了专用网络的非DigitalOcean服务器。

第1步-将MySQL存储库添加到所有服务器 (Step 1 — Adding the MySQL Repositories to All Servers)

In this step, you will add the relevant MySQL and Galera package repositories to each of your three servers so that you will be able to install the right version of MySQL and Galera used in this tutorial.

在此步骤中,您将向三个服务器中的每一个添加相关MySQL和Galera软件包存储库,以便能够安装本教程中使用的正确版本MySQL和Galera。

Note: Codership, the company behind Galera Cluster, maintains the Galera repository, but be aware that not all external repositories are reliable. Be sure to install only from trusted sources.

注意: Galera Cluster背后的公司Codership负责维护Galera存储库,但请注意,并非所有外部存储库都是可靠的。 确保仅从受信任的来源进行安装。

In this tutorial, you will use MySQL version 5.7. You’ll start by adding the external Ubuntu repository maintained by the Galera project to all three of your servers.

在本教程中,您将使用MySQL 5.7版 。 首先,将Galera项目维护的外部Ubuntu存储库添加到所有三个服务器中。

Once the repositories are updated on all three servers, you will be ready to install MySQL along with Galera.

一旦在所有三台服务器上更新了存储库,您就可以与Galera一起安装MySQL。

First, on all three of your servers, add the Galera repository key with the apt-key command, which the APT package manager will use to verify that the package is authentic:

首先,在您的所有三台服务器上,使用apt-key命令添加Galera存储库密钥,APT软件包管理器将使用该命令来验证软件包是真实的:

  • sudo apt-key adv --keyserver keyserver.ubuntu.com --recv BC19DDBA

    须藤apt-key adv --keyserver keyserver.ubuntu.com --recv BC19DDBA

After a few seconds, you will receive the following output:

几秒钟后,您将收到以下输出:


   
Output
Executing: /tmp/apt-key-gpghome.RG5cTZjQo0/gpg.1.sh --keyserver keyserver.ubuntu.com --recv BC19DDBA gpg: key D669017EBC19DDBA: public key "Codership Oy <info@galeracluster.com>" imported gpg: Total number processed: 1 gpg: imported: 1

Once you have the trusted key in each server’s database, you can add the repositories. To do so, create a new file called galera.list within the /etc/apt/sources.list.d/ directory on each server:

在每个服务器的数据库中拥有受信任的密钥后,就可以添加存储库。 为此,请在每台服务器的/etc/apt/sources.list.d/目录中创建一个名为galera.list的新文件:

  • sudo nano /etc/apt/sources.list.d/galera.list

    须藤nano /etc/apt/sources.list.d/galera.list

In the text editor, add the following lines, which will make the appropriate repositories available to the APT package manager:

在文本编辑器中,添加以下行,这将使APT包管理器可以使用适当的存储库:

/etc/apt/sources.list.d/galera.list
/etc/apt/sources.list.d/galera.list
deb http://releases.galeracluster.com/mysql-wsrep-5.7/ubuntu bionic main
deb http://releases.galeracluster.com/galera-3/ubuntu bionic main

Save and close the files on each server (press CTRL + X, Y, then ENTER).

保存并关闭每个服务器上的文件(按CTRL + XY ,然后按ENTER )。

The Codership repositories are now available to all three of your servers. However, it’s important that you instruct apt to prefer Codership’s repositories over others to ensure that it installs the patched versions of the software needed to create a Galera cluster. To do this, create another new file called galera.pref within the /etc/apt/preferences.d/ directory of each server:

现在,所有三个服务器都可以使用Codership存储库。 但是,重要的是,您必须指示apt使用Codership的存储库,而不是其他存储库,以确保它安装了创建Galera集群所需的软件的修补版本。 为此,请在每个服务器的/etc/apt/preferences.d/目录中创建另一个名为galera.pref新文件:

  • sudo nano /etc/apt/preferences.d/galera.pref

    须藤纳米/etc/apt/preferences.d/galera.pref

Add the following lines to the text editor:

将以下行添加到文本编辑器:

/etc/apt/preferences.d/galera.pref
/etc/apt/preferences.d/galera.pref
# Prefer Codership repository
Package: *
Pin: origin releases.galeracluster.com
Pin-Priority: 1001

Save and close that file, then run the following command on each server in order to include package manifests from the new repositories:

保存并关闭该文件,然后在每台服务器上运行以下命令,以包括来自新存储库的软件包清单:

  • sudo apt update

    sudo apt更新

Now that you have successfully added the package repository on all three of your servers, you’re ready to install MySQL in the next section.

现在,您已经在所有三台服务器上成功添加了软件包存储库,接下来就可以安装MySQL了。

第2步-在所有服务器上安装MySQL (Step 2 — Installing MySQL on All Servers)

In this step, you will install the MySQL package on your three servers.

在此步骤中,您将在三台服务器上安装MySQL软件包。

Run the following command on all three servers to install a version of MySQL patched to work with Galera, as well as the Galera package.

在所有三台服务器上运行以下命令,以安装经修补以与Galera一起使用MySQL版本以及Galera软件包。

  • sudo apt install galera-3 mysql-wsrep-5.7

    sudo apt安装galera-3 mysql-wsrep-5.7

You will be asked to confirm whether you would like to proceed with the installation. Enter Y to continue with the installation. During the installation, you will also be asked to set a password for the MySQL administrative user. Set a strong password and press ENTER to continue.

系统将要求您确认是否要继续安装。 输入Y继续安装。 在安装过程中,还将要求您为MySQL管理用户设置密码。 设置一个强密码,然后按ENTER继续。

Once MySQL is installed, you will disable the default AppArmor profile to ensure that Galera functions properly, as per the official Galera documentation. AppArmor is a kernel module for Linux that provides access control functionality for services through security profiles.

安装MySQL后,您将根据官方Galera文档禁用默认的AppArmor配置文件,以确保Galera正常运行。 AppArmor是Linux的内核模块,通过安全配置文件为服务提供访问控制功能。

Disable AppArmor by executing the following on each server:

通过在每个服务器上执行以下操作来禁用AppArmor:

  • sudo ln -s /etc/apparmor.d/usr.sbin.mysqld /etc/apparmor.d/disable/

    须藤ln -s /etc/apparmor.d/usr.sbin.mysqld /etc/apparmor.d/disable/

This command adds a symbolic link of the MySQL profile to the disable directory, which disables the profile on boot.

此命令将MySQL配置文件的符号链接添加到disable目录,从而在启动时禁用该配置文件。

Then, run the following command to remove the MySQL definition that has already been loaded in the kernel.

然后,运行以下命令以删除内核中已加载MySQL定义。

  • sudo apparmor_parser -R /etc/apparmor.d/usr.sbin.mysqld

    sudo apparmor_parser -R /etc/apparmor.d/usr.sbin.mysqld

Once you have installed MySQL and disabled the AppArmor profile on your first server, repeat these steps for your other two servers.

在安装MySQL并在第一台服务器上禁用AppArmor配置文件后,对其他两台服务器重复这些步骤。

Now that you have installed MySQL successfully on each of the three servers, you can proceed to the configuration step in the next section.

现在,您已经在三台服务器上成功安装了MySQL,接下来可以进行下一节的配置步骤。

步骤3 —配置第一个节点 (Step 3 — Configuring the First Node)

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, MySQL is configured to check the /etc/mysql/conf.d directory to get additional configuration settings from files ending in .cnf. On your first server, create a file in this directory with all of your cluster-specific directives:

默认情况下,MySQL配置为检查/etc/mysql/conf.d目录,以从以.cnf结尾的文件中获取其他配置设置。 在您的第一台服务器上,使用所有特定于集群的指令在此目录中创建一个文件:

  • sudo nano /etc/mysql/conf.d/galera.cnf

    须藤nano /etc/mysql/conf.d/galera.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地址替换突出显示的行。

/etc/mysql/conf.d/galera.cnf
/etc/mysql/conf.d/galera.cnf
[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 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.

    第一部分修改或重新声明将允许群集正常运行MySQL设置。 例如,Galera不能与MyISAM或类似的非事务性存储引擎一起使用,并且mysqld不得绑定到localhost的IP地址。 您可以在Galera Cluster 系统配置页面上详细了解设置。

  • The “Galera Provider Configuration” section configures the MySQL 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 in the documentation.

    “ Galera Provider Configuration”部分配置了提供WriteSet复制APIMySQL组件。 在您的情况下,这意味着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 Synchronization Configuration部分定义了群集如何在成员之间通信和同步数据。 这仅用于节点联机时发生的状态转移。 对于您的初始设置,您正在使用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 Node Configuration部分阐明了当前服务器的IP地址和名称。 在尝试诊断日志中的问题以及以多种方式引用每个服务器时,这很有用。 wsrep_node_address必须与您所在计算机的地址匹配,但是您可以选择所需的任何名称,以帮助您在日志文件中标识该节点。

When you are satisfied with your cluster configuration file, copy the contents into your clipboard, then save and close the file.

对集群配置文件满意后,将内容复制到剪贴板中,然后保存并关闭文件。

Now that you have configured your first node successfully, you can move on to configuring the remaining nodes in the next section.

现在您已经成功配置了第一个节点,接下来可以继续配置其余节点。

步骤4 —配置其余节点 (Step 4 — Configuring the Remaining Nodes)

In this step, you will configure the remaining two nodes. On your second node, open the configuration file:

在此步骤中,您将配置其余两个节点。 在第二个节点上,打开配置文件:

  • sudo nano /etc/mysql/conf.d/galera.cnf

    须藤nano /etc/mysql/conf.d/galera.cnf

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地址或可解析的域名用于您要设置的特定节点。 最后,更新其名称,您可以将其设置为可帮助您在日志文件中标识节点的任何名称:

/etc/mysql/conf.d/galera.cnf
/etc/mysql/conf.d/galera.cnf
. . .
# 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.

您几乎已经准备好启动群集,但是在执行此操作之前,请确保在防火墙中打开了适当的端口。

步骤5 —在每台服务器上打开防火墙 (Step 5 — Opening the Firewall on Every Server)

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:

在此步骤中,您将配置防火墙,以便打开节点间通信所需的端口。 在每台服务器上,通过运行以下命令检查防火墙的状态:

  • sudo ufw status

    sudo ufw状态

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 these 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 commands:

使用以下命令打开端口:

  • sudo ufw allow 3306,4567,4568,4444/tcp

    sudo ufw允许3306,4567,4568,4444 / tcp
  • sudo ufw allow 4567/udp

    sudo ufw允许4567 / udp

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.

既然您已经成功配置了防火墙,那么就可以在下一步中启动集群了。

步骤6 —启动集群 (Step 6 — Starting the Cluster)

In this step, you will start your MySQL Galera cluster. But first, you will enable the MySQL systemd service, so that MySQL will start automatically whenever the server is rebooted.

在此步骤中,您将启动MySQL Galera集群。 但是首先,您将启用MySQL systemd服务,以便每当服务器重新启动时MySQL就会自动启动。

使MySQL能够在所有三台服务器上启动时启动 (Enable MySQL to Start on Boot on All Three Servers)

Use the following command on all three servers to enable the MySQL systemd service:

在所有三台服务器上使用以下命令来启用MySQL systemd服务:

  • sudo systemctl enable mysql

    sudo systemctl启用mysql

You will see the following output, which shows that the service has been linked successfully to the startup services list:

您将看到以下输出,显示该服务已成功链接到启动服务列表:


   
Output
Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service → /lib/systemd/system/mysql.service.

Now that you’ve enabled mysql to start on boot on all of the servers, you’re ready to proceed to bring the cluster up.

既然已经启用了mysql以在所有服务器上启动启动,那么您就可以继续启动群集了。

调出第一个节点 (Bring Up the First Node)

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 will try to connect to at least one other node specified in its galera.cnf file to get its initial state. Without using the mysqld_bootstrap 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参数的mysqld_bootstrap脚本,则正常的systemctl start mysql将失败,因为第一个节点没有与之连接的节点在运行。

Run the following on your first server:

在第一台服务器上运行以下命令:

  • sudo mysqld_bootstrap

    须藤mysqld_bootstrap

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:

成功执行后,此命令将不显示任何输出。 成功执行此脚本后,该节点将注册为集群的一部分,您可以使用以下命令查看它:

  • mysql -u root -p -e "SHOW STATUS LIKE 'wsrep_cluster_size'"

    mysql -u root -p -e“显示状态类似'wsrep_cluster_size'”

After entering your password, 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, and when they find one, they will join the cluster.

在其余节点上,您可以正常启动mysql 。 他们将搜索群集列表中任何联机的成员,找到后,他们将加入群集。

调出第二个节点 (Bring Up the Second Node)

Now you can bring up the second node. Start mysql:

现在可以启动第二个节点。 启动mysql

  • sudo systemctl start mysql

    sudo systemctl启动mysql

No output will be displayed on successful execution. You will see your cluster size increase as each node comes online:

成功执行后将不显示任何输出。 随着每个节点联机,您将看到群集大小增加:

  • mysql -u root -p -e "SHOW STATUS LIKE 'wsrep_cluster_size'"

    mysql -u root -p -e“显示状态类似'wsrep_cluster_size'”

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 | +--------------------+-------+

调高第三个节点 (Bring Up the Third Node)

It’s now time to bring up the third node. Start mysql:

现在是时候提出第三个节点。 启动mysql

  • sudo systemctl start mysql

    sudo systemctl启动mysql

Run the following command to find the cluster size:

运行以下命令以查找集群大小:

  • mysql -u root -p -e "SHOW STATUS LIKE 'wsrep_cluster_size'"

    mysql -u root -p -e“显示状态类似'wsrep_cluster_size'”

You will see the following output, which indicates that the third node has joined the cluster and that the total number of 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 following section.

此时,整个群集都已联机并成功通信。 接下来,您可以通过在下一节中测试复制来确保工作设置。

第7步-测试复制 (Step 7 — Testing Replication)

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. In this step, you will test and see if the replication is working as expected.

至此,您已经完成了所有步骤,以便您的集群可以执行从任何节点到任何其他节点的复制,这称为主动-主动复制。 在此步骤中,您将测试并查看复制是否按预期工作。

写入第一个节点 (Write to the First Node)

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的表。

  • mysql -u root -p -e 'CREATE DATABASE playground;

    mysql -u root -p -e'创建数据库游乐场;
  • CREATE TABLE playground.equipment ( id INT NOT NULL AUTO_INCREMENT, type VARCHAR(50), quant INT, color VARCHAR(25), PRIMARY KEY(id));

    创建表park.equipment(id INT NOT NULL AUTO_INCREMENT,类型VARCHAR(50),quant INT,颜色VARCHAR(25),PRIMARY KEY(id));
  • INSERT INTO playground.equipment (type, quant, color) VALUES ("slide", 2, "blue");'

    将INSERT插入park.equipment(类型,数量,颜色)值(“ slide”,2,“ blue”);'

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 ,数量2blue的条目。

You now have one value in your table.

现在,表中有一个值。

在第二个节点上读写 (Read and Write on the Second Node)

Next, look at the second node to verify that replication is working:

接下来,查看第二个节点以验证复制是否有效:

  • mysql -u root -p -e 'SELECT * FROM playground.equipment;'

    mysql -u root -p -e'选择*来自操场。设备;'

The data you entered on the first node will be visible here on the second, proving that replication is working:

您在第一个节点上输入的数据将在第二个节点上可见,这证明复制正常进行:


   
Output
+----+-------+-------+-------+ | id | type | quant | color | +----+-------+-------+-------+ | 1 | slide | 2 | blue | +----+-------+-------+-------+

From this same node, write data to the cluster:

从同一节点将数据写入集群:

  • mysql -u root -p -e 'INSERT INTO playground.equipment (type, quant, color) VALUES ("swing", 10, "yellow");'

    mysql -u root -p -e'在操场上插入设备。(类型,数量,颜色)值(“ swing”,10,“ yellow”);'

在第三个节点上读写 (Read and Write on the Third Node)

From the third node, you can read all of this data by querying the table again:

从第三个节点,您可以通过再次查询表来读取所有这些数据:

  • mysql -u root -p -e 'SELECT * FROM playground.equipment;'

    mysql -u root -p -e'选择*来自游乐场。设备;'

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:

同样,您可以从该节点添加另一个值:

  • mysql -u root -p -e 'INSERT INTO playground.equipment (type, quant, color) VALUES ("seesaw", 3, "green");'

    mysql -u root -p -e'在操场上插入设备(类型,数量,颜色)值(“跷跷板”,3,“绿色”);'

在第一个节点上阅读 (Read on the First Node)

Back on the first node, you can verify that your data is available everywhere:

回到第一个节点,您可以验证数据在任何地方都可用:

  • mysql -u root -p -e 'SELECT * FROM playground.equipment;'

    mysql -u root -p -e'选择*来自游乐场。设备;'

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 now verified successfully that you can write to all of the nodes and that replication is being performed properly.

现在,您已经成功验证可以写入所有节点,并且复制正在正确执行。

结论 (Conclusion)

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 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 for MySQL, in which case you can check out our How To Create a Multi-Node MySQL Cluster on Ubuntu 18.04 tutorial. If you are looking to try out a managed database solution, see our DigitalOcean Managed Databases documentation.

您可能还对MySQL的其他群集解决方案感兴趣,在这种情况下,可以查看我们的《 如何在Ubuntu 18.04上创建多节点MySQL群集》教程。 如果您想尝试使用托管数据库解决方案,请参阅我们的DigitalOcean托管数据库文档 。

翻译自: https://www.digitalocean.com/community/tutorials/how-to-configure-a-galera-cluster-with-mysql-on-ubuntu-18-04-servers


http://www.niftyadmin.cn/n/3649073.html

相关文章

Android Launcher开发(一)LiveFolder(实时文件夹) 完全解析

实时文件夹概述: 实时文件夹是在SDK1.5中引入的&#xff0c;支持开发人员在设备的默认打开屏幕&#xff08;我们将其称为设备的主页&#xff09;上公开 ContentProvider&#xff0c;如联系人信息、笔记和媒体。将ContentProvider(比如Android的 contactsContentProvider)在主页…

网页开端第一次培训笔记

HTML HTML基本语法由 标签、整体结构、DOCTYPE组成 HTML的常用标签有 标题和水平线、段落和换行、列表、div和span、格式化标签 图片、超链接标签、表格、表单、字符实体等 HTML的全称为超文本标记语言&#xff0c;是一种标记语言。它包括一系列标签&#xff0e;通过这些标…

eclipse theia_如何在Ubuntu 18.04上设置Eclipse Theia Cloud IDE平台

eclipse theia介绍 (Introduction) With developer tools moving to the cloud, adoption of cloud IDE (Integrated Development Environment) platforms is growing. Cloud IDEs are accessible from every type of modern device through web browsers, and they offer nume…

Activity还是getApplicationContext、Fragment

Activity还是getApplicationContext 显示dialog必须使用Activity 使用adapter中初始化convertView最好使用Activity 显示地图时必须使用ApplicationContext 其他绝大的情况下两者都可以 选择ApplicationContext肯定没有问题 选择Activity可能会导致Activity对象不能被回收&…

网页开端第二次培训笔记

form标签性质及相关属性 input元素相关内容及框内属性、按钮属性 label标签、button按钮、select 常用属性&#xff1a;value设置值&#xff0c;selected 设置选中状态

BaseAdapter的封装(ListView)

首先第一步&#xff1a;我们看下我们一般情况下我们的BaseAdapter的使用 public class ProductAdapter extends BaseAdapter {private List<Product> productList;public ProductAdapter(List<Product> productList) {this.productList productList; }Override …

minio.exe怎么安装_如何在Ubuntu 18.04上使用Minio设置对象存储服务器

minio.exe怎么安装The author selected the Open Internet/Free Speech Fund to receive a donation as part of the Write for DOnations program. 作者选择了“ 开放互联网/言论自由基金会”作为“ Write for DOnations”计划的一部分来接受捐赠。 介绍 (Introduction) From…

[收藏]笑傲职场--如何获得影响力、权力和晋升

笑傲职场(如何在工作中获得、扩大影响力、权力和晋升&#xff09;以下是初稿大纲&#xff0c;有时间再细细道来。      序言&#xff1a;不走弯路就是捷径   内容&#xff1a;   一. 定位的原则   1、 你不能永远同时走两条钢丝   2、 永远站在主流之列   3、 一…