MySQL Workbench Load Data Local Infile error - mysql

I have been running into a problem with MySQL's Load Data Local Infile command. I know that they disabled it by default due to a security threat. I have a SQL Script that I would like to run in MySql Workbench. I keep getting the error "1148. The used command is not allowed with this MySQL version." I have been able to get this to work from the command line using command line arguements. However I havent had any success in the Workbench. Any help would be appreciated. Here is my script.
DROP DATABASE IF EXISTS a2004TS;
CREATE DATABASE IF NOT EXISTS a2004TS;
USE a2004TS;
DROP TABLE IF EXISTS a2004TS_1;
CREATE TABLE a2004TS_1(
Version CHARACTER(25),
Dsetid CHARACTER(6),
V040001 DOUBLE,
..
.
);
SET GLOBAL local_infile = 1;
LOAD DATA LOCAL INFILE '/home/output/sqlCSV/a2004TS.sqlscripts.table1.csv'
INTO TABLE a2004TS_1
FIELDS TERMINATED BY ',' ENCLOSED BY'"'
LINES TERMINATED BY'
'(columns......);

If the server is started with the option to disable LOAD DATA then a client has to explicitly call mysql_options(...) to enable it (http://dev.mysql.com/doc/refman/5.5/en/load-data-local.html). This does not happen in WB, so you cannot use it to load a file currently. Instead use the command line client for this.
You could file a feature request at http://bugs.mysql.com to have handling implemented that allows the import (e.g. a preference setting).

Related

Load data infile does nothing

I'm kinda new to SQL. I am trying to add data to an alredy created table, the csv looks like these, it is 132645 lines long:
iso_code;continent;location;date;population;total_cases;new_cases;new_cases_smoothed;total_deaths;new_deaths;new_deaths_smoothed;total_cases_per_million;new_cases_per_million;new_cases_smoothed_per_million;total_deaths_per_million;new_deaths_per_million;new_deaths_smoothed_per_million;reproduction_rate;icu_patients;icu_patients_per_million;hosp_patients;hosp_patients_per_million;weekly_icu_admissions;weekly_icu_admissions_per_million;weekly_hosp_admissions;weekly_hosp_admissions_per_million
AFG;Asia;Afghanistan;24/02/2020;398354280;50;50;NULL;NULL;NULL;NULL;126;126;NULL;NULL;NULL;NULL;NULL;NULL;NULL;NULL;NULL;NULL;NULL;NULL;
AFG;Asia;Afghanistan;25/02/2020;398354280;50;0;NULL;NULL;NULL;NULL;126;0;NULL;NULL;NULL;NULL;NULL;NULL;NULL;NULL;NULL;NULL;NULL;NULL;
AFG;Asia;Afghanistan;26/02/2020;398354280;50;0;NULL;NULL;NULL;NULL;126;0;NULL;NULL;NULL;NULL;NULL;NULL;NULL;NULL;NULL;NULL;NULL;NULL;
AFG;Asia;Afghanistan;27/02/2020;398354280;50;0;NULL;NULL;NULL;NULL;126;0;NULL;NULL;NULL;NULL;NULL;NULL;NULL;NULL;NULL;NULL;NULL;NULL;
AFG;Asia;Afghanistan;28/02/2020;398354280;50;0;NULL;NULL;NULL;NULL;126;0;NULL;NULL;NULL;NULL;NULL;NULL;NULL;NULL;NULL;NULL;NULL;NULL;
And my sql query is this:
LOAD DATA LOCAL INFILE 'C:\\Users\\Usuario\\Desktop\\CovidDeaths.csv' INTO TABLE coviddeaths
columns terminated by ';'
LINES TERMINATED BY '\r\n';
I have some alredy uploaded data to the coviddeaths table using the import wizards but the upload was really slow, so tried to finish importing using the LOAD DATA INFILE query.
To see how many lines have been alredy uploaded I used
select count(*) from coviddeaths;
And threw:
count(*)
11312
When I try to run the LOAD DATA LOCAL INFILE query, nothing happens. The mouse pointer spins for a second like it tries to run it but it doesnt add any additional row. It doesnt throw any errors like the ones I have read about Error Code: 1148. The used command is not allowed with this MySQL version. I also tried to place the csv file in the Uploads folder, where my Secure File Priv option pointed to in my.ini:
# Secure File Priv.
secure-file-priv="C:/ProgramData/MySQL/MySQL Server 8.0/Uploads"
It just does nothing. Any ideas?
Thanks in advance!
EDIT 2: Database Schema:
The error comes from the fact that both client and server must enable an option to allow local datafiles to be imported. If you use the command-line client, you would need to open the client like this:
mysql --local-infile -u root -p
In addition, the MySQL Server would need to be configured with the local-infile option.
The secure-file-priv is not needed, because that controls importing or exporting files on the server, not from the client.
See also ERROR 1148: The used command is not allowed with this MySQL version or my answer to "mysql 8.0" local infile fails I tried and display the settings. Could it be permission issues?
The error message is unusually obscure. MySQL error messages are usually more accurate and informative. I logged a bug in 2019 to request an improved error message, and they did fix it in MySQL 8.0.

Cannot Remotely Load CSV into MySQL

So I'm remotely accessing a MySQL server and I've tried everything under the sun in order to invoke the following command:
LOAD DATA INFILE 'C:\Users\user1\Desktop\csv\mycsv.csv'
INTO TABLE mytable
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
However I get the following error:
Error Code: 1290. The MySQL server is running with the --secure-file-priv
option so it cannot execute this statement
I then tried modifying the statement and adding LOCAL as such:
LOAD DATA LOCAL INFILE 'C:\Users\user1\Desktop\csv\mycsv.csv'
INTO TABLE mytable
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
and received the following error
Error Code: 1148. The used command is not allowed with this MySQL version
I ran the following 2 commands to check my global variables pertaining to these two errors which returned the following:
SHOW VARIABLES LIKE "secure_file_priv";
Returns NULL
And
SHOW GLOBAL VARIABLES LIKE 'local_infile';
Returns ON
I contacted the system administrator to change the my.ini. I set my local secure_file_priv to a directory on my PC with no luck. I have no access to the PC the server lies on so I can't move files to any local directories, only remotely accessed ones.
I have referred to every solution under How should I tackle --secure-file-priv in MySQL? and How to import CSV file to MySQL table with no luck.
If you have any other ideas please let me know. Thanks
secure_file_priv is set at start up and can not be dynamically changed. Make the following addition to your my.ini file, then restart mySQL.
secure-file-priv="C:\Users\user1\Desktop\csv\"

MySQL into output file error

I am very new with SQL but I have to extract some fields of a table stored in a sql file.
I have installed mysql and create the database and source the file. Now I wanted to execute a sql request in order to read all the elts of the table, extract the interesting fields and write them into cvs file:
SELECT * INTO OUTFILE '/home/cr/database/Dump2/program_info.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY '\\' LINES TERMINATED BY '\n' FROM program_info;
When running the command I have the following error message:
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
Does anyone knows how to solve it ?
I have struggled with this message all the afternoon but could not .
I am working on Linux ubuntu and my config file is /etc/my.cfg
[mysqld]
read-only = 0
secure-file-priv = ""
Thank you for your help
MySQL is actually working as intended. The MySQL server booted with the --secure-file-priv option which basically restricts you from saving output into random directories. You need to check the startup parameters.
To do so, run the following command in the MySQL shell:
SHOW VARIABLES LIKE 'secure_file_priv';
The output would be what the MySQL Server currently thinks is the permitted location. You should be able to save your output to this location.
To disable the restriction, you need to edit the configuration file that declares it and restart the MySQL Server after changing the option.
MySQL could be reading the my.ini (or my.cnf) configuration file from a variety of locations. See Using Option Files for more information.

How MySQL server is running with the google

When I tried to upload a bulk data for a table I get this Error.
ERROR 1290 (SQLSTATE HY000): The MySQL server is running with the google so it cannot execute this statement
The Command which I used for uploading a data in command line (google_sql.cmd)
sql> LOAD DATA INFILE 'C\:List.txt' INTO TABLE Associate_List FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n';
Please help Me.
What syntax I have to use and how do I upload a data in bulk?
Typing your error message in a search engine (for example Google) makes me think that you are using Google Cloud SQL and you are attempting to run a command that isn't supported, as explained in Does Google Cloud SQL support all the features of MySQL?:
Unsupported MySQL statements:
Sending any of the following types of SQL statements will generate an
error with the message "Error 1290: The MySQL server is running with
the google option so it cannot execute this statement"
LOAD DATA INFILE
SELECT ... INTO OUTFILE
SELECT ... INTO DUMPFILE
INSTALL PLUGIN ...
UNINSTALL PLUGIN
CREATE FUNCTION ... SONAME ...
To bulk load data, upload your data to Google Cloud Storage first and then import it from there. Please see https://developers.google.com/cloud-sql/docs/import_export

access denied for load data infile in MySQL

I use MySQL queries all the time in PHP, but when I try
LOAD DATA INFILE
I get the following error
#1045 - Access denied for user 'user'#'localhost' (using password: YES)
Does anyone know what this means?
I just ran into this issue as well. I had to add LOCAL to my SQL statement.
For example, this gives the permission problem:
LOAD DATA INFILE '{$file}' INTO TABLE {$table}
Add LOCAL to your statement and the permissions issue should go away. Like so:
LOAD DATA LOCAL INFILE '{$file}' INTO TABLE {$table}
I had this problem. I searched around and did not find a satisfactory answer. I summarise below the results of my searches.
The access denied error could mean that:
'user'#'localhost' does not have the FILE privilege (GRANT FILE on *.* to user#'localhost'); or,
the file you are trying to load does not exist on the machine running mysql server (if using LOAD DATA INFILE); or,
the file you are trying to load does not exist on your local machine (if using LOAD DATA LOCAL INFILE); or,
the file you are trying to load is not world readable (you need the file and all parent directories to be world-readable: chmod 755 directory; and, chmod 744 file.dat)
Try using this command:
load data local infile 'home/data.txt' into table customer;
This should work. It worked in my case.
Ensure your MySQL user has the FILE privilege granted.
If you are on shared web hosting, there is a chance this is blocked by your hosting provider.
I found easy one if you are using command line
Login asmysql -u[username] -p[password] --local-infile
then SET GLOBAL local_infile = 1;
select your database by use [db_name]
and finally LOAD DATA LOCAL INFILE 'C:\\Users\\shant\\Downloads\\data-1573708892247.csv' INTO TABLE visitors_final_test FIELDS TERMINATED BY ','LINES TERMINATED BY '\r \n' IGNORE 1 LINES;
The string from Lyon gave me a very good tip: On Windows, we need to use slahes and not backslashes. This code works for me:
File tempFile = File.createTempFile(tableName, ".csv");
FileUtils.copyInputStreamToFile(data, tempFile);
JdbcTemplate template = new JdbcTemplate(dataSource);
String path = tempFile.getAbsolutePath().replace('\\', '/');
int rows = template.update(MessageFormat
.format("LOAD DATA LOCAL INFILE ''{0}'' INTO TABLE {1} FIELDS TERMINATED BY '',''",
path, tableName));
logger.info("imported {} rows into {}", rows, tableName);
tempFile.delete();
If you are trying this on MySQL Workbench,
Go to connections -> edit connection -> select advanced tab
and add OPT_LOCAL_INFILE=1 in the 'Others' text field.
Now restart the connection and try.
I ran into the same issue, and solve it by folowing those steps :
activate load_infile variable
grand file permission to my custom mysql user
deactivate secure_file_priv variable (my file was uploaded by the webserver to the /tmp folder which is of course not the secured directory of myslq /var/lib/mysql-file)
For this 3rd point, you can refer to : https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_secure_file_priv
BR,
AD
This happened to me as well and despite having followed all the steps described by Yamir in his post I couldn't make it work.
The file was in /tmp/test.csv with 777 permissions. The MySQL user had file permissions, LOCAL option was not allowed by my MySQL version, so I was stuck.
Finally I was able to solve the problem by running:
sudo chown mysql:mysql /tmp/test.csv
I discovered loading MySQL tables can be fast and painless (I was using python / Django model manager scripts):
1) create table with all columns VARCHAR(n) NULL e.g.:
mysql> CREATE TABLE cw_well2( api VARCHAR(10) NULL,api_county VARCHAR(3) NULL);

2) remove headers (first line) from csv, then load (if you forget the LOCAL, you’ll get “#1045 - Access denied for user 'user'#'localhost' (using password: YES)”):
mysql> LOAD DATA LOCAL INFILE "/home/magula6/cogswatch2/well2.csv" INTO TABLE cw_well2 FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'
    -> ;
Query OK, 119426 rows affected, 19962 warnings  (3.41 sec)

3) alter columns:
mysql> ALTER TABLE cw_well2 CHANGE spud_date spud_date DATE;
mysql> ALTER TABLE cw_well2 CHANGE latitude latitude FLOAT;
voilà!
I was trying to insert data from CSV to MYSQL DB using python.
You can try the below method to load data from CSV to Database.
Make a connection with the Database using pymysql or MySQL.connector any library you want in python.
Make Sure you are able to use the in-line while connecting for that while providing host, user, and password try to add local_inline=True.
Skipping to load data part.
sql = f'''LOAD DATA LOCAL infile "filename.csv" INTO TABLE schema.tablename FILED TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n'''' Note: If you have column names in CSV, use IGNORE ROW 1 LINES. The execute the sql by: cursor.execute(sql) conn.commit() conn.close()
It probably means that the password you supplied for 'user'#'localhost' is incorrect.