【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
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值