Search code examples
mysqlruby-on-railsruby-on-rails-3ruby-on-rails-3.1mysql-error-1064

error when run SQL statement


I am developing Rails v2.3.2 with MySQL v5.1 on Ubuntu machine.

MySQL data directory is /var/lib/mysql/

I have a test.dat file located on /var/lib/mysql/tmp/test.dat

I would like to load data from the test.dat file into my database table, so I execute the following SQL statement in one of my rake task:

namespace :db do
  task :do_something => :environment do

    sql="LOAD DATA INFILE '/var/lib/mysql/tmp/test.dat'
                INTO TABLE myapp.cars;"

    ActiveRecord::Base.connection.execute(sql);
  end
end

But I got the following error:

Mysql2::Error: Can't get stat of '/var/lib/mysql/tmp/test.dat' (Errcode: 2): LOAD DATA INFILE '/var/lib/mysql/tmp/test.dat'

What could be the reason??

P.S. one thing come to my mind is that /var/lib/mysql/ can only be accessed by root user, but I am not sure if it is the reason.


Solution

  • Try LOAD DATA LOCAL INFILE ....