Home > Operation and Maintenance > Linux Operation and Maintenance > How to configure database master-slave replication on Linux

How to configure database master-slave replication on Linux

PHPz
Release: 2023-07-07 09:45:06
Original
1736 people have browsed it

How to configure database master-slave replication on Linux

In Linux systems, configuring database master-slave replication is a common task. Master-slave replication can provide data redundancy backup, load balancing and high availability. This article will introduce the steps of configuring database master-slave replication on Linux and provide corresponding code examples.

Step 1: Install database software
First, we need to install the corresponding database software on the master server and slave server. This article takes the MySQL database as an example. The installation process is as follows:

  1. Execute the following commands on the master server and slave server to install the MySQL database:

    sudo apt update
    sudo apt install mysql-server
    Copy after login

Step 2: Configure the main server
Next, we need to perform a series of configurations on the main server:

  1. Edit the MySQL configuration file/etc/mysql/mysql. conf.d/mysqld.cnf, uncomment the following lines (delete the # before the line):

    #bind-address            = 127.0.0.1
    #log_bin                  = /var/log/mysql/mysql-bin.log
    #binlog_do_db            = mydatabase
    Copy after login
  2. Restart the MySQL server:

    sudo service mysql restart
    Copy after login
    Copy after login
  3. Log in to the MySQL server and execute the following SQL command:

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

    Execute in the MySQL interactive interface:

    CREATE USER 'replication'@'%' IDENTIFIED BY 'password';
    GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%';
    FLUSH PRIVILEGES;
    FLUSH TABLES WITH READ LOCK;
    SHOW MASTER STATUS;
    Copy after login

    Note down the File and The value of the Position field will be used on the slave server.

  4. Unlock form:

    UNLOCK TABLES;
    Copy after login

Step 3: Configure the slave server
Next, perform a series of configurations on the slave server:

  1. Edit the MySQL configuration file/etc/mysql/mysql.conf.d/mysqld.cnf and uncomment the following lines:

    #bind-address            = 127.0.0.1
    Copy after login
  2. Restart the MySQL server:

    sudo service mysql restart
    Copy after login
    Copy after login
  3. Log in to the MySQL server and execute the following SQL command: (Replace <master-ip> with that of the master server IP address, <master-file> and <master-position> are replaced with the File and Position## recorded in the previous step. #The value of the field.)

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

    Execute in the MySQL interactive interface:

    CHANGE MASTER TO MASTER_HOST='<master-ip>', MASTER_USER='replication', MASTER_PASSWORD='password', MASTER_LOG_FILE='<master-file>', MASTER_LOG_POS=<master-position>;
    START SLAVE;
    Copy after login

  4. Check the status of the slave server:

    SHOW SLAVE STATUSG
    Copy after login
    Ensure

    The values ​​of the Slave_IO_Running and Slave_SQL_Running fields are both Yes, indicating that master-slave replication has been successfully configured.

So far, we have completed the process of configuring database master-slave replication on Linux.

Summary

This article introduces the steps to configure database master-slave replication under Linux system, and provides corresponding code examples. Through master-slave replication, we can obtain the benefits of data redundancy backup, load balancing and high availability. When configuring master-slave replication, you need to pay attention to the software installation and configuration of the master server and slave servers, and correctly set the connections and permissions of the master and slave servers. I hope this article will help you configure database master-slave replication on your Linux system.

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

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template