关于 mysql 锁的疑问
mysql 数据库,表的概念是对用户而言的么?用户A锁了某个表,用户B就会受锁影响,是这样么?
如果是,在一个网站站中,操作数据库都是同一个用户,这时候锁还起作用吗?
如果不是,又应该怎么理解?
回复内容:
mysql 数据库,表的概念是对用户而言的么?用户A锁了某个表,用户B就会受锁影响,是这样么?
如果是,在一个网站站中,操作数据库都是同一个用户,这时候锁还起作用吗?
如果不是,又应该怎么理解?
锁定是针对请求而言的。
MySQL表锁定分为读锁定和写锁定,主要通过四个队列来维护:两个存放正在锁定中的读写锁定信息,两个存放等待中的读写锁定信息。
如下: • Current read-lock queue (lock->read) • Pending read-lock queue
(lock->read_wait) • Current write-lock queue (lock->write) • Pending
write-lock queue (lock->write_wait) 当前持有读锁的所有线程的相关信息都能够在 Current
read-lock queue 中找到,队列中的信息按 照获取到锁的时间依序存放。而正在等待锁定资源的信息则存放在 Pending
read-lock queue 里面,另 外两个存放写锁信息的队列也按照上面相同规则来存放信息。读锁定 一个新的客户端请求在申请获取读锁定资源的时候,需要满足两个条件: 1、 请求锁定的资源当前没有被写锁定;
2、写锁定等待队列(Pending write-lock queue)中没有更高优先级的写锁定等待;
如果满足了上面两个条件之后,该请求会被立即通过,并将相关的信息存入 Current read-lock queue
中,而如果上面两个条件中任何一个没有满足,都会被迫进入等待队列Pending read-lock queue 中等待资源的释放。 写锁定
当客户端请求写锁定的时候,MySQL 首先检查在 Current write-lock queue 是否已经有锁定相同资 源的信息存在。
如果 Current write-lock queue 没有,则再检查 Pending write-lock queue,如果在
Pending write-lock queue 中找到了,自己也需要进入等待队列并暂停自身线程等待锁定资源。反之,如果 Pending
write-lock queue 为空,则再检测 Current read-lock queue,如果有锁定存在,则同样需要 进入
Pending write-lock queue 等待。当然,也可能遇到以下这两种特殊情况:
请求锁定的类型为 WRITE_DELAYED;
请求锁定的类型为 WRITE_CONCURRENT_INSERT 或者是 TL_WRITE_ALLOW_WRITE ,同时 Current read lock 是 READ_NO_INSERT 的锁定类型。
当遇到这两种特殊情况的时候,写锁定会立即获得而进入Current write-lock queue 中 如果刚开始第一次检测就Current write-lock queue 中已经存在了锁定相同资源的写锁定存在,那 么就只能进入等待队列等待相应资源锁定的释放了。
读请求和写等待队列中的写锁请求的优先级规则主要为以下规则决定:
除了 READ_HIGH_PRIORITY 的读锁定之外,Pending write-lock queue 中的 WRITE 写锁定能够阻 塞所有其他的读锁定;
READ_HIGH_PRIORITY 读锁定的请求能够阻塞所有 Pending write-lock queue 中的写锁定;
除了 WRITE 写锁定之外,Pending write-lock queue 中的其他任何写锁定都比读锁定的优先级 低。 写锁定出现在 Current write-lock queue 之后,会阻塞除了以下情况下的所有其他锁定的请求:
在某些存储引擎的允许下,可以允许一个WRITE_CONCURRENT_INSERT 写锁定请求
写锁定为 WRITE_ALLOW_WRITE 的时候,允许除了 WRITE_ONLY 之外的所有读和写锁定请求
写锁定为 WRITE_ALLOW_READ 的时候,允许除了 READ_NO_INSERT 之外的所有读锁定请求
写锁定为 WRITE_DELAYED 的时候,允许除了 READ_NO_INSERT 之外的所有读锁定请求
写锁定为 WRITE_CONCURRENT_INSERT 的时候,允许除了 READ_NO_INSERT 之外的所有读锁定请求
锁是用来解决多个进程同时处理同一个数据时的竞争问题的。
InnoDB引擎最小的锁粒度是行级锁。当2个请求同时修改某一行纪录时,就会给这个事务上行级锁。
跟用户不用户没啥鸟关系。
锁表是对数据库数据表而言的,表一锁,这个表压根无法访问,就更别讨论哪个用户了

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.
