If you are a man, you can't open it, MySQL global lock is really fragrant

Get technical dry goods and industry information for the first time!

If you are a man, you can't open it, MySQL global lock is really fragrant

Good luck, MySQL occupies more and more space

Teach you how to query back tables in MySQL

Don't wave, wretched development. A large wave of Redis interview questions is coming

Do you know all the MySQL interview questions that BAT must ask?

How to learn the Web protocol by capturing the actual packet?

This is a good lock, it's so fragrant!

If you are a man, you can't open it, MySQL global lock is really fragrant

Not many people may hear about MySQL global locks. On the one hand, some people think that DBA needs in-depth knowledge. On the other hand, most people do not have access to such deep knowledge. On the other hand, it may be as everyone said "no time to study"!

In that article yesterday, I mentioned a data migration. Let's look at a scenario. Now I need to perform data migration in order to reduce the MySQL space occupied by deletions. But between data migration, if there is still data written to the database. How can I ensure that the entire amount of data I migrated is not a few?

Because I read while writing data. For example, for a table, I have already copied it, but what if you add data again? The easiest way at this time is to use a global lock.

You can also understand global locks as database-level locks. This is the lock, which will be added to the entire database. For the above scenario, we can actually add a global read lock. Just one command.

If you are a man, you can't open it, MySQL global lock is really fragrant

Flush tables with read lock is also referred to as FTWRL for short. Execute this statement, it will make the entire library be in a read-only state. Subsequent addition, deletion, and modification statements will be blocked, including transaction commit, table structure modification and creation, etc.

In a read-only database, we can copy as much as we want. Because at this time you are copying the entire library, all the data. So this lock is a good thing.

If you are a man, you can't open it, MySQL global lock is really fragrant

Don't think that this lock is too strong to be useful. In fact, in real development work, sometimes it can help you solve a lot of trouble. The so-called existence is reasonable!

FTWRL This command is mainly used for backup tools to obtain consistent backups (data matches the binlog location). Since FTWRL needs to hold a total of two global MDL locks (I will write an article later), and also need to close all table objects, this command is very lethal, and it is easy to cause the library to hang when the command is executed. If it is the main database, the business cannot be accessed normally; if it is the standby database, it will cause the SQL thread to get stuck and delay the main and standby.

I won’t say much about how important data consistency is. The most common is that when you traverse a Java collection, ConcurrentModificationException may occur. That is, concurrent modification exception. This knowledge point, I will write the next article. Forgive me for a preview here!

FTWRL is so awesome, what exactly has it done behind the scenes? It mainly does 3 things.

  1. On the global read lock (lock_global_read_lock);

  2. Clean up the table cache (close_cached_tables);

  3. On the global COMMIT lock (make_global_read_lock_block_commit).

The global read lock will cause all update operations to be blocked; during the process of closing the table, if there is a large query that causes the closing of the table to wait, then all queries and updates that access this table need to wait; cleaning the table cache means that each table is in memory There is a table_cache in each table. The cache objects of different tables are maintained through the hash linked list, refreshing its own data, and also refreshing the data of the operating system to the disk; when the global COMMIT lock is applied, it will block the active transaction submission.

Both the global read lock and the global COMMIT lock are implemented through MDL locks. This MDL lock MySQL bottom layer is used more. There is an article to focus on. In addition to the locks described in this article, MySQL also has table locks, row-level locks and other locks. I will take time to write them one by one later. If you are interested, you can pay attention to it for a long time!

Finally, after we execute this statement, how to release the lock? That is, cancel the lock. It's also very simple, just one sentence below can be done.

If you are a man, you can't open it, MySQL global lock is really fragrant

Above, I hope to help everyone!

If you are a man, you can't open it, MySQL global lock is really fragrant

Guess you like

Origin blog.51cto.com/15127565/2666214