wampserver + phpstorm环境下操作数据库时突然碰到’MySQL server has gone away’,于是上网求助,以为是连接超时或者导入的数据过大,于是
打开my.ini,找到wait_timeout、interactive_timeout两个变量,如果没有的话,可自行输入。
2)把这两个变量的值设为wait_timeout=2880000 interactive_timeout = 2880000
3)找到max_allowed_packet,将该变量的值设为max_allowed_packet = 10M(也可以设置自己需要的大小) ,保存。
但是发现问题依旧。代码中连续用LOAD DATA LOCAL INFILE导入了3个文件:a.csv,b.csv,c.csv:
"LOAD DATA LOCAL INFILE 'a.csv' REPLACE INTO TABLE $table FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\"'LINES TERMINATED BY '\r\n' IGNORE 1 LINES";
"LOAD DATA LOCAL INFILE 'b.csv' REPLACE INTO TABLE $table FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\"'LINES TERMINATED BY '\r\n' IGNORE 1 LINES";
"LOAD DATA LOCAL INFILE 'c.csv' REPLACE INTO TABLE $table FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\"'LINES TERMINATED BY '\r\n' IGNORE 1 LINES";
偶尔发现如果只导入一个文件不会报错,连续导入就会继续gone away,难道是超时?于是sleep(1000),睡一会后走丢的孩子依然没有回家。期间调试有几次莫名没有报“MySQL server has gone away”,而是报“incorrect string value:'\C4\E3 ' for column ‘name’”于是怀疑是否是编码的原因导致load data的时候出现数据库的错误。对比了这abc这三个文件,果然发现只有b文件里面有中文,一旦导入b文件数据库就会gone away。锁定问题以后,又在网上找解决办法,先是采取某网友写的建议将mysql的相关几个characterset都改成utf8mb4:
在mysql的安装目录下找到my.ini,作如下修改:
[mysqld]
character-set-server=utf8mb4
[mysql]
default-character-set=utf8mb4
修改后重启Mysql
然后在代码中:$sql_set = "set names 'utf8mb4'"; $result = $this->mysqli->query($sql_set);
这个折腾一下后,导入文件数据时问题依旧,此时执行show variables like "%char%"发现总有几个项目依然是utf8
| Variable_name | Value
+--------------------------+------------
| character_set_client | utf8mb4
| character_set_connection | utf8mb4 | character_set_database | utf8mb4 | character_set_filesystem | binary
| character_set_results | utf8
| character_set_server | utf8mb4 | character_set_system | utf8
在建库或建表时指定或者在my.ini中修改都一样,不能全部统一为utf8mb4,本人用的wamp集成环境,如果在phpmyadmin中强制将属性改成utf8mb4便会出现all service无法启动的现象。难道问题出现在指定的字符集不统一上面,全部改成utfmb4不行,那我全部改回统一成utf8总可以吧。将上面的工作又重新做了一篇,发现这一切又是徒劳.....
要不试试在"LOAD DATA LOCAL INFILE '语句中直接指定字符:
柳暗花明在这一刻发生了,修改字符放在哪里都不如放在自己身上。这是要告诉我凡事莫要舍近求远、求人不如先求己的道理吗? the end"LOAD DATA LOCAL INFILE '$csv' REPLACE INTO TABLE $table CHARACTER SET 'utf8' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\"'LINES TERMINATED BY '\r\n' IGNORE 1 LINES "