MyCat01——How to implement master-slave replication in MySQL

1 question

Data is one of the most important assets for us, because the losses caused by data loss can sometimes be devastating to a company.

So how to ensure data security and prevent data loss due to power outages or system failures?

As users increase, the number of visits to the database also increases. How to improve database performance and reduce the pressure on the data caused by the increase in visits?

Master-slave replication is undoubtedly one of the better choices. It copies the data of the master database to multiple slave databases. If the master database hangs up, the data of the slave databases will be intact and can continue to provide services to users.

At the same time, the database can be read and written separately. The master database is used for data writing, and the slave database is used for data reading.

2 binlog log

2.1 What is binlog

It is the most important log in MySQL. It records all data changes in MySQL and is stored on disk in binary form. These changes include all table structure changes (CREATE, ALTER TABLE...) and table data changes ( INSERT, UPDATE, DELETE...), excluding SELECT.

It is also the data source for master-slave replication.

Binlog logs have three recording formats:

  • Statement Every data change in SQL will be recorded;
  • row only records modified data;
  • mixed The above two methods are mixed. Statement is used for general statement modification, and row is used when the entire table is updated. However, the @@host name function cannot be used in this mode.

2.2 binlog log related commands

Log in to the mysql command line:

View log location

show variables like '%log_bin%';

Check the log format. The format here refers to the three formats for recording logs.

show variables like '%format%';

View currently used log files

show master status;

3 Principle of master-slave replication

Start two threads from the database:

One is used to monitor changes in the binlog log of the master database. If there is an update, read it out and write it to the relay log of the slave database;

Another thread is used to monitor changes in the local relay log. If there is an update, the updated content will be executed locally, thus ensuring the consistency of the data in the slave database and the master database.

4 Set up master-slave replication

4.1 Install MySQL database

At least two MySQL databases have been installed here by default. If you are not sure how to install them, you can search them online.

4.2 Turn on the Binlog log of the database

Add two configurations to the configuration file of the main database MySQL

Add the same configuration to the slave database. The server-id cannot be the same and needs to be changed to 102.

After the setup is complete, the database needs to be restarted.

4.3 Configure the user used to read the main database

4.3.1 Operations in the master database

First, you need to create a binlog log reader user in the main database to allow the slave database to use the slave user to access the main database.

Check the status of log files in the database

show master status;

Remember two of the parameters, which will be used in the slave library below.

Execute in the command line of the main database:

grant replication slave on *.* to 'slave'@'%' identified by '123456';

4.3.2 Operations from the database

Configure the main library connection information in the slave library

Each information in the command needs to be consistent with the content queried in the main database.

  • master_host IP address of the main library
  • master_user the user of the main library
  • master_password Password of the main library
  • master_log_file The binlog file currently used by the main library
  • master_log_pos is the current binlog file location of the main library

change master to master_host='192.168.1.2',master_user='slave',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=106;

After the configuration is complete, execute the following command to start the slave service

start slave;

View slave status

show slave status \G;

If the Slave_IO_Running Slave_SQL_Running parameters are both YES, it means synchronization is normal.

Otherwise, you need to check whether it is a firewall problem, or it is caused by the same Server-id in the database configuration file.

At this point, as long as data is modified in the master database, the data in the slave database will be automatically refreshed.

5 Release master-slave replication

Enter the command line of the slave library and perform the following operations

mysql> stop slave;
Query OK, 0 rows affected (0.08 sec)

mysql> reset slave all;
Query OK, 0 rows affected (0.18 sec)

mysql> show slave status\G;
Empty set (0.00 sec)

Guess you like

Origin blog.csdn.net/QQ156881887/article/details/131386216