最近项目迁移到亚马逊上,同时把mysql从5.1升级到了5.7
数据库常常突然内存飙升 导致系统死机。
在询问了某个靠谱的dba之后,在网上找了jetprofiler来监控数据库当机前发生了什么。
www.jetprofiler.com
抓到了这样一个sql
locklock tables t write
通过full processlist,发现这个查询myisam table的query status是 Waiting for table metadata lock
59965 xxxx AAA.BBB.CCC.DDD:PPP xxxx Query 305 Waiting for table metadata lock lock tables t write
同时还发现这样的thread有很多,反复在 Waiting for table metadata lock与 opening tables之间切换https://dev.mysql.com/doc/refman/5.7/en/general-thread-states.html
Opening tables
The thread is trying to open a table. This is should be veryfast procedure, unless something prevents opening. For example, an ALTER
or a
TABLELOCKTABLE
statementcan prevent opening a table until the statement is finished. It is also worthchecking that your table_open_cache
value is large enough.
http://dev.mysql.com/doc/refman/5.7/en/metadata-locking.html
http://blog.itpub.net/25704976/viewspace-1379568/
“MySQL5.5.3 and up uses metadata locking to manage concurrent access to databaseobjects and to ensure data consistency. Metadata locking applies not just totables, but also to schemas and stored programs (procedures, functions,triggers, and scheduled events).”
由于myisam是表级锁,所以移除lock table语句 避免触发metadata lock机制。
目前数据库平稳运行超过48小时。
存疑:
myisam 的表会导致 innodb disk IO上升??