MySQL database - locks - overview and global locks (introduction, syntax, features)

Table of contents

Overview

global lock

introduce

grammar

Features 


Overview

Locks are a mechanism for computers to coordinate concurrent access to a resource by multiple processes or threads.

In the database, in addition to traditional competition for computing resources (CPU, RAM, I/O), data is also a A resource shared by many users. How to ensure concurrent data accessconsistency and validity, is all A problem that the database must solve, lock conflict is also an important factor affecting the performance of concurrent access to the database. From this perspective, locks are particularly important and complex for databases.

Locks in MySQL are divided into the following three categories according to the lock granularity:

  • Global lock: Lock all tables in the database.
  • Table-level lock: locks the entire table for each operation.
  • Row-level locking: Each operation locks the corresponding row data.

global lock

introduce

Global lock is to lock the entire database instance. After locking, the entire instance is inread-only state, and subsequent operations DML (data manipulation language, used to add, delete and modify data in database tables) statements, DDL (data definition language, used to define data objects) statements, transaction commit statements that have been updated will be blocked. Only DQL (data query language, used to query records in tables in the database) can be executed.

Its typical usage scenario is to make a logical backup of the entire database and lock all tables to obtain a consistent view and ensure data integrity.


Why do we need to add global locks for full database logical backup?

A. Let’s first analyze: possible problems without adding a global lock.

Assume that there are three tables in the database: tb_stock inventory table, tb_order order table, tb_orderlog order log table.

  1. When performing data backup, the tb_stock inventory table is backed up first.
  2. Then in the business system, the order operation is executed, the inventory is deducted, and the order is generated (the tb_stock table is updated and the tb_order table is inserted).
  3. Then execute the logic of backing up the tb_order table.
  4. Execute order log insertion operation in business.
  5. Finally, the tb_orderlog table was backed up.

There is a problem with the data backed up at this time. Because of the backed up data, the data in the tb_stock table and the tb_order table are inconsistent (there is order information for the latest operation, but the inventory number has not been reduced).

 So how to avoid this problem? At this time, you can use MySQL's global lock to solve it.

B. Let’s analyze the situation after adding a global lock.

Before performing a logical backup of the database, first add a global lock to the entire database. Once the global lock is added, all other DDL and DML are blocked, but DQL statements can be executed, that is, they are in a read-only state, and the data Backup is a query operation.

Then during the process of logical backup of data, the data in the database will not change, thus ensuring the consistency and integrity of the data.

grammar

1. Add global lock

flush tables with read lock ;

2.Data backup

mysqldump -uroot –p1234 itcast > itcast.sql
-- ‘-u’指定主机,‘-p’输入密码,‘>’字符后面接的是备份后文件的位置

 The relevant instructions for data backup will be explained in detail in the MySQL management section later.

3. Release the lock

unlock tables ;

Features 

Adding a global lock to the database is a relatively heavy operation and has the following problems:

  • If the backup is performed on the main database, updates cannot be performed during the backup period, and the business will basically be shut down.
  • If the backup is performed on the slave database, the slave database cannot execute the binary log (binlog) synchronized from the master database during the backup period, which will cause master-slave delay.
In InnoDB engine, we can add parameters during backup --single-transaction parameters to achieve lock-free consistency
Data backup.
mysqldump --single-transaction -uroot –p123456 itcast > itcast.sql

END 


Learn from: Dark Horse Programmer - MySQL Database Course 

Guess you like

Origin blog.csdn.net/li13437542099/article/details/134770881