MySQL database maintenance experience sharing

Foreword:

The MySQL master-slave architecture should be the most commonly used group of architectures. The slave library will synchronize the data transmitted from the master library in real time. Generally, the slave library can be used as a backup node or for query. In fact, it is not only the main library that needs more attention, but also the frequent maintenance of the slave library. This article will share some experience in the maintenance of the slave library, let's learn together.

1. The GTID mode is recommended for master-slave replication

GTID stands for Global Transaction ID. GTID is actually composed of server_uuid:transaction_id. Among them, server_uuid is the unique identifier of a MySQL instance, transaction_id represents the number of transactions that have been committed on the instance, and it increases monotonically with transaction submission, so GTID can guarantee the execution of each MySQL instance transaction (the same transaction will not be executed repeatedly, And will complete the transactions that are not executed).

GTID-based master-slave replication can replace the traditional way of locating the replication position through the offset of the binlog file in the past. Especially for the one-master-multi-slave architecture, with the help of GTID, in the event of a master-slave switchover, other MySQL slaves can automatically find the correct replication location on the new master, which greatly simplifies the maintenance of the cluster under complex replication topologies. It also reduces the risk of misoperation in manually setting the copy position. In addition, GTID-based replication can ignore transactions that have been executed, reducing the risk of data inconsistency.

2. It is recommended that the parameters of the slave library be as consistent as the main library

In order to ensure the data consistency of the master-slave library, it is recommended that the version of the slave library be consistent with the main library, and the relevant parameters should be as consistent as possible with the main library. Such as character set, default storage engine, sql_mode and other parameters should be set the same. Especially for some parameters that cannot be dynamically modified, it is recommended to write the configuration file in advance and be consistent with the main library.

3. The backup can be done from the library side

MySQL full backups will put a certain amount of pressure on the server, and sometimes hold global locks temporarily. Especially for databases with large amounts of data and busy business, full backup may have an impact on the business. It is recommended to deploy the backup script on the slave library server, and the full backup can be performed on the slave library side, which can reduce the impact on the main library business during the backup process.

4. From the library is recommended to be read-only

For database read and write status, it is mainly set by the read_only global parameter. By default, the database is used for read and write operations, so the read_only parameter is 0 or false. At this time, whether it is a local user or a user who accesses the database remotely, as long as they have permission, they can read and write operations.

In order to avoid the manual update operation of the slave library, it is recommended to set the slave library to read-only, that is, set the read_only parameter to 1. read_only=1 Read-only mode, which will not affect the synchronization and replication function of the slave database. The slave database will still read the log on the master and apply the log on the slave side to ensure the synchronization of the master and slave databases. Setting the slave database as read-only will restrict users without super privileges from performing data modification operations. When ordinary application users perform DML operations such as insert, update, and delete that will cause data changes, they will report that the database is in read-only mode. This can effectively prevent the update operation from the library.

In addition, under conditions, the slave library can undertake part of the query work. For example, some report aggregation analysis queries or external service queries can be configured to query from the database to reduce the pressure on the master database.

5. Pay attention to slave library monitoring and master-slave delay

Although the slave library is not as important as the main library, you should always pay more attention to the monitoring status of the slave library. Don't wait until you need to use the slave library to find that the slave library is already inconsistent with the main library. Apart from some basic monitoring, special attention should be paid to the replication status and delay status from the library side.

We can execute show slave status; on the slave library side to query the status of the slave library. Among them, there are three main values, namely Slave SQL Running, Slave IO Running and Seconds Behind Master. These three values ​​represent the running status of the SQL thread, the running status of the IO thread, and the delay in seconds from the library. Only when Slave SQL Running, Slave IO Running is yes, and Seconds Behind Master is 0, we think that the slave library is running normally.

to sum up:

This article mainly shares some personal experiences about slave library maintenance. If there are errors, please correct me. If other students have relevant experience or suggestions, they can also leave a message to share and discuss.

Guess you like

Origin blog.51cto.com/10814168/2552248