本文介绍了启用local-infile从rails将数据加载到远程mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用connection_ninja()从我的连接到远程mysql数据库轨道应用。我有一个方法在我的模型加载csv文件使用'load data local infile ..'从运行我的rails应用程序的服务器到远程mysql数据库。



代码如下:

  class Product< ActiveRecord :: Base 
@conn = use_connection_ninja(:rl_op)
self.table_name ='R1Product'

def self.update(file_path)
sql =LOAD DATA LOCAL INFILE'#{file_path}'
INTO TABLE R1Product
由'\'终止的字段
由'\\\
'终止的字符
name,price,productId)

@ conn.connection()。execute(sql)
end
end

这给我以下错误:

  Mysql2 ::错误:此MySQL版本不允许使用此命令:LOAD DATA LOCAL INFILE .. 

在中的 部分中设置 local-infile = 1 .cnf 运行我的rails应用程序的服务器,这允许我将数据导入到远程数据库,如果我直接登录到服务器上的mysql,并运行load data local ..命令。 / p>

如何为我的rails代码设置local-infile = 1?

解决方案

将此添加到 database.yml :

  local_infile :true 


Im using connection_ninja (https://github.com/cherring/connection_ninja) to connect to a remote mysql database from my rails applicaion. I have a method in my model which loads a csv file using 'load data local infile..' from the server running my rails app into the remote mysql db.

The code is as follows :

class Product < ActiveRecord::Base
  @conn = use_connection_ninja(:rl_op)
  self.table_name = 'RlProduct'

  def self.update(file_path)
    sql = "LOAD DATA LOCAL INFILE '#{file_path}'
           INTO TABLE RlProduct
           FIELDS TERMINATED BY ',' ENCLOSED BY '\"'
           LINES TERMINATED BY '\n'
           (name,price,productId)"               

    @conn.connection().execute(sql)
  end      
end

This is giving me the following error :

Mysql2::Error: The used command is not allowed with this MySQL version: LOAD DATA LOCAL INFILE..

I have set local-infile=1 in [mysql] section of /etc/mysql/my.cnf of the server running my rails app. And that allows me to import the data into remote db if i directly log in to mysql on the server and run the load data local.. command there.

How can i set local-infile=1 for my rails code as well ?

解决方案

add this to database.yml:

local_infile: true

这篇关于启用local-infile从rails将数据加载到远程mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 10:11