Detailed explanation of Mysql master-slave synchronization configuration practice

Detailed explanation of Mysql master-slave synchronization configuration practice

1. Introduction

I wrote an article before: The principle of MySQL master-slave synchronization.

I believe that everyone who has read this article is eager to give it a try, right?

Today we will have a practical experience of MySQL master-slave synchronization!

2. Environmental description

OS: Ubuntu 16.04

mysql:5.7.17

The following practical exercises are all based on the above environment. Of course, other environments are similar.

3. Entering actual combat

tool

2 machines:

master IP:192.168.33.22

slave IP:192.168.33.33

Operations on the master machine

1. Change the configuration file

We find the file /etc/mysql/mysql.conf.d/mysqld.cnf .

The configuration is as follows:

bind-address = 192.168.33.22 #your master ip
server-id = 1 #In the master-slave architecture, each machine node needs to have a unique server-id
log_bin = /var/log/mysql/mysql-bin.log #Open binlog

2. Restart MySQL to make the configuration file take effect.

sudo systemctl restart mysql

3. Create a mysql user for master-slave synchronization.

$ mysql -u root -p
Password:

##Create the slave1 user and specify that the user can only log in on the host 192.168.33.33.
mysql> CREATE USER 'slave1'@'192.168.33.33' IDENTIFIED BY 'slavepass';
Query OK, 0 rows affected (0.00 sec)

##Give slave1 the REPLICATION SLAVE permission.
mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave1'@'192.168.33.33';
Query OK, 0 rows affected (0.00 sec)

4. Add a read lock to MYSQL

In order to keep the data of the master database consistent with that of the slave database, we first add a read lock to MySQL to make it read-only.

mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)

5. Record the location of the MASTER REPLICATION LOG

This information will be used later.

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 613 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

6. Export the existing data information in the master DB

$ mysqldump -u root -p --all-databases --master-data > dbdump.sql

7. Access the read lock of the master DB

mysql> UNLOCK TABLES;

8. Copy the dbdump.sql file in step 6 to the slave

scp dbdump.sql [email protected]:/home/ubuntu

Operations on the slave machine

1. Change the configuration file

We find the file /etc/mysql/mysql.conf.d/mysqld.cnf .

Change the configuration as follows:

bind-address = 192.168.33.33 #your slave ip
server-id = 2 #Unique server-id in the master-slave structure
log_bin = /var/log/mysql/mysql-bin.log #Open binlog

2. Restart MySQL to make the configuration file take effect

sudo systemctl restart mysql

3. Import from master DB. Export dbdump.sql file to make master-slave data consistent

$ mysql -u root -p < /home/ubuntu/dbdump.sql

4. Establish a connection between slave and master for synchronization

$ mysql -u root -p
Password:

mysql> STOP SLAVE;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CHANGE MASTER TO
 -> MASTER_HOST='192.168.33.22',
 -> MASTER_USER='slave1',
 -> MASTER_PASSWORD='slavepass',
 -> MASTER_LOG_FILE='mysql-bin.000001',
 -> MASTER_LOG_POS=613;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> START SLAVE;
Query OK, 0 rows affected (0.00 sec)

The values ​​of MASTER_LOG_FILE='mysql-bin.000001' and MASTER_LOG_POS=613 are obtained from the SHOW MASTER STATUS above.

After this setting, you can perform master-slave synchronization~

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • MySQL master-slave synchronization principle and application
  • Master-slave synchronization configuration of Mysql database
  • This article will show you the principle of MySQL master-slave synchronization
  • MySQL master-slave synchronization mechanism and synchronization delay problem tracking process
  • The implementation principle of Mysql master-slave synchronization
  • How to set up master-slave synchronization in MYSQL database

<<:  Use of hasOwnProperty method of js attribute object

>>:  Detailed explanation and summary of the use of Linux scheduled task Crontab command

Recommend

Detailed explanation of CocosCreator message distribution mechanism

Overview This article begins to introduce content...

TypeScript union types, intersection types and type guards

Table of contents 1. Union Type 2. Crossover Type...

Why is there this in JS?

Table of contents 1. Demand 2. Solution 3. The fi...

How to clean up data in MySQL online database

Table of contents 01 Scenario Analysis 02 Operati...

Practical notes on installing Jenkins with docker-compose

Create a Directory cd /usr/local/docker/ mkdir je...

Shorten the page rendering time to make the page run faster

How to shorten the page rendering time on the bro...

How to install and modify the initial password of mysql5.7.18

For Centos installation of MySQL, please refer to...

Angular environment construction and simple experience summary

Introduction to Angular Angular is an open source...

Implementation of Nginx Intranet Standalone Reverse Proxy

Table of contents 1 Nginx Installation 2 Configur...

After reading the introduction of CSS box model, you will not be confused

The property names often heard in web design: con...