How to configure master-slave replication in 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].
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.
1 |
|
Find the [mysqld] section in this file, and then enter the following content:
1 2 3 4 5 |
|
Save and exit this file. After completion, you need to restart the MariaDB service.
1 |
|
Next, we log into the Mariadb instance on our main server.
1 |
|
Create a new user named slaveuser on it for master-slave replication, and then run the following command to assign it the required permissions:
1 2 3 4 5 |
|
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.
Now, we need to create a backup for our database important. You can use the mysqldump command to back up.
1 |
|
After the backup is complete, we need to log back into the MariaDB database and unlock our tables.
1 2 |
|
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.
我们再次去编辑(从服务器上的) /etc/my.cnf 文件,找到配置文件中的 [mysqld] 节,然后输入如下内容:
1 2 3 4 |
|
现在,我们恢复我们主数据库的备份到从服务器的 MariaDB 上,运行如下命令:
1 |
|
当这个恢复过程结束之后,我们将通过登入到从服务器上的 MariaDB,为数据库 important 上的用户 'slaveuser' 授权。
1 |
|
1 2 |
|
接下来,为了这个变化生效,重启 MariaDB。
1 |
|
记住,我们需要 MASTER_LOG_FILE 和 MASTER_LOG_POS 变量的值,它可以通过在主服务器上运行 SHOW MASTER STATUS 获得。现在登入到从服务器上的 MariaDB,然后通过运行下列命令,告诉我们的从服务器它应该去哪里找主服务器。
1 2 3 4 |
|
注意: 请根据你的机器的具体情况来改变主服务器的配置。
我们将在我们的主服务器上创建一个新表来测试主从复制是否正常工作。因此,登入到主服务器上的 MariaDB。
1 |
|
选择数据库为 important:
1 |
|
在这个数据库上创建一个名为 test 的表:
1 |
|
然后在这个表中插入一些数据:
1 |
|
检索刚才插入的值是否存在:
1 |
|
你将会看到刚才你插入的值已经在这个新建的表中了。
现在,我们登入到从服务器的数据库中,查看主从复制是否正常工作。
1 2 3 |
|
你可以看到与前面在主服务器上的命令输出是一样的。因此,说明我们的主从服务工作正常,没有发生任何问题。
我们的教程结束了,请在下面的评论框中留下你的查询/问题。
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Using python in Linux terminal...

Multithreading in the language can greatly improve program efficiency. There are four main ways to implement multithreading in C language: Create independent processes: Create multiple independently running processes, each process has its own memory space. Pseudo-multithreading: Create multiple execution streams in a process that share the same memory space and execute alternately. Multi-threaded library: Use multi-threaded libraries such as pthreads to create and manage threads, providing rich thread operation functions. Coroutine: A lightweight multi-threaded implementation that divides tasks into small subtasks and executes them in turn.

To open a web.xml file, you can use the following methods: Use a text editor (such as Notepad or TextEdit) to edit commands using an integrated development environment (such as Eclipse or NetBeans) (Windows: notepad web.xml; Mac/Linux: open -a TextEdit web.xml)

Regarding the problem of removing the Python interpreter that comes with Linux systems, many Linux distributions will preinstall the Python interpreter when installed, and it does not use the package manager...

Linux is best used as server management, embedded systems and desktop environments. 1) In server management, Linux is used to host websites, databases, and applications, providing stability and reliability. 2) In embedded systems, Linux is widely used in smart home and automotive electronic systems because of its flexibility and stability. 3) In the desktop environment, Linux provides rich applications and efficient performance.

DebianLinux is known for its stability and security and is widely used in server, development and desktop environments. While there is currently a lack of official instructions on direct compatibility with Debian and Hadoop, this article will guide you on how to deploy Hadoop on your Debian system. Debian system requirements: Before starting Hadoop configuration, please make sure that your Debian system meets the minimum operating requirements of Hadoop, which includes installing the necessary Java Runtime Environment (JRE) and Hadoop packages. Hadoop deployment steps: Download and unzip Hadoop: Download the Hadoop version you need from the official ApacheHadoop website and solve it

Do I need to install an Oracle client when connecting to an Oracle database using Go? When developing in Go, connecting to Oracle databases is a common requirement...

"DebianStrings" is not a standard term, and its specific meaning is still unclear. This article cannot directly comment on its browser compatibility. However, if "DebianStrings" refers to a web application running on a Debian system, its browser compatibility depends on the technical architecture of the application itself. Most modern web applications are committed to cross-browser compatibility. This relies on following web standards and using well-compatible front-end technologies (such as HTML, CSS, JavaScript) and back-end technologies (such as PHP, Python, Node.js, etc.). To ensure that the application is compatible with multiple browsers, developers often need to conduct cross-browser testing and use responsiveness
