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

时间:2020-02-23 14:32:22  来源:igfitidea点击:

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

MariaDB主从复制方案

这是复制设置:

Master node (CentOS 7 64 bit) : IP 173.82.2.236

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

步骤1:在主节点和从节点上都安装MariaDB

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

yum install mariadb-server mariadb

示例输出

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

# systemctl start mariadb
# systemctl enable mariadb

示例输出

步骤2:在主服务器和从服务器上设置MariaDB密码

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

mysql_secure_installation

示例输出

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:配置主节点

现在,我们已经在两个节点上加固了MariaDB实例,接下来,配置主节点。

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

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

示例输出

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

# firewall-cmd --relaod

示例输出

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

vim /etc/my.cnf

在[mysqld]部分中添加以下行

[mysqld]
server_id=1
log-basename=master
log-bin
binlog-format=row
binlog-do-db=<font color="green">replica_db</font>
[...]

其中replica_db是我们将要创建并在从属服务器上复制它的数据库。

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

systemctl restart mariadb

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

mysql -u root -p

下一步将是创建copy_db数据库

MariaDB [(none)]> CREATE DATABASE replica_db;

接下来,创建一个从属用户和密码。
例如,我们将使用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)

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

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)

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

SHOW MASTER STATUS;

步骤4:备份主服务器中的数据库并将其传输到从属服务器

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

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

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

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

mysql -u root -p

并且,解锁表:

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

MariaDB [(none)]> quit
Bye

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

因此,命令将是:

scp masterdatabase.sql [email protected]:/home

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

步骤4:配置MariaDB从站

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

编辑文件/etc/my.cnf

vim /etc/my.cnf

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

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

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

保存并退出文件。

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

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

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

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

systemctl restart mariadb

现在以root用户身份登录MariaDB

mysql -u root -p

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

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)

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

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

测试MariaDB复制

主人方:

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

mysql -u root -p

创建数据库replica_db

接下来,创建一个表" Persons"

如图所示添加一条记录

最后,显示表格

从侧:

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

mysql -u root -p

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

SHOW DATABASES;

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

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

use replica_db;

然后

show tables;

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

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

select *from replica_db;

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