LOAD DATA本地INFILE无法在.net核心上运行

时间:2019-02-07 05:41:10

标签: c# mysql asp.net-core

以下查询在mysql中有效。

DROP TEMPORARY TABLE IF EXISTS TempTbl; 
create temporary table TempTbl like ACTIVITY; 

LOAD DATA local INFILE 'C:\\test\\000000135_20190109133919.flat' 
INTO TABLE TempTbl COLUMNS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 LINES; 

但是当我尝试在.net core中运行时,出现以下错误。我该怎么解决?

var db = new RheEntities(Utils.GetAppSettingsByKey("ConnectionStrings:RheConnection"));
var query = System.IO.File.ReadAllText(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "App_Data/BulkInsertTemplate.sql"));
db.Database.ExecuteSqlCommand(query);

异常消息:

Error during LOAD DATA LOCAL INFILE

StackTrace:

at MySql.Data.MySqlClient.MySqlDataReader.ActivateResultSet(ResultSet resultSet) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlDataReader.cs:line 80
at MySql.Data.MySqlClient.MySqlDataReader.ReadFirstResultSetAsync(IOBehavior ioBehavior) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlDataReader.cs:line 302
at MySql.Data.MySqlClient.MySqlDataReader.CreateAsync(MySqlCommand command, CommandBehavior behavior, ResultSetProtocol resultSetProtocol, IOBehavior ioBehavior) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlDataReader.cs:line 287
at MySqlConnector.Core.TextCommandExecutor.ExecuteReaderAsync(String commandText, MySqlParameterCollection parameterCollection, CommandBehavior behavior, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\Core\TextCommandExecutor.cs:line 37
at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQueryAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlCommand.cs:line 261
at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlCommand.cs:line 62
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.ExecuteSqlCommand(DatabaseFacade databaseFacade, RawSqlString sql, IEnumerable`1 parameters)
at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.ExecuteSqlCommand(DatabaseFacade databaseFacade, RawSqlString sql, Object[] parameters)

InnerException:

Use SourceStream or SslMode >= VerifyCA for LOAD DATA LOCAL INFILE

详细信息:

  

.Net core 2.2

     

Microsoft.EntityFrameworkCore 2.2.1

     

Pomelo.EntityFrameworkCore.MySql 2.1.4

     

MySqlConnector 0.47.1

2 个答案:

答案 0 :(得分:2)

这是MySqlConnector中的一项安全功能;参见issue 334。由于您未连接到经过验证的主机,因此MySqlConnector可以防止信息泄露漏洞,该漏洞可能使恶意服务器/代理读取系统上的任何本地文件。

来自Security Issues with LOAD DATA LOCAL

  

本地版本的LOAD DATA存在两个潜在的安全问题:

     
      
  • 文件从客户端主机到服务器主机的传输由MySQL服务器启动。从理论上讲,可以构建一个打补丁的服务器,该服务器将告诉客户端程序传输服务器选择的文件,而不是客户端在LOAD DATA语句中指定的文件。这样的服务器可以访问客户端用户具有读取权限的客户端主机上的任何文件。
  •   

您可以通过使用MySqlBulkLoader类来解决此问题,而不用自己构造SQL:

var loader = new MySqlBulkLoader(db.Database)
{
    FileName = "C:\\test\\000000135_20190109133919.flat",
    TableName = "TempTbl",
    FieldTerminator = ",",
    FieldQuotationCharacter = '"',
    NumberOfLinesToSkip = 1,
};
loader.Load();

答案 1 :(得分:-1)

问题已通过MySql.Data (8.0.15)解决。我认为(Pomelo.EntityFrameworkCore.MySql 2.1.4 / MySqlConnector 0.47.1

有问题