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
PHPzOriginal
2023-07-07 09:45:061702browse

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

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
  2. Restart the MySQL server:

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

    mysql -u root -p

    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;

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

  4. Unlock form:

    UNLOCK TABLES;

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
  2. Restart the MySQL server:

    sudo service mysql restart
  3. Log in to the MySQL server and execute the following SQL command: (Replace 412d557bec4e5def6d6435dfb165ebbe with that of the master server IP address, 6c758c1d3077d1131076a81a435b0724 and e9089245baf80435fcf1948d83058143 are replaced with the File and Position## recorded in the previous step. #The value of the field.)

    mysql -u root -p

    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;

  4. Check the status of the slave server:

    SHOW SLAVE STATUSG

    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!

Statement:
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