设为首页 收藏本站
查看: 1750|回复: 0

[经验分享] MySQL备份导致的waiting for global read lock

[复制链接]

尚未签到

发表于 2018-10-2 08:46:23 | 显示全部楼层 |阅读模式
  最近业务高峰期间经常会有开发跳起来说应用连接数据库超时了!
  我们来看下mysql的运行状态
DSC0000.png


  Waiting for>
  The thread is waiting for a global read lock obtained by another thread (with FLUSH TABLES WITH READ LOCK) to be>  Waiting for table:等待表
  Waiting for tables, Waiting for table, Waiting for table flush
  The thread got a notification that the underlying structure for a table has changed and it needs to reopen the table to get the new structure. However, to reopen the table, it must wait until all other threads have closed the table in question.

  This notification takes place if another thread has used FLUSH TABLES or one of the following statements on the table in question: FLUSH TABLES tbl_name,>  In MySQL 5.5.6, Waiting for table was replaced with Waiting for table flush.
  线程获得一个通知,底层表结构已经发生变化,它需要重新打开表来获取新的结构。然而,重新打开表,它必须等到所有其他线程关闭这个有问题的表。
  这个通知产生通常因为另一个线程对问题表执行了FLUSH TABLES或者以下语句之一:FLUSH TABLES tbl_name, ALTER TABLE, RENAME TABLE, REPAIR TABLE, ANALYZE TABLE, or OPTIMIZE TABLE.
  查看crontab,每天定时执行备份任务
  /usr/local/mysql/bin/mysqldump --user=$bakuser --patestdbword=$bakpwd--skip-opt --master-data=2 --single-transaction --add-drop-table --create-options --quick --extended-insert --set-charset --disable-keys --triggers -R --flush-logs --databases testdb > testdb.sql
  --master-data[=#]   This causes the binary log position and filename to be
  appended to the output. If equal to 1, will print it as a
  CHANGE MASTER command; if equal to 2, that command will
  be prefixed with a comment symbol. This option will turn
  --lock-all-tables on, unless --single-transaction is
  specified too (in which case a global read lock is only
  taken a short time at the beginning of the dump; don't
  forget to read about --single-transaction below). In all
  cases, any action on logs will happen at the exact moment
  of the dump. Option automatically turns --lock-tables  off.
  这个参数会运行--lock-all-tables,将master的binlog和postion信息写入SQL文件的头部,除非结合--single-transaction(但并不是说就完全的不会锁表了,执行的时候也会添加短暂的全局读锁
DSC0001.png

  我们来重现一下这个场景
  /usr/local/mysql/bin/mysqldump -u root -p  --skip-opt --master-data=2--single-transaction --add-drop-table --create-options --quick --extended-insert --set-charset --disable-keys --triggers -R --flush-logs --databases testdb > testdb.sql
  执行插入
  mysql> call insT1(10000000);
  30s后执行【如果同时执行,效果不明显】
  /usr/local/mysql/bin/mysqldump -u root -p  --skip-opt --master-data=2 --single-transaction --add-drop-table --create-options --quick --extended-insert --set-charset --disable-keys --triggers -R --flush-logs --databases testdb > testdb1.sql
  执行插入
  mysql> call insT2(1000000);
DSC0002.png

  等待刷表
  不使用--single-transaction
DSC0003.png

  等待全局读锁释放
  不使用--master-data,再跑上面的2个场景,mysql不会加锁,所以SQL很快执行完成 DSC0004.png
  结论:因为选用--master-data参数在SQL文件的头部会写入binlog和position信息,所以在执行备份前mysql需要执行flush tables,搭建过从库的同学都了解,我们在获取完整备份前都要执行FLUSH TABLES WITH READ LOCK;来获取这些主库当前信息,这里也是这样。 www.it165.net
  生产环境还是复杂的,大家会注意到我们同时使用了--msater-date和--single-transation但还是出现了全局读锁,可是在测试环境,只有不加--single-transation的时候才会出现。
  解决方法:
  1.如果你只需要文件备份,不需要经常建立从库,那么可以去掉--master-data。
  2.如果你的数据量很大 or 备份时的master信息非常需要,那么可以调整备份周期,避开两次备份出现重叠的情况。
  转载:http://www.it165.net/database/html/201303/3658.html


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-607371-1-1.html 上篇帖子: mysql innobackupex xtrabackup 大数据量 备份 还原 下篇帖子: MySQL常用函数实例(总结2)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表