I want to import a csv file from my local computer to a mysql server on a remote machine using LOAD DATA LOCAL INFILE. When I try this without LOCAL and my file is stored at the remote computer it works (file and mysql on the same server), but is not working when the mysql server is on a remote computer.
LOAD DATA LOCAL INFILE 'C:/wamp/www/accesos/uploaded_files/wawawa.csv'
INTO TABLE eventos
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
(#skip,#skip,#skip,#skip,#skip,#skip,#skip,#skip,#skip,nombre,#skip,num_servo,#fecha,hora,#skip,num_tarjeta,#archivo_procedencia)
set archivo_procedencia = 'test_file', fecha = STR_TO_DATE(#fecha, '%d/%m/%Y');
So, what I'm doing from my computer: I'm connecting to phpmyadmin on the remote computer.
Executing the above code but is not working. But it works if I remove "LOCAL" and move my file to the remote computer and change the path to match the file path.
The error is:
Can't find file 'C:/wamp/www/accesos/uploaded_files/wawawa.csv'.
Any ideas on how to do this?
From documentation:
The LOCAL keyword affects expected location of the file and error handling, as described later. 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”.
The LOCAL keyword affects where the file is expected to be found:
If LOCAL is specified, the file is read by the client program on the client host and sent to the server. The file can be given as a full path name to specify its exact location.
In Your case client program is phpmyadmin. So it expects csv file is on server where phpmyadmin is installed. To work with files on your local computer, you must use MySQL client program installed locally on your computer.
The LOAD DATA INFILE statement loads a file on machine the MySQL server is running on. (the remote server)
Use LOAD DATA LOCAL INFILE to load a file located on your local machine.
Related
This code is working fine for me to load a huge .csv file into ecolo-dis-tbl table in Localhost using PHPMyAdmin and Wampserver
LOAD DATA INFILE 'C:/Data/Spreatsheets/Data-Single.csv'
INTO TABLE `ecolo-dis-tbl`
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
Now can some one please let me know how I can address the file in production server like Godaddy? I mean instead of C:/Data/Spreatsheets/ what path should use if I load the file into the root
Using phpMyAdmin you can select the Import tab and work with the file directly from your local machine. To use the SQL code you've posted here, you'll first have to upload the file to your production database server by means of SSH, FTP, or whatever other way you put files on the server. Note that for some hosts the web server is on a different machine from the database server, so you have to make sure they go on the database server. Then just use the new path (maybe something like /home/suffii/Data-Single.csv) in place of C:/Data/Spreatsheets/Data-Single.csv.
I keep getting an error 29 that says file is not found with the following syntax and I cannot figure out why:
LOAD DATA INFILE 'C:/Users/rkartj2/Desktop/LOINCSUNQUESTV2.txt'
INTO TABLE xiao
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
Table xiao has been created in MySQL 5.7 and LOINCSUNQUESTV2.txt was an Excel spreadsheet created on a Windows machine.
While I have not used version 5.7 of Mysql yet, the error you are receiving is the same in previous versions. ERROR 29 (HY000): File 'C:/Users/rkartj2/Desktop/LOINCSUNQUESTV2.txt' not found
This may seem a bit misleading to a newb especially since you can go to your Desktop and see that the LIONSUNQUESTV2.txt file is there.
I see one of two possibilities here which is either that Mysql does not have permissions to access the file from the current directory or that it does not know that the file is on your local server.
You can try using LOAD DATA LOCAL INFILE. From dev.mysql
If that does not work, you need to move file to a location that mysql has access to, such as the Mysql data directory. In version 5.6 of a stock vanilla install it would be in this directory: C:\ProgramData\MySQL\MySQL Server 5.6. Try moving the file there. I believe that Mysql looks at this location by default, so I would change LOAD DATA INFILE 'C:/Users/rkartj2/Desktop/LOINCSUNQUESTV2.txt' to LOAD DATA INFILE 'LOINCSUNQUESTV2.txt'
I get the error message. I am using phpAdmin
2000 - Can't find file 'c:/tmp/userlist.csv'.
LOAD DATA LOCAL INFILE 'c:/tmp/userlist.csv'
INTO TABLE users2
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\n';
I have googled and that error seems to be a lot of other things too.
It does exist as i have just pasted into windows and it opens the file.
With LOAD DATA LOCAL the file is read locally and it needs to be accessible by the client. You mentioned you are using phpMyAdmin (a MySQL client), so the file needs to be accessible from wherever phpMyAdmin is running from.
If phpMyAdmin isn't installed on your computer, then you need to upload the file to the server where it's located and change the file path accordingly.
Another solution would be to install a MySQL client on your computer to run the query, so it can read the file locally. I recommend MySQL Workbench.
The only thing I had to do in the end was press IMPORT inside phpMyadmin. It would have been nicer if somebody had told me that instead of mucking around for two days trying to import a csv via sqls.
I'm loading a csv file into a mysql instance running on local host. I can do this using the LOAD DATA LOCAL INFILE syntax, but what I'm wondering is why I need the LOCAL if the server and file are on the same machine. Without the local, I get an error saying:
ERROR 13 (HY000): Can't get stat of '/path/to/myfile.csv' (Errcode: 13)
That is because the system account under which MySQL is working have no rights to read this file.
When you don't specify LOCAL the file is being read directly by the MySQL server process and have to have rights to read this file.
When you add LOCAL the file is being read by mysql client program not the server process and in your case apparently has access to your csv file, which is I presume is located somewhere in the user directory of an account under which you're working.
If you put this file to a directory where MySQL process has rights to read from (e.g. /tmp/myfile.csv) LOAD DATA INFILE will work just fine.
LOAD DATA INFILE
The LOCAL keyword affects where the file is
expected to be found:
If LOCAL is specified, the file is read by the client program on the client host and sent to the server. The file can be given as a full
path name to specify its exact location. If given as a relative path
name, the name is interpreted relative to the directory in which the
client program was started.
If LOCAL is not specified, the file must be located on the server host and is read directly by the server.
The best place to look these up are in MySQL docs. Check out http://dev.mysql.com/doc/refman/5.1/en/load-data.html
The --local option causes mysqlimport to read data files from the
client host.
I need to load a CSV file from client machine to MySQL server database.
I am trying LOAD DATA INFILE.
My confusion is regarding ACCESS PERMISSIONs required to use
- LOAD DATA INFILE
- LOAD DATA LOCAL INFILE
Earlier I believed that I need FILE privilege to use both of them.
I came across this line in mysql documentation :
when reading text files located on the server, the files must either reside in the database directory or be readable by all. Also, to use LOAD DATA INFILE on server files, you must have the FILE privilege. See Section 6.2.1, “Privileges Provided by MySQL”. For non-LOCAL load operations, if the secure_file_priv system variable is set to a nonempty directory name, the file to be loaded must be located in that directory.
Looking at this, I got confused.
Do I need FILE privilege to load FILE from client machine using LOCAL option?
We do not need FILE privilege to LOAD data-file from a remote machine to MySQL Server. We need --local-infile option on Client machine enabled for that.
We need FILE privilege when we are trying to LOAD a data-file which is present on MySQL server. Additionally, mysql demon should also have access to READ from directory where data-file is placed.