MYSQL SOURCE報錯 ERROR: ASCII

gaopengtttt發表於2016-11-24
由 ASCII '\0' 引起的MYSQL SOURCE錯誤


今天在群裡面有一個朋友給出一個錯誤:
source test.sql
ERROR: 
ASCII '\0' appeared in the statement, but this is not allowed unless option 
--binary-mode is enabled and mysql is run in non-interactive mode. Set --bin
ary-mode to 1 if ASCII '\0' is expected. Query: ''.


以前沒見過這個錯誤,於是仔細看了一下。先根據報錯檢視--binary-mode的意思:
  --binary-mode       By default, ASCII '\0' is disallowed and '\r\n' is
                      translated to '\n'. This switch turns off both features,
                      and also turns off parsing of all clientcommands except
                      \C and DELIMITER, in non-interactive mode (for input
                      piped to mysql or loaded using the 'source' command).
                      This is necessary when processing output from mysqlbinlog
                      that may contain blobs.


意思就是ASCII '\0' 是不允許的,除非在binary mode下,為什麼會不允許呢?
原因在於一個存文字模式的sql指令碼不可能存在'\0','\0'對應ASCII的00,NUL,
我們知道在純文字模式下,任何字元都對應自己的編碼,即使是空格、換行、回車、製表符等
,00 NUL只會在二進位制模式的檔案中才有,當使用非互動模式的時候,比如
在mysqlbinlog|mysql -u root -p 時候是用會關閉,但是我測試了一下也不行
不管使用pipe管道還是重定向都不行:
[root@testmy ~]# cat test.sql|/mysqldata/mysql5.7/bin/mysql  --socket=/mysqldata/mysql5.7/mysqld3307.sock  
ERROR: ASCII '\0' appeared in the statement, but this is not allowed unless option --binary-mode is enabled and mysql is run in non-interactive mode. Set --binary-mode to 1 if ASCII '\0' is expected. Query: ''.
[root@testmy ~]# /mysqldata/mysql5.7/bin/mysql  --socket=/mysqldata/mysql5.7/mysqld3307.sock <test.sql  =""   ERROR: ASCII '\0' appeared in the statement, but this is not allowed unless option --binary-mode is enabled and mysql is run in non-interactive mode. Set --binary-mode to 1 if ASCII '\0' is expected. Query: ''.
也許這種方式只有在匯入ROW格式的binlog才會用到吧。




下面是重現方式,簡單的程式碼:
#include
#include




int main(void)
{
   FILE* fd;
   char a='\0';
   
   if(!(fd = fopen("test.sql","a+")))
        {
                perror("error:");
                exit(1);
        }
   fputc(a,fd);
   fputc('\n',fd);
   fclose(fd);
}


向test.sql寫入一個\0即可,就可以重現了。下面是一個ASCII部分控制字元的截圖

詳細見我轉的文章:
http://blog.itpub.net/7728585/viewspace-2129010/

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7728585/viewspace-2129019/,如需轉載,請註明出處,否則將追究法律責任。

相關文章