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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 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)

How to start nginx in Linux How to start nginx in Linux Apr 14, 2025 pm 12:51 PM

Steps to start Nginx in Linux: Check whether Nginx is installed. Use systemctl start nginx to start the Nginx service. Use systemctl enable nginx to enable automatic startup of Nginx at system startup. Use systemctl status nginx to verify that the startup is successful. Visit http://localhost in a web browser to view the default welcome page.

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

How to solve nginx403 error How to solve nginx403 error Apr 14, 2025 pm 12:54 PM

The server does not have permission to access the requested resource, resulting in a nginx 403 error. Solutions include: Check file permissions. Check the .htaccess configuration. Check nginx configuration. Configure SELinux permissions. Check the firewall rules. Troubleshoot other causes such as browser problems, server failures, or other possible errors.

How to solve nginx403 How to solve nginx403 Apr 14, 2025 am 10:33 AM

How to fix Nginx 403 Forbidden error? Check file or directory permissions; 2. Check .htaccess file; 3. Check Nginx configuration file; 4. Restart Nginx. Other possible causes include firewall rules, SELinux settings, or application issues.

How to solve nginx304 error How to solve nginx304 error Apr 14, 2025 pm 12:45 PM

Answer to the question: 304 Not Modified error indicates that the browser has cached the latest resource version of the client request. Solution: 1. Clear the browser cache; 2. Disable the browser cache; 3. Configure Nginx to allow client cache; 4. Check file permissions; 5. Check file hash; 6. Disable CDN or reverse proxy cache; 7. Restart Nginx.

How to check whether nginx is started? How to check whether nginx is started? Apr 14, 2025 pm 12:48 PM

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

How to clean nginx error log How to clean nginx error log Apr 14, 2025 pm 12:21 PM

The error log is located in /var/log/nginx (Linux) or /usr/local/var/log/nginx (macOS). Use the command line to clean up the steps: 1. Back up the original log; 2. Create an empty file as a new log; 3. Restart the Nginx service. Automatic cleaning can also be used with third-party tools such as logrotate or configured.

See all articles