如何在CentOS 7上设置MariaDB主从复制

The concept of database replication ensures that data is copied across multiple servers from a Master server. This provides data redundancy and ensures that data is not lost in the event that the Master node fails. In this article, we take a look at MariaDB Master-Slave replication on CentOS 7. We will demonstrate how data can be copied from a database located on a Master node to another database located on a Slave system.

数据库复制的概念可确保从主服务器跨多个服务器复制数据。 这样可以提供数据冗余,并确保在主节点发生故障的情况下不会丢失数据。 在本文中,我们将研究CentOS 7上的MariaDB主从复制。我们将演示如何将数据从位于主节点上的数据库复制到位于从系统上的另一个数据库。

MariaDB主从复制方案 (MariaDB Master-Slave Replication Scenario)

Here’s the replication Set-up:

这是复制设置:

Master node (CentOS 7 64 bit) : IP 173.82.2.236

Slave node: (CentOS 7 64 bit) : IP 173.82.94.57

步骤1:在主节点和从节点上安装MariaDB (Step 1: Install MariaDB on both the Master and Slave node)

To start off, log in to both the master and slave node and run the following commands to install MariaDB server

首先,登录到主节点和从节点,然后运行以下命令来安装MariaDB服务器

yum install mariadb-server mariadb

Sample Output

样本输出

Start MariaDB service and enable it on boot

启动MariaDB服务并在启动时启用它

# systemctl start mariadb
# systemctl enable mariadb

Sample Output

样本输出

步骤2:在主服务器和从服务器上设置MariaDB密码 (Step 2: Set MariaDB password on both Master and Slave)

By default, the password for MariaDB/MySQL is usually empty and unauthorized users can access the database. We need to make it secure by configuring a password and hardening it with other few settings. To achieve this run the command below on both the master and the slave node

默认情况下,MariaDB / MySQL的密码通常为空,未经授权的用户可以访问数据库。 我们需要通过配置密码并使用其他一些设置对其进行加固来使其安全。 为此,请在主节点和从节点上运行以下命令

mysql_secure_installation

Sample Output

样本输出

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y ## Enter Y and press Enter
New password:   ## Enter new password
Re-enter new password:  ## Enter password again
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y  ## Enter Y and press Enter
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y  ## Enter Y and press Enter
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y  ## Enter Y and press Enter
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y  ## Enter Y and press Enter
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

步骤3:配置主节点 (Step 3: Configuring the Master node)

Now that we have hardened our MariaDB instances on both nodes, Let’s configure the Master node.

既然我们已经在两个节点上加固了MariaDB实例,我们就配置主节点。

First, we need to allow MariaDB’s port 3306 across the CentOS 7 firewall. To accomplish this, run the commands

首先,我们需要在CentOS 7防火墙上允许MariaDB的端口3306 。 为此,请运行命令

# firewall-cmd --add-port=3306/tcp --zone=public --permanent

Sample Output

样本输出

The reload the firewall to effect the changes

重新加载防火墙以使更改生效

# firewall-cmd --relaod

Sample Output

样本输出

Next, make a few changes to the /etc/my.cnf file

接下来,对/etc/my.cnf文件进行一些更改

vim /etc/my.cnf

append the following lines in the [mysqld] section

[mysqld]部分中添加以下行

[mysqld]
server_id=1
log-basename=master
log-bin
binlog-format=row
binlog-do-db=replica_db
[...]

Here, replica_db is the database that we are going to create and replicate it across the slave.

在这里, replica_db是我们将要创建并在从属服务器之间复制它的数据库。

Next, restart the MariaDB service using command:

接下来,使用以下命令重新启动MariaDB服务:

systemctl restart mariadb

Now we are going to login to MariaDB as root user:

现在,我们将以root用户身份登录MariaDB:

mysql -u root -p

The next step will be to Create the replica_db database

下一步将是创建copy_db数据库

MariaDB [(none)]> CREATE DATABASE replica_db;

Next, create a Slave user and password. For example, we will use slave_user as Slave username and P@ssword100 as password:

接下来,创建一个从属用户和密码。 例如,我们将使用slave_user作为从用户名,并使用P @ ssword100作为密码:

MariaDB [(none)]> STOP SLAVE;
Query OK, 0 rows affected, 1 warning (0.00 sec)

MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'%' IDENTIFIED BY 'P@ssword100';
Query OK, 0 rows affected (0.00 sec)

Next, Flush the privileges as shown

接下来,刷新特权,如下所示

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)

Next, execute the command below to display the master status

接下来,执行以下命令以显示主状态

SHOW MASTER STATUS;

步骤4:备份主服务器中的数据库并将其传输到从服务器 (Step 4: Backing up the database in Master server and transferring it to the Slave)

Next, run the command below to back up all the Master databases

接下来,运行下面的命令备份所有Master数据库

# mysqldump --all-databases --user=root --password --master-data > masterdatabase.sql

This creates a file called masterdatabase.sql in your current working directory.

这将在当前工作目录中创建一个名为masterdatabase.sql的文件。

Again login to MySQL as root user:

再次以root用户身份登录MySQL:

mysql -u root -p

And, unlock the tables:

并且,解锁表:

MariaDB [(none)]> UNLOCK TABLES;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> quit
Bye

Now Copy the masterdatabase.sql file to your Slave server.

现在,将masterdatabase.sql文件复制到您的从属服务器。

So the command will be:

因此,命令将是:

scp masterdatabase.sql [email protected]:/home

Please recall that 173.82.94.57 is our MariaDB slave server.

请回想一下173.82.94.57是我们的MariaDB从服务器。

步骤4:配置MariaDB从站 (Step 4: Configuring MariaDB Slave)

Now it’s time to configure the MariaDB Slave node

现在是时候配置MariaDB Slave节点了

Edit file the /etc/my.cnf file

编辑文件/etc/my.cnf文件

vim /etc/my.cnf

Append the following entries under the [mysqld] section as shown

[mysqld]部分下添加以下条目,如下所示

[mysqld]
server-id = 2
replicate-do-db=replica_db
[...]

Here, replica_db is the database created on the Master Server node. Also, Be mindful to use different server-id for both master and slave servers. In this case, the server-id is 2

在这里,replica_db是在主服务器节点上创建的数据库。 另外,请注意对主服务器和从服务器使用不同的服务器ID。 在这种情况下,服务器ID为2

Save and exit the file.

保存并退出文件。

Next, we are going to import the master database as shown

接下来,我们将导入master数据库,如下所示

mysql -u root -p < /home/masterdatabase.sql

Bear in mind that we had already copied the masterdatabase.sql file from the master server to /home/ directory of the slave server.

请记住,我们已经将masterdatabase.sql文件从主服务器复制到了从服务器的/ home /目录。

Restart MariaDB service to effect the changes.

重新启动MariaDB服务以使更改生效。

systemctl restart mariadb

Now login into MariaDB as the root user

现在以root用户身份登录MariaDB

mysql -u root -p

Stop the Slave. Instruct the Slave where to find the Master Log file and start the Slave.

停止从站。 指示从属服务器在哪里找到主日志文件并启动从属服务器。

MariaDB [(none)]> STOP SLAVE;

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='173.82.2.236', MASTER_USER='slave_user', MASTER_PASSWORD='P@ssword100', MASTER_LOG_FILE='mariadb-bin.000001', MASTER_LOG_POS=473;
Query OK, 0 rows affected (0.03 sec)

MariaDB [(none)]> STOP SLAVE;
Query OK, 0 rows affected (0.01 sec)

Next, run the command below to show the status of the Slave

接下来,运行以下命令以显示从站的状态

MariaDB [(none)]> SHOW SLAVE STATUS\G;

测试MariaDB复制 (Testing MariaDB Replication)

主人方: (Master side:)

Head out to your MariaDB master server and log in to MariaDB instance using the command as shown

前往您的MariaDB主服务器并使用如下所示的命令登录MariaDB实例

mysql -u root -p

Create the database replica_db

创建数据库replica_db

Next, create a table Persons

接下来,创建一个表Persons

Add a record as shown

如图所示添加一条记录

Finally, Display the table

最后,显示表格

从侧: (Slave side:)

Now , log in to MariaDB database instance at the Slave server

现在,从属服务器上登录MariaDB数据库实例。

mysql -u root -p

Next, display the databases using the command below

接下来,使用下面的命令显示数据库

SHOW DATABASES;

As you can see, the replica_db database is present, implying that it has been replicated !

如您所见, replicate_db数据库存在,表明它已被复制!

Let’ go ahead and probe inside the database and check if any tables are present. Run

让我们继续探索数据库内部,并检查是否存在任何表。 跑

use replica_db;

Then

然后

show tables;

As you can see, the table Persons created earlier in the Master node is present. Perfect!

如您所见,存在先前在“主”节点中创建的“ 人员 ”表。 完善!

Let’s reveal its records to be dead sure that our database has been fully replicated

让我们揭示它的记录,以确保我们的数据库已被完全复制

select *from replica_db;

As observed, all the contents have been replicated and are accurate. Way to go!

如所观察到的,所有内容均已复制且准确。 要走的路!

In this article, you have learned how to How to set up MariaDB Master-Slave replication on CentOS 7. Give it a try and feel free to weigh in.

在本文中,您学习了如何在CentOS 7上设置MariaDB主从复制。请尝试一下。

翻译自: https://www.journaldev.com/29314/set-up-mariadb-master-slave-replication-centos

你可能感兴趣的:(数据库,mysql,linux,java,运维)