轉 mysql 備份致使 waiting for global read lock

######轉 https://blog.csdn.net/weixin_34038652/article/details/92129498mysql

 

近業務高峯期間常常會有開發跳起來講應用鏈接數據庫超時了!sql

 

咱們來看下mysql的運行狀態
 shell

\


Waiting for release of readlock:等待釋放全局鎖數據庫

The thread is waiting for a global read lock obtained by another thread (with FLUSH TABLES WITH READ LOCK) to be released.This state was removed in MySQL 5.5.8; Waiting for global read lock or Waiting for commit lock are used instead.oracle

 

Waiting for table:等待表app

Waiting for tables, Waiting for table, Waiting for table flushless

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.socket

 

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, ALTER TABLE, RENAME TABLE, REPAIR TABLE, ANALYZE TABLE, or OPTIMIZE TABLE.post

 

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(但並非說就徹底的不會鎖表了,執行的時候也會添加短暫的全局讀鎖)

\

 

咱們來重現一下這個場景

 

/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);

 

\

等待刷表

不使用--single-transaction
 

\


等待全局讀鎖釋放

不使用--master-data,再跑上面的2個場景,mysql不會加鎖,因此SQL很快執行完成\

 

結論:由於選用--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信息很是須要,那麼能夠調整備份週期,避開兩次備份出現重疊的狀況。

 

 

##sample 

 

1. ps -ef|grep mysql   show backup is doing 

[root@pzabbixdb01 tmp]# ps -ef|grep mysql
root 2406 1 0 Apr09 ? 00:00:00 /bin/sh /db/mysql/app/mysql/bin/mysqld_safe --defaults-file=/db/mysql/app/mysql/my.cnf --datadir=/db/mysql/data/mydata --user=mysql
mysql 3076 2406 16 Apr09 ? 34-04:12:09 /db/mysql/app/mysql/bin/mysqld --defaults-file=/db/mysql/app/mysql/my.cnf --basedir=/db/mysql/app/mysql --datadir=/db/mysql/data/mydata --plugin-dir=/db/mysql/app/mysql/lib/plugin --user=mysql --log-error=/db/mysql/data/mydata/mysql-error.log --open-files-limit=65535 --pid-file=/db/mysql/app/mysql/mysql.pid --socket=/db/mysql/data/mysqltmp/mysql.sock --port=3306
root 10345 3784 0 21:01 pts/0 00:00:00 mysql -uroot -px xxx
root 11688 10389 0 21:18 pts/1 00:00:00 grep mysql
root 32759 1 0 19:00 ? 00:00:00 bphdb -sb -rdbms oracle -S pnbumaster -to 3600 -c mysql_zabbixdb -s full -clnt pzabbixdb01 -FULL -kl 28 -b pzabbixdb01_1572519604 -jobid 7409084
root 32761 32759 0 19:00 ? 00:00:00 /bin/sh /db/mysql/app/shell/mysql_backup.sh >/dev/null 2>/dev/null
root 32767 32761 7 19:00 ? 00:10:08 /opt/mysql/meb-3.12/bin/mysqlbackup --user=root --password=xxxxx --backup-image=sbt:bkpsbtNB20191031190005 --sbt-lib-path=/usr/openv/netbackup/bin/libobk.so64 --sbt-environment=NB_ORA_SERV=pnbumaster,NB_ORA_CLIENT=pzabbixdb01,NB_ORA_POLICY=mysql_zabbixdb,NB_ORA_SCHED=Default-Application-Backup,ORACLE_HOME=/db/mysql --backup-dir=/usr/openv/meb_bkdir/ backup-to-image --socket=/db/mysql/data/mysqltmp/mysql.sock --port=3306

 

2.

lock inf:  Waiting for global read lock

| 16449640 | zabbix | 10.200.:50083 | zabbix | Query | 2166 | Waiting for global read lock | update httptest set nextcheck=1572524839 where httptestid=15 |
| 16449641 | zabbix | 10.200.:50084 | zabbix | Query | 1735 | Waiting for global read lock | insert into events (eventid,source,object,objectid,clock,ns,value) values (38773114,0,0,128316,15725 |

 

 

3.workaroud:

kill -9 <backup_process>

相關文章
相關標籤/搜索