MySql 8 版本 load data出错的问题

时间:2021-09-11 03:41:12

MySql数据库升级到8以上时,对于开发人员来说非常好用的数据批量导入的工具loaddata使用会出现一些小问题。

这里简单总结一下,如果可以帮大家省时间最好不过

简单粗暴,围绕出啥错,为什么,怎么解决三点开始正文

 

错误:ERROR 1148: The used command is not allowed with this MySQL version

为啥:load data local可被利用为高危漏洞,所以8以上mysql默认关闭改功能

咋办:修改全局变量启用load data

set global local_infile = 1;

修改config启用load data local

[client]
loose-local-infile = 1

 

错误:MySqlBulkLoader 报 promissions deny

为啥:5.6之前,这代码没问题,5.6之后的版本,默认local = false

咋办:代码加一段就可以了

var bulk = new MySqlBulkLoader(conn)
                    {
                        CharacterSet = "utf8mb4",
                        Local = true, #加这个,参考官方文档,文末有链接
                        FieldTerminator = ",",
                        FieldQuotationCharacter = ",
                        EscapeCharacter = ",
                        LineTerminator = "rn",
                        FileName = csvFileItemPath,
                        NumberOfLinesToSkip = 1,
                        TableName = cacheTableName
                    }

 

错误:MySqlBulkLoader 报 The used command is not allowed with this MySQL version;设置了local_infile = 1也没啥用

为啥:猜测mysqlconnection,默认关闭这个功能的。至于为啥全局变量启用load data还是无效,我没有深究,有知道的小伙伴能告诉我最好了 [手动狗头]

咋办:修改链接字符串,加上allowLoadLocalInfile

Data Source=***.***.***.*;Initial Catalog=******;user id=*****;pwd=*******;allowLoadLocalInfile=true;

 

错误:万一手贱,以为是包的问题,把mysql.data.ef6包给升级了,发现咋跑都报错

为啥:mysql.data.ef6迁了,最新版到包mysql.data.entityframework。如果还是有问题(具体啥错我就不贴了),把下面的这个config招呼上

  <entityFramework codeConfigurationType="MySql.Data.EntityFramework.MySqlEFConfiguration, MySql.Data.EntityFramework">
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.EntityFramework, Version=8.0.18.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </providers>
  </entityFramework>

 

 

参考:

Mysql8以上版本默认禁用load data命令官宣

https://dev.mysql.com/doc/refman/8.0/en/load-data-local.html

LoadData命令相关

https://dev.mysql.com/doc/refman/5.7/en/load-data.html

命令报错:ERROR 1148: The used command is not allowed with this MySQL version

https://*.com/questions/18437689/error-1148-the-used-command-is-not-allowed-with-this-mysql-version

MySqlBulkLoader官方实例

https://dev.mysql.com/doc/connector-net/en/connector-net-programming-bulk-loader.html