22

I am running Ubuntu 12.04 and MySQL 5.5 Alright so here is the problem:

Using the MySQLDB module for Python, the SQL command:

cursor.execute("LOAD DATA LOCAL INFILE 'example.csv' INTO TABLE 'example_mysql_table' TERMINATED BY ',';")

Doesn't work. I get ERROR 1148: The used command is not allowed with this MySQL version

I have looked around for a solution for this for a while now, and so far it seems other people having the same problem have fixed it by adding "local-infile = 1" to 'my.cnf' underneath [mysqld] and [mysql]. This has not worked for me, and I am not sure why.

Relevant Link:

MySQL: Enable LOAD DATA LOCAL INFILE

Community
  • 1
  • 1
user1746082
  • 230
  • 1
  • 2
  • 5
  • Did you restart MySQL after modifying my.cnf? What happens when you run it with the MySQL console directly? – Nathan Villaescusa Oct 15 '12 at 06:12
  • 1
    I did restart MySQL after modifying and it didn't change anything. The command does work when running it directly on the MySQL console, so I am assuming it is an issue with MySQLdb – user1746082 Oct 16 '12 at 01:08

3 Answers3

69

After spending hours on trying all kinds of settings in the config files and restarting mysql dozens of times I came up with this which seems to solve the problem (could not find this in any documentation anywhere)

MySQLdb.connect(server, username, password, database, local_infile = 1)
Johann Bosman
  • 713
  • 4
  • 5
  • 1
    great solution, pure genius! and keeps me from editing `my.conf` – sadmicrowave Sep 03 '13 at 18:26
  • 1
    coming a little late on this thread, I was curious if this is the recommended way and also if you can pass other arguments when creating the connection similar to this. – Hatem Jaber Feb 24 '15 at 14:34
  • Thanks, this is how it should be to add the local_infile option, but not to edit my.conf. – zhihong Aug 12 '16 at 09:57
  • Thanks. It's a shame though that the more optimized c binding `_mysql.connect` doesn't have the same option... Any luck with that? – AustEcon Aug 27 '21 at 12:57
4

As I see, there is no file option local-infile. But you can change this value dynamically in a script.

To view this option run this query -

SELECT @@global.local_infile;

To set variable use this statement -

SET @@global.local_infile = 1;

Tested in MySQL command line console, everything is OK:

SET @@GLOBAL.local_infile = 1;
LOAD DATA LOCAL INFILE 'test.csv' INTO TABLE table1;
Query OK, 3 rows affected (0.06 sec)

SET @@GLOBAL.local_infile = 0;
LOAD DATA LOCAL INFILE 'test.csv' INTO TABLE table1;
ERROR 1148 (42000): The used command is not allowed with this MySQL version
Devart
  • 119,203
  • 23
  • 166
  • 186
  • I ran those queries in the MySQL console, and they worked, but when I added a line in my python code to set the local_infile variable to 1, ala: cursor.execute("SELECT @@global.local_infile = 1;") nothing changed, still got the 1148 error. – user1746082 Oct 16 '12 at 01:21
  • Were there any errors? Or maybe someone, or something reset this flag again? Anyway, if it works in console it should work everywhere. – Devart Oct 16 '12 at 06:32
  • 1
    Stop... You shouldn't run SELECT. Use SET statement! – Devart Oct 16 '12 at 06:34
  • Sorry for the typo, I did use the SET statement and not SELECT. It still does not work.. – user1746082 Oct 17 '12 at 05:40
  • I have tried to reproduce the problem. Everything works. Have a look at answer. – Devart Oct 17 '12 at 07:57
0

I know this is a really old thread, but I thought I'd just add a little footnote regarding Flask.

I was unable to upload any csv files to MySQL via LOAD DATA INFILE, so I attempted to use LOAD DATA LOCAL INFILE. I got the same 'ERROR 1148: The used command is not allowed with this MySQL version' mentioned above.

My solution to this was to open up flaskext > mysql.py and modify the 'connect' function. I added:

if self.app.config['MYSQL_LOCAL_INFILE']:
    self.connect_args['local_infile'] = self.app.config['MYSQL_LOCAL_INFILE']

I then added:

app.config['MYSQL_LOCAL_INFILE'] = True

to my flask module. This effectively allows for the local_infile option of pymysql to be set to 'True' when using flaskext.mysql

stoicalpirate
  • 250
  • 3
  • 12