MySQL: Enable LOAD DATA LOCAL INFILE

后端 未结 18 1030
北海茫月
北海茫月 2020-11-22 09:30

I\'m running Mysql 5.5 on Ubuntu 12 LTS. How should I enable LOAD DATA LOCAL INFILE in my.cnf?

I\'ve tried adding local-infile in my config at various places but I\'

相关标签:
18条回答
  • 2020-11-22 09:35

    The my.cnf file you should edit is the /etc/mysql/my.cnf file. Just:

    sudo nano /etc/mysql/my.cnf
    

    Then add:

    [mysqld]
    local-infile 
    
    [mysql]
    local-infile 
    

    The headers [mysqld] and [mysql] are already given, just locate them in the file and add local-infile underneath each of them.

    It works for me on MySQL 5.5 on Ubuntu 12.04 LTS.

    0 讨论(0)
  • 2020-11-22 09:35

    in case your flavor of mysql on ubuntu does NOT under any circumstances work and you still get the 1148 error, you can run the load data infile command via command line

    open a terminal window

    run mysql -u YOURUSERNAME -p --local-infile YOURDBNAME

    you will be requested to insert mysqluser password

    you will be running MySQLMonitor and your command prompt will be mysql>

    run your load data infile command (dont forget to end with a semicolon ; )

    like this:

    load data local infile '/home/tony/Desktop/2013Mini.csv' into table Reading_Table FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';
    
    0 讨论(0)
  • 2020-11-22 09:35

    Also, for other readers, if you are trying to do this in Django AND your server allows local_infile (you can check by typing SHOW VARIABLES via a mysql client) then you can add this to your settings.py file (since python MySQLdb doesn't by default read the .my.cnf file):

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'mydb',
            'USER': 'myname',
            'PASSWORD': 'mypass',
            'HOST': 'myserver',
            'PORT': '3306',
            'OPTIONS' : {
                'local_infile':1,
            },
        }
    }
    
    0 讨论(0)
  • 2020-11-22 09:37

    From the MySQL 5.5 manual page:

    LOCAL works only if your server and your client both have been configured to permit it. For example, if mysqld was started with --local-infile=0, LOCAL does not work. See Section 6.1.6, “Security Issues with LOAD DATA LOCAL”.

    You should set the option:

    local-infile=1
    

    into your [mysql] entry of my.cnf file or call mysql client with the --local-infile option:

    mysql --local-infile -uroot -pyourpwd yourdbname
    

    You have to be sure that the same parameter is defined into your [mysqld] section too to enable the "local infile" feature server side.

    It's a security restriction.

    0 讨论(0)
  • 2020-11-22 09:39

    Replace the driver php5-mysql by the native driver

    On debian

    apt-get install php5-mysqlnd
    
    0 讨论(0)
  • 2020-11-22 09:39

    You have to take care how you establish your mysqli connection. Full credit for this solution goes to Jorge Albarenque, source

    In order to fix it I had to:

    • Add local-infile=1 to the [mysqld] and [mysql] sections of my.cnf (as explained in the comments above)
    • Use mysqli_real_connect function (PHP documentation).

    The catch is that with that function you can explicitly enable the support for LOAD DATA LOCAL INFILE. For example (procedural style):

    $link = mysqli_init();
    mysqli_options($link, MYSQLI_OPT_LOCAL_INFILE, true);
    mysqli_real_connect($link, $host, $username, $password, $database);
    

    or object oriented

    $mysqli = mysqli_init();
    $mysqli->options(MYSQLI_OPT_LOCAL_INFILE, true);
    $mysqli->real_connect($host, $username, $password, $database);
    
    0 讨论(0)
提交回复
热议问题