Detailed explanation of MySQL master-slave database construction method

Detailed explanation of MySQL master-slave database construction method

This article describes how to build a MySQL master-slave database. Share with you for your reference, the details are as follows:

The master-slave server is a very good solution for MySQL real-time data synchronization and backup. Now all large, medium and small websites use the MySQL database master-slave server function to perform asynchronous backup of the website database. Now we will introduce the master-slave server configuration steps.

MySQL master-slave replication requires at least two MySQL services. Of course, MySQL services can be distributed on different servers, or multiple services can be started on one server.

(1) First, ensure that the MySQL versions on the master and slave servers are the same

(2) On the master server, set up a slave database account and grant permissions using REPLICATION SLAVE , such as:

mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave001'@'192.168.0.99' IDENTIFIED BY
'123456';
Query OK, 0 rows affected (0.13 sec)

(3) Modify the configuration file my.cnf of the main database, enable BINLOG, and set the value of server-id. After the modification, you must restart the MySQL service.

[mysqld]
log-bin = /home/mysql/log/mysql-bin.log
server-id=1

(4) You can then get the current binary log name and offset of the master server. The purpose of this operation is to recover data from this point after the slave database is started.

mysql> show master statusG;
*************************** 1. row ***************************
File:mysql-bin.000003
Position: 243
Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)

(5) OK, now we can stop updating the master data and generate a backup of the master database. We can export the data to the slave database using mysqldump. Of course, you can also directly copy the data file to the slave database using the cp command.

Note that you should READ LOCK the primary database before exporting data to ensure data consistency.

mysql> flush tables with read lock;
Query OK, 0 rows affected (0.19 sec)

Then mysqldump

mysqldump -h127.0.0.1 -p3306 -uroot -p test > /home/chenyz/test.sql

It is best to restore the write operation after the primary database is backed up.

mysql> unlock tables;
Query OK, 0 rows affected (0.28 sec)

(6) Copy the test.sql file from the master data backup to the slave database and import it

(7) Then modify the my.cnf file of the slave database, add the server-id parameter, specify the user used for replication, the IP and port of the master database server, and the file and location where the replication log is to be executed.

[mysqld]
server-id=2
log_bin = /var/log/mysql/mysql-bin.log
master-host =192.168.1.100
master-user=test
master-pass=123456
master-port =3306
master-connect-retry=60
replicate-do-db =test

(8) On the slave server, start the slave process

mysql> start slave;

(9) Verify the slave server by running show salve status

mysql> SHOW SLAVE STATUS G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: localhost
Master_User: root
Master_Port: 3306
Connect_Retry: 3
Master_Log_File:mysql-bin.003
Read_Master_Log_Pos: 79
Relay_Log_File: gbichot-relay-bin.003
Relay_Log_Pos: 548
Relay_Master_Log_File:mysql-bin.003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

(10) OK, now we can do some update operations on our master server, and then check whether the update has been completed on the slave server.

Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL transaction operation skills", "MySQL stored procedure skills", "MySQL database lock related skills summary" and "MySQL common function summary"

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • Using Docker containers to build MySql master-slave replication
  • MySQL 5.7.18 master-slave replication setup (one master and one slave) tutorial detailed explanation
  • Tutorial on building a master-slave replication architecture for MySQL 5.7 Docker
  • Detailed explanation of MySQL master-slave replication read-write separation construction
  • Implementation steps for building a MySQL master-slave replication environment based on Docker
  • Implementation ideas and steps for MySQL master-slave construction (multiple masters and one slave)

<<:  Detailed explanation of using Docker to build a development environment for Laravel and Vue projects

>>:  Detailed explanation of custom instructions for Vue.js source code analysis

Recommend

Should I use Bootstrap or jQuery Mobile for mobile web wap

Solving the problem Bootstrap is a CSS framework ...

Implementation of crawler Scrapy image created by dockerfile based on alpine

1. Download the alpine image [root@DockerBrian ~]...

The problem of Vue+tsx using slot is not replaced

Table of contents Preface Find the problem solve ...

js memory leak scenarios, how to monitor and analyze them in detail

Table of contents Preface What situations can cau...

36 principles of MySQL database development (summary)

Preface These principles are summarized from actu...

Detailed explanation of how to detect and prevent JavaScript infinite loops

Table of contents Preface Fix infinite loop in fo...

How to install Docker and configure Alibaba Cloud Image Accelerator

Docker Installation There is no need to talk abou...

A brief understanding of the relevant locks in MySQL

This article is mainly to take you to quickly und...

Common errors and solutions for connecting Navicat to virtual machine MySQL

Question 1 solve Start the service: service mysql...

Ubuntu 18.04 MySQL 8.0 installation and configuration method graphic tutorial

This article shares the installation and configur...

How to install and configure GitLab on Ubuntu 20.04

introduce GitLab CE or Community Edition is an op...

JS+Canvas draws a lucky draw wheel

This article shares the specific code of JS+Canva...

Use Javascript to develop sliding-nav navigation plug-in with sliding bar effect

Table of contents 1. Introduction 2. Usage 3. Dev...

Tutorial on installing Ubuntu 20.04 and NVIDIA drivers

Install Ubuntu 20.04 Install NVIDIA drivers Confi...

How to use React forwardRef and what to note

Previously, react.forwardRef could not be applied...