【mysql】mysql执行load data遇到的问题总结(全)

 


#问题一:

使用python 连接 mysql load data 报错。

报错内容:

(1148, u'The used command is not allowed with this MySQL version')

问题原因:

python 通过load data 导入mysql 数据库时候需要配置connectlocal_infile=1

解决方案:

  1. 查看mysql服务端配置
show global varaiables like 'local_infile';
  1. 得到结果
+---------------+-------+ | Variable_name | Value | +---------------+-------+ | local_infile | OFF | +---------------+-------+ 1 row in set (0.00 sec)
  1. 修改服务端配置
set global local_infile = 'ON';

此时,使用客户端连接的方式,是可以成功的load data,但是使用python的方式进行load data仍然是不行,还需要继续配置。

  1. 修改服务端配置
set global local_infile = 1;
  1. python 连接mysql 代码处添加参数
local_infile=1

最终代码如下:

db = pymysql.connect(hostname, user, password, database, port=port, local_infile=1)

问题二:

命令执行成功,但其实没有导入数据

问题原因:

pymysql在连接数据库的时候会有一个参数autocommit默认为False,表示执行完SQL语句后是否自动提交到真正的数据库,如果没有设置为True,那么你执行sql过后,是需要显式提交的,即conn.commit()

解决方案:

db = pymysql.connect(hostname, user, password, database, port=port, local_infile=1, autocommit=True)

问题三:

使用python load data导入数据,如何指定导入的字段。

解决方法:

最后加上(字段名1, 字段名2,字段名3)

案例如下:

load data infile 'elasticsearch.txt' into table es_shard_store fields terminated by ' ' lines terminated by '\n' (disk, index_uuid, node) ;

问题四:

使用python load data导入数据,报找不到该文件,但是该文件确实存在。

问题原因:

导入模式不是本地文件模式,默认是去找mysql所在节点目录下的文件。

解决方法:

加上 local 关键字

案例如下:

load data local infile 'elasticsearch.txt' into table es_shard_store fields terminated by ' ' lines terminated by '\n' (disk, index_uuid, node) ;

问题五:

报错内容:

'Loading local data is disabled; this must be enabled on both the client and

问题原因:

提示是限制了本地文件加载:
根据文档LOAD DATA LOCAL提示:https://dev.mysql.com/doc/refman/8.0/en/load-data-local-security.html#load-data-local-permitted-files

解决方案:

修改本地加载功能:

set global local_infile = 1;

参考文章

  • https://blog.csdn.net/u010787690/article/details/80473419
  • https://blog.csdn.net/rainjeyin/article/details/107779060
  • https://www.cnblogs.com/Thancoo/p/mysql8loaddatadisable.html

__EOF__

本文作者彬在俊
本文链接https://www.cnblogs.com/erlou96/p/16878289.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   彬在俊  阅读(423)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示