Waiting for table metadata lock
出现下图这个现象之前是在一张事务操作频繁地表上,执行了truncate操作。
mysql.sock@(none)> select user,host,db,command,time,state,info from information_schema.processlist where user='root'\G;
*************************** . row ***************************
user: root
host: localhost
db: NULL
command: Query
time:
state: Waiting for table metadata lock
info: truncate payment.tb_cs_rule
问题原因:
MySQL 5.5. and up uses metadata locking to manage access to objects (tables, triggers, and so forth). Metadata locking is used to ensure data consistency but does involve some overhead, which increases as query volume increases. Metadata contention increases the more that multiple queries attempt to access the same objects.
Metadata locking is not a replacement for the table definition case, and its mutxes and locks differ from the LOCK_open mutex. The following discussion provides some information about how metadata locking works.
To ensure transaction serializability, the server must not permit one session to perform a data definition language (DDL) statement on a table that is used in an uncompleted transaction in another session. The server achieves this by acquiring metadata locks on tables used within a transaction and deferring release of those locks until the transaction ends. A metadata lock on a table prevents changes to the table's structure. This locking approach has the implication that a table that is being used by a transaction within one session cannot be used in DDL statements by other sessions until the transaction ends.
This principle applies not only to transactional tables, but also to nontransactional tables. Suppose that a session begins a transaction that uses transactional table t and nontransactional table nt as follows:
START TRANSACTION;
SELECT * FROM t;
SELECT * FROM nt;
Metadata locks are held on both t and nt until the transaction ends. If another session attempts a DDL operation on either table, it blocks until metadata lock release at transaction end. For example, a second session blocks if it attempts any of these operations:
DROP TABLE t;
ALTER TABLE t ...;
DROP TABLE nt;
ALTER TABLE nt ...;
If the server acquires metadata locks for a statement that is syntactically valid but fails during execution, it does not release the locks early. Lock release is still deferred to the end of the transaction because the failed statement is written to the binary log and the locks protect log consistency.
In autocommit mode, each statement is in effect a complete transaction, so metadata locks acquired for the statement are held only to the end of the statement.
Metadata locks acquired during a PREPARE statement are released once the statement has been prepared, even if preparation occurs within a multiple-statement transaction.
Before MySQL 5.5., when a transaction acquired the equivalent of a metadata lock for a table used within a statement, it released the lock at the end of the statement. This approach had the disadvantage that if a DDL statement occurred for a table that was being used by another session in an active transaction, statements could be written to the binary log in the wrong order
解决办法:
将truncate变成delete
Waiting for table metadata lock的更多相关文章
- MySQL出现Waiting for table metadata lock的原因以及解决方法
转自:http://ctripmysqldba.iteye.com/blog/1938150 (有修改) MySQL在进行alter table等DDL操作时,有时会出现Waiting for tab ...
- 【转】【MySql】Waiting for table metadata lock原因分析
MySQL在进行alter table等DDL操作时,有时会出现Waiting for table metadata lock的等待场景.而且,一旦alter table TableA的操作停滞在Wa ...
- mysql出现Waiting for table metadata lock的原因及解决方案
最近经常遇到mysql数据库死锁,郁闷死, show processlist; 时 Waiting for table metadata lock 能一直锁很久 下面有官网的一段话,可以理解下 htt ...
- 20180117MySQL出现Waiting for table metadata lock的原因以及解决方法
转自http://www.cnblogs.com/digdeep/p/4892953.html 转自:http://ctripmysqldba.iteye.com/blog/1938150 (有修改) ...
- 【MySQL经典案例分析】 Waiting for table metadata lock
本文由云+社区发表 一. 问题是这样来的 2018年某个周末,接到连续数据库的告警,告警信息如下: 二. 苦逼的探索过程 1.总体的思路 看到too many connection的报错信息,基本 ...
- MySQL出现Waiting for table metadata lock的场景浅析
MySQL版本为5.6.12. 在进行alter table操作时,有时会出现Waiting for table metadata lock的等待场景.而且,一旦alter table TableA的 ...
- 修改字段字符集 mysql 修改 锁表 show processlist; 查看进程 Waiting for table metadata lock
ALTER TABLE `question` MODIFY COLUMN `title` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unico ...
- 记一次MySQL中Waiting for table metadata lock问题的处理
起因:由于需要,要把一张表的一个字段从不是 null 改成 可null,我用的Navicat Premium ,但是在保存的时候,工具无响应了,几个同事操作都是这样的,很奇怪,怀疑是不是由于表被锁了还 ...
- MySQL Waiting for table metadata lock的解决方法
最近需要在某一个表中新增字段,使用Sequel Pro 或者Navicat工具都会出现程序没有反应,使用 show processlist 查看,满屏都是 Waiting for table meta ...
随机推荐
- 将Python脚本打包成可执行文件——转载
Python是一个脚本语言,被解释器解释执行.它的发布方式: py文件:对于开源项目或者源码没那么重要的,直接提供源码,需要使用者自行安装Python并且安装依赖的各种库.(Python官方的各种安装 ...
- ansible自动化
一,工具与环境介绍 1.1 ansible简介 批量管理服务器的工具 无需部署agent,通过ssh进行管理 流行的自动化运维工具:https://github.com/ansible/ansib ...
- [邀月博客] SQL Server 2008中SQL增强之二:Top新用途
top数为变量时 declare @intTop intset @intTop=2print @intTop --set rowcount @intTop--select * from [dbo].[ ...
- JAVA获取客户端请求的当前网络ip地址(附:Nginx反向代理后获取客户端请求的真实IP)
1. JAVA获取客户端请求的当前网络ip地址: /** * 获取客户端请求的当前网络ip * @param request * @return */ public static String get ...
- 消息中间件 rabbitMQ
参考内容:https://www.cnblogs.com/dwlsxj/p/RabbitMQ.html 一.什么是RabbitMQ RabbitMQ是一个由erlang 开发的AMQP(Advance ...
- WPF常用布局介绍
概述:本文简要介绍了WPF中布局常用控件及布局相关的属性 1 Canvas Canvas是一个类似于坐标系的面板,所有的元素通过设置坐标来决定其在坐标系中的位置..具体表现为使用Left.Top.Ri ...
- DDB---查询与优化
摘要:分布式数据库(Distributed DB)是数据库中非常重要的一个部分,随着要处理的数据越来越多,分布式逐渐成为了一种策略.主要有:分布式操作系统,分布式程序设计语言,分布式文件系统,分布式数 ...
- paloalto防火墙执行初始配置
1.默认情况下,防火墙的 IP 地址为 192.168.1.1,用户名/密码为 admin/admin. 为了安全起见,在继续执行其他防火墙配置任务之前,必须更改这些设置.必须从 MGT 接口(即使计 ...
- 【转载】python中not,and,or的优先级问题及用法
作业: >>> print(5<4 or 3)3>>> print(2>1 or 6)True>>> print(5>1 and ...
- 4-18 class与id的区别
1. class是设置标签的类, class属性用于指定元素属于何种样式的类. 如样式表可以加入.content1 { color: red; background: #ff80c0 } 使用方法:c ...