前幾天幫同僚解決一個案例,在主從複制環境下,從庫上的MySQL版本号是5.5.5,遇到下面的錯誤:
~~~
#其他非相關資訊我都隐藏掉了
[([email protected])]> show slave status \G;
Slave_IO_Running: Yes
Slave_SQL_Running: No
Last_Errno: 1064
Last_Error: Error 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '6e86db84_14847168f19__8000' at line 1' on query. Default database: 'act'. Query: 'SAVEPOINT 6e86db84_14847168f19__8000'
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 1064
Last_SQL_Error: Error 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '6e86db84_14847168f19__8000' at line 1' on query. Default database: 'act_log'. Query: 'SAVEPOINT 6e86db84_14847168f19__8000'
~~~
第一感覺是遇到保留關鍵字了,不過看到這麼長的字元串,不應該是保留關鍵字才對。
經過嘗試,最後發現是字元串中的 “e” 這個字元如果存在就可能會報錯,看起來應該是bug才對了。
在MySQL的bug系統裡确實找到了這個bug,不過看bug描述,在5.5版本中應該是已經修複了才對,看來太不靠譜了呀~~
關于這個bug:[Savepoint identifier is occasionally considered as floating point numbers](http://bugs.mysql.com/bug.php?id=55962 "Savepoint identifier is occasionally considered as floating point numbers")
其實除了更新版本外,解決方法也很簡單,把savepoint後面的 identifier 字元串用反引号(波浪号的下檔鍵,英文叫做 backticks 鍵)引用起來就行。
例如:
~~~
savepoint `6e86db84_14847168f19__8000`;
~~~
這樣就可以了。
這個案例也提示我們,在寫SQL時,涉及到資料庫、表、字段、identifier 等名稱時,最好是都能用反引号引用,確定可用。
曾經看到線上資料表有個字段名是 check ,這個名字在MySQL裡很早就已經是保留關鍵字,幸好開發同學比較靠譜,都加上了反引号。
關于savepoint的2個bug:
[Savepoint Identifier should be enclosed with backticks](http://bugs.mysql.com/bug.php?id=55961 "Savepoint Identifier should be enclosed with backticks")
[Savepoint identifier is occasionally considered as floating point numbers](http://bugs.mysql.com/bug.php?id=55962 "Savepoint identifier is occasionally considered as floating point numbers")