How import csv file into mysql /var/lib/mysql-files/ - mysql

I need to import csv file into mysql:
first I tried:
LOAD DATA LOCAL INFILE 'C:/Users/MJ/Desktop/nysgi1.csv' INTO TABLE gift FIELDS TERMINATED BY ',' (giftid,date,udate,vamount);
errors:
ERROR 1148 (42000): The used command is not allowed with this MySQL version
And I then followed some instruction to successfully set local_infile ON ,and then I restarted mysql by using service mysql restart.However it not works,still the same error.
Then I tried to use directly:
LOAD DATA INFILE 'C:/Users/MJ/Desktop/nysgi1.csv' INTO TABLE gift FIELDS TERMINATED BY ',' (giftid,date,udate,vamount);
errors:
The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
So I use:
SELECT ##global.secure_file_priv;
and find out the directory is:
/var/lib/mysql-files/
My question is how to import the csv file to /var/lib/mysql-files/ using command line.
Thanks!

Seems you are having exactly the same problem i have,
please use scp command to place your csv file into linux directory.

Related

MySQL LOAD DATA LOCAL INFILE difference in Clients

I am trying to run this query
LOAD DATA LOCAL INFILE "C:/myfile.txt"
IGNORE INTO TABLE mydb.mytable
FIELDS TERMINATED BY ';'
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
If I run it in MySQL Workbench (v8.0.16) I get an error
Error Code: 1148. The used command is not allowed with this MySQL version
However if I run it in SQLYog (Professional v12.09) it works fine.
I am running both bits of code using the same computer, with the same user, on the same database running on the same server.
That server has local_infile = 1
As far as I can tell the only difference in these queries is the SQL Client being used.
My assumption is that it is the connection string / settings that is different between the two as a default, however I can't find any documentation on this.
Why would this be and how can I fix it?
Thanks
In linux you have to start mysql client in command line as.
mysql -u root -ppassword --local-infile=1
you should also set local_infile = 1 in configuration file

secure-file-priv error while importing csv file in sql

I am trying to import csv file in sql, please find the below code:
LOAD DATA INFILE 'C:\ProgramData\MySQL\MySQL Server 8.0\Uploads\Assignment\Auto.csv' INTO TABLE assignments.Auto
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;
Its giving me the error:
Error Code: 1290. The MySQL server is running with the --secure-file-priv option so it cannot execute this statement 0.016 sec
I then tried with keyword LOCAL(as mentioned in some of the solutions) and it gave me error:
Error Code: 1148. The used command is not allowed with this MySQL version 0.000 sec
As i found many solution to this, so i checked the value of this variable manually in session with command:
SHOW VARIABLES LIKE "secure_file_priv";
the output for this is :
secure_file_priv C:\ProgramData\MySQL\MySQL Server 8.0\Uploads\
**This is the same location i am using in the load data path above.
I checked the C:\ProgramData\MySQL\MySQL Server 8.0\my.ini file and it has the below details:
Secure File Priv.
secure-file-priv="C:/ProgramData/MySQL/MySQL Server 8.0/Uploads"
Can someone please tell me what is the issue here and how can i resolve this.
SHOW VARIABLES LIKE "secure_file_priv";
This command will show the path, for example
C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/
Put your file in the same path in uploads
Also check the backslash in both the path. If it is different, change in your code.
I did the two steps and it got resolved in MySQL 8.
If you want to use LOAD DATA LOCAL INFILE … you need to set MySQL Server global variable local_infile to true.
SET GLOBAL local_infile = true;
You need also pass --local-infle option to mysql command-line client
mysql --local-infile -hlocalhost -uroot -p
About secure file priv error - does your Auto.csv file is on the same machine where MySQL Server is running?

Error 1148 (42000) using LOAD DATA LOCAL INFILE in MySQL

I'm trying to load data into a MySQL table using LOAD DATA LOCAL INFILE using:
LOAD DATA LOCAL INFILE 'f.csv' INTO TABLE tableName
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\\r\\n'
IGNORE 1 LINES
(C1, C2, C3, C4)
SET cmj = REPLACE(#cmj,',','.');
OS : Ubuntu 14.04
MySQL : 5.7
In file my.cnf I have this config:
[mysqld]
local-infile=1
[mysql]
local-infile=1
[client]
loose-local-infile=1
local-infile=1
I'm getting the following error:
Message: SQLSTATE[42000]: Syntax error or access violation: 1148 The
used command is not allowed with this MySQL version, query was: LOAD
DATA LOCAL INFILE 'f.csv' INTO TABLE tableName FIELDS TERMINATED BY
';' LINES TERMINATED BY '\r\n' IGNORE 1 LINES (C1, C2, C3, C4)
I think the problem is related to new line in MySQL
Thanks for your help
EDIT
In my file.csv I have two lines (header + values). If I update my request "IGNORE 1 LINES" to "IGNORE 0 LINES" the first line (header) is inserted.
All lines are considered as one line!!!!!
use --local-infile while connecting database.
mysql -hhostname -uusername -p --local-infile test -A
Hope this helps.
According to the MySQL reference manual v5.7, the error you received can result from LOCAL file capability being disabled either on the server or client side.
Running mysql with the --local-infile option will enable LOCAL capability on the client side, but this won't help if it's disabled on the server, where it's controlled by the system variable local_infile. The easiest way to fix this is to reset the system variable during runtime with the SET command:
SET GLOBAL local_infile = 1;
Note: In later versions of MySQL you can make the variable change persist over server restarts with the command
SET PERSIST local_infile = 1;

Import data to MySQL using CSV

I am trying to understand Data Warehousing using the book 'Dimensional Data Warehousing with MySQL'. In Chapter 2 I need to insert the data to a table in MySQL.
I am trying this code
TRUNCATE customer_stg;
LOAD DATA INFILE 'customer2.csv'
INTO TABLE customer_stg
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES(
customer_number,
customer_name,
customer_street_address,
customer_zip_code,
customer_city,
customer_state
);
I am using lampp on Ubuntu 12.04.2. The file 'customer2.csv' has been copied to the folder '/opt/lampp/var/mysql/dw'.
In MySQL I am using an user 'dwid' with password as 'password'.
The file permissions of the csv file is set to
-rwxr-xr-x 1 mysql mysql 469 Oct 31 2005 customer2.csv
but still I am unable to load the data in the table. The loading is possible from the user 'root'.
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. I learned this from some other question in stackoverflow, I couldn't find the link, I had the answer saved. Hope this will help.

MySQL LOAD DATA Error can't resolve...!

mysql> LOAD DATA INFILE '/abc.txt' INTO TABLE test1 FIELDS TERMINATED BY '|';
ERROR 13 (HY000): Can't get stat of '/abc.txt' (Errcode: 2)
I used LOAD DATA as per the syntax. But getting the above error. can anybody tell what is the error..!?
I tried searching google dev.mysql can't find what error it is??
Thanks in advance
If the file is on your MYSQL server try using the full path.
LOAD DATA INFILE '/var/tmp/abc.txt' INTO TABLE test1 FIELDS TERMINATED BY '|';
If the file isn't on the server and it is on your local machine, and your user has access to SCP files between the servers ( try this
scp abc.txt mysqlserver:
) then you should be able to do this (using full path again) ..
LOAD DATA LOCAL INFILE '/var/tmp/abc.txt' INTO TABLE test1 FIELDS TERMINATED BY '|';
If that doesn't work.. try renaming the file to test1.txt and using the 'mysqlimport' command?
Login to the mysql console using the following flag:
mysql -uroot -p --local-infile
and then enter the password.
After that execute the load data command as follows:
LOAD DATA LOCAL INFILE '/abc.txt' INTO TABLE test1 FIELDS TERMINATED BY '|';