Home System Tutorial LINUX How to configure master-slave replication in MariaDB

How to configure master-slave replication in MariaDB

May 01, 2024 pm 01:49 PM
mysql linux linux tutorial Red Hat linux system linux command linux certification red hat linux linux video

如何在 MariaDB 中配置主从复制

In our previous tutorial, we have learned how to install and configure MariaDB[1], and also learned some basic commands for managing MariaDB [2]. Now let's learn how to configure a master-slave replication on a MariaDB server.

Replication is used to create multiple copies of our database, these copies can be used to run queries on other databases, like some very heavy queries may affect the performance of the main database server, or we can use it to do Data redundancy, or both. We can automate this process, that is, the replication process from the master server to the slave server is automatically performed. Perform backups without affecting writes on the primary server.

So, let’s now configure our master-slave replication, which requires two machines with MariaDB installed. Their IP addresses are as follows:

  • Master Server - 192.168.1.120 Host Name - master.ltechlab.com
  • Slave server - 192.168.1.130 Host name - slave.ltechlab.com

With MariaDB installed on these machines, we continue with the tutorial. If you need a tutorial on installing and configuring MariaDB, check out this tutorial[3].

Step 1 - Master Server Configuration

We now enter a database named important in MariaDB, which will be copied to our slave server. To start the process, we edit the file named /etc/my.cnf, which is the configuration file for MariaDB.

$ vi /etc/my.cnf
Copy after login

Find the [mysqld] section in this file, and then enter the following content:

[mysqld]
log-bin
server_id=1
replicate-do-db=important
bind-address=192.168.1.120
Copy after login

Save and exit this file. After completion, you need to restart the MariaDB service.

$ systemctl restart mariadb
Copy after login
Copy after login

Next, we log into the Mariadb instance on our main server.

$ mysql -u root -p
Copy after login
Copy after login
Copy after login

Create a new user named slaveuser on it for master-slave replication, and then run the following command to assign it the required permissions:

STOP SLAVE;
GRANT REPLICATION SLAVE ON *.* TO  'slaveuser'@'%' IDENTIFIED BY 'iamslave';
FLUSH PRIVILEGES;
FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;
Copy after login

Note: We need the values ​​of MASTER_LOG_FILE and MASTER_LOG_POS to configure master-slave replication, which can be obtained by show master status, Therefore, you must make sure you write down their values.

After running these commands, enter exit to exit this session.

Step 2 - Create a database backup and move it to the slave server

Now, we need to create a backup for our database important. You can use the mysqldump command to back up.

$ mysqldump -u root -p important > important_backup.sql
Copy after login

After the backup is complete, we need to log back into the MariaDB database and unlock our tables.

$ mysql -u root -p
$ UNLOCK TABLES;
Copy after login

Then exit this session. Now, we move our backup to the slave server, whose IP address is: 192.168.1.130.

The configuration on the master server has been completed. Now, we start configuring the slave server.

Step 3: Configure slave server

我们再次去编辑(从服务器上的) /etc/my.cnf 文件,找到配置文件中的 [mysqld] 节,然后输入如下内容:

[mysqld]
server-id = 2
replicate-do-db=important
[ …]
Copy after login

现在,我们恢复我们主数据库的备份到从服务器的 MariaDB 上,运行如下命令:

$ mysql -u root -p < /data/ important_backup.sql
Copy after login

当这个恢复过程结束之后,我们将通过登入到从服务器上的 MariaDB,为数据库 important 上的用户 'slaveuser' 授权。

$ mysql -u root -p
Copy after login
Copy after login
Copy after login
GRANT ALL PRIVILEGES ON important.* TO 'slaveuser'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Copy after login

接下来,为了这个变化生效,重启 MariaDB。

$ systemctl restart mariadb
Copy after login
Copy after login
第 4 步:启动复制

记住,我们需要 MASTER_LOG_FILEMASTER_LOG_POS 变量的值,它可以通过在主服务器上运行 SHOW MASTER STATUS 获得。现在登入到从服务器上的 MariaDB,然后通过运行下列命令,告诉我们的从服务器它应该去哪里找主服务器。

STOP SLAVE;
CHANGE MASTER TO MASTER_HOST= '192.168.1.110′, MASTER_USER='slaveuser', MASTER_PASSWORD='iamslave', MASTER_LOG_FILE='mariadb-bin.000001′, MASTER_LOG_POS=460;
SLAVE START;
SHOW SLAVE STATUS\G;
Copy after login

注意: 请根据你的机器的具体情况来改变主服务器的配置。

第 5 步:测试复制

我们将在我们的主服务器上创建一个新表来测试主从复制是否正常工作。因此,登入到主服务器上的 MariaDB。

$ mysql -u root -p
Copy after login
Copy after login
Copy after login

选择数据库为 important

use important;
Copy after login

在这个数据库上创建一个名为 test 的表:

create table test (c int);
Copy after login

然后在这个表中插入一些数据:

insert into test (c) value (1);
Copy after login

检索刚才插入的值是否存在:

select * from test;
Copy after login

你将会看到刚才你插入的值已经在这个新建的表中了。

现在,我们登入到从服务器的数据库中,查看主从复制是否正常工作。

$ mysql -u root -p
$ use important;
$ select * from test;
Copy after login

你可以看到与前面在主服务器上的命令输出是一样的。因此,说明我们的主从服务工作正常,没有发生任何问题。

我们的教程结束了,请在下面的评论框中留下你的查询/问题。


The above is the detailed content of How to configure master-slave replication in MariaDB. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1272
29
C# Tutorial
1252
24
Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

git software installation git software installation Apr 17, 2025 am 11:57 AM

Installing Git software includes the following steps: Download the installation package and run the installation package to verify the installation configuration Git installation Git Bash (Windows only)

How to use sublime shortcut keys How to use sublime shortcut keys Apr 16, 2025 am 08:57 AM

Sublime Text provides shortcuts to improve development efficiency, including commonly used (save, copy, cut, etc.), editing (indentation, formatting, etc.), navigation (project panel, file browsing, etc.), and finding and replacing shortcuts. Proficiency in using these shortcut keys can significantly improve Sublime's efficiency.

See all articles