I know there are other solutions to this problem, but it did not work for me.
I am trying to execute a LOAD DATA LOCAL INFILE statement in a MySQL database via a Yii2 website. I have got this working in the past, so I am not sure why it doesn't work anymore.
I get the following error:
SQLSTATE[42000]: Syntax error or access violation: 1148 The used
command is not allowed with this MySQL version
I do have the following setting in my db.php config file.
'attributes' => [ PDO::MYSQL_ATTR_LOCAL_INFILE => true ],
I also tried disabling secure-file-priv to no avail.
EDIT
I tried running the following query from Workbench and it throws an error via Workbench as well. However, it works on MySQL 5.7. I am currently using 8.0
LOAD DATA LOCAL INFILE 'D:/Temp/6_attlog.dat' IGNORE
INTO TABLE att_log
FIELDS TERMINATED BY '\t' ENCLOSED BY '''' LINES TERMINATED BY '\r\n' IGNORE 0 LINES
(#id, `date_time`, `dev_id`, `mode`, `work_code`, `work_code1`) SET `id` = TRIM(#id)
EDIT 2
I got it to work with "LOAD DATA INFILE"...
...but it does not work with "LOAD DATA LOCAL INFILE"
Here's the code I was using...
$inFile = str_replace('\\', '/', realpath('uploads/'.$model->file->name));
Yii::$app->db->createCommand('
LOAD DATA LOCAL
INFILE \''.$inFile.'\' IGNORE
INTO TABLE att_log
FIELDS TERMINATED BY \'\\t\'
ENCLOSED BY \'\'\'\'
LINES TERMINATED BY \'\r\n\'
IGNORE 0 LINES
(#id, `date_time`, `dev_id`, `mode`, `work_code`, `work_code1`)
SET `id` = TRIM(#id)
')->execute();
unlink($inFile);
According to MySQL documentation, server needs to be configured to allow LOCAL keyword:
LOCAL works only if your server and your client both have been configured to permit it. For example, if mysqld was started with the local_infile system variable disabled, LOCAL does not work.
https://dev.mysql.com/doc/refman/8.0/en/load-data.html
Are you sure that your server is correctly configured to allow this keyword?
Just a suggestion
in PHP you can use single and double quote so you could build you command in a simple an more clear way eg:
$inFile = str_replace('\\', '/', realpath('uploads/'.$model->file->name));
Yii::$app->db->createCommand("
LOAD DATA LOCAL
INFILE '".$inFile."' IGNORE
INTO TABLE att_log
FIELDS TERMINATED BY '\t'
ENCLOSED BY '\'
LINES TERMINATED BY '\r\n\'
IGNORE 0 LINES
(#id, `date_time`, `dev_id`, `mode`, `work_code`, `work_code1`)
SET `id` = TRIM(#id)
")->execute();
unlink($inFile);
Related
when we import a csv by phpmyadmin using "csv using load data", we have this error in phpmyadmin and the import into our table from the csv is not done.
phpmyadmin version 4.4.15.10
With another version of phpmyadmin 4.4.15.8 with the same database and my same data server we have no problem.
thank you for your answers
Requête SQL :
LOAD DATA LOCAL INFILE '/tmp/phpRH1Q9u' INTO TABLE `glpi_corresp_usernames` FIELDS TERMINATED BY ';' ENCLOSED BY '"' ESCAPED BY '\\' LINES TERMINATED BY '\r\n'
MySQL a répondu: Documentation
#2000 - LOAD DATA LOCAL INFILE is forbidden, check mysqli.allow_local_infile
phpMyAdminWarning in ./libraries/dbi/DBIMysqli.class.php#262
mysqli_query(): LOAD DATA LOCAL INFILE forbidden
Backtrace
./libraries/dbi/DBIMysqli.class.php#262: mysqli_query(
object,
string 'LOAD DATA LOCAL INFILE \'/tmp/phpRH1Q9u\' INTO TABLE `glpi_corresp_usernames` FIELDS TERMINATED BY \';\' ENCLOSED BY \'"\' ESCAPED BY \'\\\\\' LINES TERMINATED BY \'\\r\\n\'',
integer 0,
)
./libraries/DatabaseInterface.class.php#183: PMA_DBI_Mysqli->realQuery(
string 'LOAD DATA LOCAL INFILE \'/tmp/phpRH1Q9u\' INTO TABLE `glpi_corresp_usernames` FIELDS TERMINATED BY \';\' ENCLOSED BY \'"\' ESCAPED BY \'\\\\\' LINES TERMINATED BY \'\\r\\n\'',
object,
integer 0,
)
./libraries/import.lib.php#170: PMA_DatabaseInterface->tryQuery(string 'LOAD DATA LOCAL INFILE \'/tmp/phpRH1Q9u\' INTO TABLE `glpi_corresp_usernames` FIELDS TERMINATED BY \';\' ENCLOSED BY \'"\' ESCAPED BY \'\\\\\' LINES TERMINATED BY \'\\r\\n\'')
./libraries/plugins/import/ImportLdi.class.php#158: PMA_importRunQuery()
./import.php#615: ImportLdi->doImport(array)
This helped my situation in phpMyAdmin:
run this command: show global variables like 'local_infile';
if value if OFF run: set global local_infile=true;
Then like #Guido Faecke said Check your php.ini for mysqli.allow_local_infile = On
in this line remove semicolon -> ;mysqli.allow_local_infile = On
restart apache and then you can run
LOAD DATA LOCAL INFILE...
I have a PHP script where I upload a CSV file and this one update/insert rows into mysql database, all is on ubuntu 18.04 server and this PHP script had many years working without problems, the server has more than 1 year working, suddenly a couple of weeks ago, with any change on server or software stop working.
The weird thing is it is not generating any error, no logs error, nothing, so i can't debug.
I read a lot this few days ago without any lucky, so i am not sure what to do.
On my tests and testing similar problems, i found this:
If i run the "Load data local infile" directly on the server it works
The file is uploading fine, i can give 777 permision and nothing changes
infile is enabled on mysql
apparmor is configured
user who connect to database has FILE on privileges
Here is the used code:
LOAD DATA LOCAL INFILE '/path/to/temp/file' INTO TABLE pagos_nuevos CHARACTER SET latin1 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES
(cuenta, #pago, #fpago) SET pago = CAST(REPLACE(REPLACE(#pago,',',''),'$','') AS DECIMAL(10,2)), fpago = STR_TO_DATE(#fpago, '%d/%m/%Y')
As I said at the begining, this is no new server, this is no new code, has many years working, when debug I only received this on screen:
Query: LOAD DATA LOCAL INFILE '/path/to/temp/file' INTO TABLE pagos_nuevos CHARACTER SET latin1 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES
(cuenta, #pago, #fpago) SET pago = CAST(REPLACE(REPLACE(#pago,',',''),'$','') AS DECIMAL(10,2)), fpago = STR_TO_DATE(#fpago, '%d/%m/%Y') failed
0:
Where is "0:" it must to be the error code and after ":" the description of the error.
If I remove LOCAL i get this error:
Query: LOAD DATA LOCAL INFILE '/path/to/temp/file' INTO TABLE pagos_nuevos CHARACTER SET latin1 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES
(cuenta, #pago, #fpago) SET pago = CAST(REPLACE(REPLACE(#pago,',',''),'$','') AS DECIMAL(10,2)), fpago = STR_TO_DATE(#fpago, '%d/%m/%Y')
not found (Errcode: 13 - Permission denied)
29: File '/var/www/html/testing/unicomer/pagos/datos/pagos/pagos al 29.csv' not found (Errcode: 13 - Permission denied)
I did a test with a user with ALL THE PRIVILEGES and do not work, so I don't know if this is a mysql issue or server issue.
Thank you very much for your help.
I'm trying to load a CSV file into a table. I'm getting a permission denied error:
Error importing CSV File: (1045) Access denied for user 'username'#'localhost'
(using password: YES)
SQL=LOAD DATA INFILE '//homeX/username/public_html/activator/uploadedCSV/20180603064917_csvtoimport.csv'
IGNORE INTO TABLE `csv_import_temp`
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\r\n'
IGNORE 1 ROWS
( contact_lname, contact_email ) ;
The csv file exists, and has permissions set to 0644.
The SQL statement is:
LOAD DATA INFILE '//homeX/username/public_html/activator/uploadedCSV/20180603064917_csvtoimport.csv'
IGNORE
INTO TABLE `csv_import_temp`
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\r\n'
IGNORE 1 ROWS
(
contact_lname,
contact_email
);
I've tried using it with both a leading single slash and the double slash (as above) in the filename. I've also tried running it in phpAdmin and get the same error message.
The site is hosted on a Hostgator shared account.
I've added code to verify that the file exists, and has the right permissions, and it says it does. But MySQL doesn't seem to be able to access the file.
Yes, I know that Joomla 2.5 is end of life, but for many reasons, this site can't be upgraded, and is only used for admin functions.
Any suggestions would be welcome as the alternative is to read the file with PHP and insert each row which would be much slower.
The first 4 lines of the CSV:
"name","email"
"John Smith","xxx1#yyy.edu"
"John Doe","xxx2#yyy.edu"
"Jane Doe","xxx3#yyy.edu"
This error is usually due to you not including the LOCAL keyword. Try:
LOAD DATA LOCAL INFILE '//homeX/username/public_html/activator/uploadedCSV/20180603064917_csvtoimport.csv'
IGNORE
INTO TABLE `csv_import_temp`
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\'
LINES TERMINATED BY ''
IGNORE 1 ROWS
(
contact_email,
contact_email
);
The permission error is misleading, it relates to trying to the mysql system directories rather than the file you are trying to read.
I want to save my CSV File data to my database and using "Load Data Infile" as-
$sql="LOAD DATA INFILE '".$tempLoc."'
INTO TABLE `tbl_csv`
FIELDS
TERMINATED BY ','
ENCLOSED BY '\"'
LINES
TERMINATED BY '\r\n'
(`mobile`, `name`, `email`) ";
I keep getting syntax errors for this query statement.
Error: CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 13 Can't get stat of '/tmp/phpikiYm6' (Errcode: 2). //tmp/phpikiYm6 is the file name
Sorry but I have no much reputations to comment on your question, however I cant see that you give the file extension to the query, have you tried:
$sql="LOAD DATA INFILE '".$tempLoc.".csv'
INTO TABLE tbl_csv
FIELDS
TERMINATED BY ','
ENCLOSED BY '\"'
LINES
TERMINATED BY '\r\n'
(mobile, name, email) ";
Follow given checks to fix this problem :
perror 13
OS error code 13: Permission denied
Check these variables (using SHOW VARIABLES):
have_csv = YES
local_infile = ON
If those are correct, then...
The .csv file, I think, needs to be readable and locatable by the "mysql" user. Note that both "r" and "x" is needed on directories to let a user 'read' and 'search' the directory.
Suggest you change permissions on the file and directories of this part to 777:
I'm fairly new to php and CakePHP, and I'm trying to execute the following code after uploading a text file (2kb):
$filefullname = $this->request->data['File']['file']['tmp_name'];
debug($filefullname);
move_uploaded_file($filefullname, WWW_ROOT.'tmp.txt');
$query = 'LOAD DATA LOW_PRIORITY INFILE "'.WWW_ROOT.'tmp.txt'.'" INTO TABLE agencies FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY """" LINES TERMINATED BY "\r" IGNORE 1 LINES';
debug($query);
$this->Agency->query($query);
Though the file can be found, I get the following output:
'C:\Windows\Temp\phpB413.tmp'
'LOAD DATA LOW_PRIORITY INFILE "C:\Data\myphpapplication\app\webroot\tmp.txt" INTO TABLE agencies FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY """" LINES TERMINATED BY "\r" IGNORE 1 LINES'
Error: SQLSTATE[HY000]: General error: 29 File 'C:\Data\myphpapplication\app\webroot\tmp.txt' not found (Errcode: 13)
SQL Query: LOAD DATA LOW_PRIORITY INFILE "C:\Data\myphpapplication\app\webroot\tmp.txt" INTO TABLE agencies FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY """" LINES TERMINATED BY "\r" IGNORE 1 LINES
How can I fix this?
Most likely the file is there, but MySQL doesn't allow you to read files from that location. MySQL is fairly restrictive from where it reads LOAD DATA ... INFILE, with good reasons. You may need to move your file first to a path that MySQL can read from, or change the settings for your MySQL server.
From the MySQL 5.1 manual:
Note that, in the non-LOCAL case, these rules mean that a file named
as ./myfile.txt is read from the server's data directory, whereas the
file named as myfile.txt is read from the database directory of the
default database. [...]
And:
Windows path names are specified using forward slashes rather than
backslashes. If you do use backslashes, you must double them.
#JvO is guiding you down the right path. I think you're missing a couple of important elements, one of which is moving the uploaded file from its initial temp directory on the server to a place where MySQL can officially read it (it won't read it from a temp directory for security reasons).
Here's an example from a project I did, which uploaded a CSV file and then MOVED it to a readable directory. I first uploaded it with the move_uploaded_file function, and then created a variable describing the uploaded path of the new file for use in a LOAD DATA query.
move_uploaded_file($_FILES["fCSV"]["tmp_name"],"user_uploads/SG".$dapID ."/". $_FILES["fCSV"]["name"]);
$csvfile = "user_uploads/SG".$dapID ."/". $_FILES["fCSV"]["name"];
In your LOAD DATA query, you're missing the "LOCAL" keyword, which tells MySQL to read the file from a specified directory, otherwise it's looking for it in the database directory (the use of "Field1" etc. below is for demo purposes only and you should have your real table field names in there):
$sqlLoad = "LOAD DATA LOCAL INFILE '".$csvfile."' INTO TABLE my_table FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES (Field1,Field2,Field3,Field4) SET ID =".$id
Lastly, you really need to read through this page in the MySQL manual. It covers many of the issues you're dealing with along with providing examples:
MySQL LOAD DATA INFILE Syntax