1. 程式人生 > >mysql導資料遇到錯誤提示ERROR 1227,Acess denied

mysql導資料遇到錯誤提示ERROR 1227,Acess denied

作者:天一閣圖書管理員

今天群裡有前同事在問mysqldump出來的sql檔案在匯入的時候,報錯error1227(42000)at line 18: Acess denied; you need (at least one of) the SUPER privilege(s) for this operation

定位到18行顯示的是一個這樣的語句: set @@session.sql_log_bin=0;

按正常情況來說是不會出現這種情況的,這個語句只是控制當前session的查詢是否寫入binlog。群裡的人都覺得這個問題很奇怪。

解決方案1:
大家看到既然是許可權問題,那麼就提權吧,或者使用root吧。但是前同事說是跳板機轉過去的,沒有辦法用root,而提權和授權(grant)也需要root許可權。方案1被否。

解決方案2:
既然不能用root,只能想其它辦法了,但是我疑惑的是,為什麼會出現使用者無法控制自己的session變數的情況?於是搜尋了一下,跳到mysql官網,找到這樣一段描述:
sql_log_bin
This variable controls whether logging to the binary log is done. The default value is 1 (do logging). To change logging for the current session, change the session value of this variable. The session user must have the [SUPER ](https://dev.mysql.com/doc/refman/5.7/en/privileges-provided.html#priv_super) privilege to set this variable. *Setting this variable to 0 prevents GTIDs from being assigned to transactions in the binary log*. If you are using GTIDs for replication, this means that, even when binary logging is later enabled once again, the GTIDs written into the log from this point do not account for any transactions that occurred in the meantime—in effect, those transactions are lost. In MySQL 5.7, it is not possible to set @@session.sql_log_bin within a transaction or subquery. (Bug #53437)


注意最後一行,上面說MySQL5.7有個bug,無法在子查詢或者事務中執行set @@session.sql_log_bin。但是盡信書不如無書,我自己在mysql中執行了一下:

mysql> select version();
+-------------------------+
| version()               |
+-------------------------+
| 5.7.16-0ubuntu0.16.04.1 |
+-------------------------+
1 row in set (0.00 sec)

mysql> set @@session.sql_log_bin=0;
Query OK, 0 rows affected (0.03 sec)

說明這個bug已經修復了,那這個同事用的mysql版本是不是聲名狼藉的mysql5.6?讓他確認了一下,確實是5.6。到此問題的根源找到了,解決方法也就有了。直接刪除sql中所有的set @@session.sql_log_bin語句就可以了。