LOAD DATA LOCAL INFILE fails - from php, to mysql (on Amazon rds)

后端 未结 5 1900
忘掉有多难
忘掉有多难 2020-12-09 14:08

We\'re moving our database from being on the webserver to a separate server (from an Amazon EC2 webserver to an RDS instance.)

We have a LOAD DATA INFILE that worked

相关标签:
5条回答
  • 2020-12-09 14:26

    As referred in this post, adding 3rd and 4th parameter to mysql_connect are required to get LOAD LOCAL DATA INFILE working. It helped me. Any other suggestions (apparmor, local-infile=1 in my.cnf widely discussed in internet) did not help. Following PHP code worked for me!

    mysql_connect(HOST,USER,PASS,false,128);
    

    True, this is in manual, too.

    0 讨论(0)
  • 2020-12-09 14:28

    use the following line that client activates with infile true

    mysql --local-infile=1 -u root -p

    0 讨论(0)
  • 2020-12-09 14:30

    I've given up on this, as I think it's a bug in php - in particular the mysql_connect code, which is now deprecated. It could probably be solved by compiling php yourself with changes to the source using steps similar to those mentioned in the bug report that @eggyal mentioned: https://bugs.php.net/bug.php?id=54158

    Instead, I'm going to work around it by doing a system() call and using the mysql command line:

    $sql = "LOAD DATA LOCAL INFILE '$csvPathAndFile' INTO TABLE $tableName FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\\\"' ESCAPED BY '\\\\\\\\' LINES TERMINATED BY '\\\\r\\\\n';";
    system("mysql -u $dbUser -h $dbHost --password=$dbPass --local_infile=1 -e \"$sql\" $dbName");
    

    That's working for me.

    0 讨论(0)
  • 2020-12-09 14:31

    If you're doing this in 2020, a tip for you is to check your phpinfo.php or php --ini for the location of the configuratin file. For me I was using virtualmin and changing the php ini file but my site had it's own specific ini file. Once I located it's location and changed it everything went back to normal.

    0 讨论(0)
  • 2020-12-09 14:36

    Here's a check list to rule out this nasty bug:


    1- Grant the user FILE privileges in MySQL, phpMyAdmin generaly does not cover this privilege:

    GRANT FILE ON *.* TO 'db_user'@'localhost';
    

    2- Edit my.cnf in /etc/mysql/ or your mysql path:

    [mysql]
    local-infile=1
    [mysqld]
    local-infile=1
    

    3- In php.ini at /etc/php5/cli/ or similar:

    mysql.allow_local_infile = On
    

    Optionally you can run ini_set in your script:

    ini_set('mysql.allow_local_infile', 1);
    

    4- The database handler library must use the correct options.
    PDO:

    new PDO('mysql:host='.$db_host.'.;dbname='.$db_name, $db_user, $db_pass,
    array(PDO::MYSQL_ATTR_LOCAL_INFILE => 1));
    

    mysqli:

    $conn = mysqli_init();
    mysqli_options($conn, MYSQLI_OPT_LOCAL_INFILE, true);
    mysqli_real_connect($conn,server,user,code,database);
    

    5- Make sure that the INFILE command uses the absolute path to the file and that it exists:

    $sql = "LOAD DATA INFILE '".realpath(is_file($file))."'";
    

    6- Check that the target file and parent directory are readable by PHP and by MySQL.

    $ sudo chmod 777 file.csv
    

    7- If you are working locally you can remove the LOCAL from your SQL:

    LOAD DATA INFILE
    

    Instead of:

    LOAD DATA LOCAL INFILE
    

    Note: Remember to restart the MySQL and PHP services if you edit their configuration files.

    Hope this helps someone.

    0 讨论(0)
提交回复
热议问题