specify OUTFILE path - mysql

I am using MySQL database on a Ubuntu machine, and running the following SQL statement:
SELECT id,name FROM cars
INTO OUTFILE 'my_cars.dat'
LOAD DATA INFILE 'my_cars.dat' INTO TABLE all_cars(id, name);
It works. But how can I also specify the path to where the my_cars.dat file is put??
I mean , for example, I want the my_cars.dat file to be put under /data/temp , how can I specify this??
I tried to use ... INTO OUTFILE '/data/temp/my_cars.dat' ... but it does not work...
I got error :
ERROR 1 (HY000): Can't create/write to file '/data/temp/my_cars.dat' (Errcode: 13)

What user are you running mysql as? Does it have write permissions to /data/temp?

A couple potential reasons:
(and I suspect it's this) The outfile is written by the client, so it can only write to where you have write privileges. Assuming /data/temp exists (see #2), it is most likely owned by mysql:mysql or root:root, and privs 700.
MySQL's default location (in Ubuntu, at least) is /var/lib/mysql, so make sure that /data/temp exists.

Related

Is there a standard way to get LOAD INFILE working?

Load data infile has always been a pain in the ass to set up and there does not seem to be standard way to get it to work but I have always been able to get it to work ….eventually except now.
I am struggling with
Error Code: 29. File '/loadinfile/file.csv' not found (OS errno 13 - Permission denied)
The system is as follows:
MySQL version 8.0.29-0ubuntu0.22.04.2
Ubuntu 22.04
The load file directory is owned by mysql:mysql and same with the file and I have tried chmod 757 and even 777
In the config file I have tried the following variations:
secure_file_priv = "/loadinfile/"
secure_file_priv = '/loadinfile/'
secure_file_priv = ""
secure_file_priv = ''
The following is also set :
[mysqld]
local-infile = 1
[client]
loose-local-infile=1
Changing
LOAD DATA INFILE '/loadinfile/file.csv' INTO TABLE table
to
LOAD DATA LOCAL INFILE '/loadinfile/file.csv' INTO TABLE table
changes the error message to:
Error Code: 2. File '\loadinfile\file.csv' not found (OS errno 2 - No such file or directory)
Error Code: 2. File '\loadinfile\file.csv' not found (OS errno 2 - No such file or directory)
Above message indicates that your directory where the file is located is wrong! You must define the full path:
For example In Mac If your file resides on Desktop,It must be like this :
LOAD DATA LOCAL INFILE '/Users/computer_name/Desktop/filename.file_extension' INTO TABLE table
In windows
LOAD DATA LOCAL INFILE 'C:\Users\computer_name\Desktop\filename.file_extension' INTO TABLE table
I hope It solves your problem.
Error Code: 2. indicates invalid file path. Keep the LOCAL INFILE option and either specify the full path of the CSV file or make sure the CSV file path is correct. give it a try with Full path and see if it solves the problem or not.
From MySQL Docs
If LOCAL is specified, the file must be located on the client host.
The client program reads the file, locating it as follows:
If the file name is an absolute path name, the client program uses it
as given.
If the file name is a relative path name, the client program looks for
the file relative to its invocation directory
Edited
Make sure your mysqld.cnf or your main MySQL config file has the following variables
secure_file_priv = ""
local_infile=1
And your Global variable local_infile is OFF, turn it ON.
you can check that if you run this query
SHOW GLOBAL VARIABLES LIKE 'local_infile';
if it is OFF, turn it ON
And lastly, Make sure you restart your SQL service to reflect the changes. you can do that if you execute
service mysql restart
Hope that solves your problem

DatabaseError: 1 (HY000): Can't create/write to file '2015-04-06 20:48:33.418000'.csv (Errcode: 13 - Permission denied)

I am designing an application in Python and trying to write to a CSV file, but I am getting this error:
DatabaseError: 1 (HY000): Can't create/write to file '2015-04-06 20:48:33.418000'.csv (Errcode: 13 - Permission denied)
The Code:
def generate_report(self):
conn=mysql.connector.connect(user='root',password='',host='localhost',database='mydatabase')
exe2 = conn.cursor()
exe2.execute("""SELECT tbl_site.Site_name, State_Code, Country_Code,Street_Address, instrum_start_date, instrum_end_date, Comment INTO OUTFILE %s FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n'FROM tbl_site JOIN tbl_site_monit_invent ON site_id = tbl_Site_site_id """, (str(datetime.datetime.now()),))
I can run this code without any errors on a Mac, but I need it to work on Windows.
How can I resolve this error?
Simple really. A colon character is not a valid character in a filename on Windows. It's not allowed.
Reference: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx
The colon character is in the list of "reserved characters", along with several others. (NOTE: One use of the colon character is as a separator for an Alternate Data Stream on NTFS. Ref: http://blogs.technet.com/b/askcore/archive/2013/03/24/alternate-data-streams-in-ntfs.aspx
Followup
The question has been significantly edited since my previous answer was provided. Some notes:
I'm not very familiar with running MySQL on Windows OS. Most of my work with MySQL server is on Linux.
The SELECT ... INTO OUTFILE statement will cause the MySQL server to attempt to write a file on the server host.
The MySQL user (the user logged in to MySQL) must have the FILE privilege in order to use the SELECT ... INTO OUTFILE statement.
Also, the OS account that is running MySQL server must have OS permissions to write a file to the specified directory, and the file to be written must not already exist. Also, the filename must conform to the naming rules for filenames on OS filesystem.
Ref: https://dev.mysql.com/doc/refman/5.5/en/select-into.html
For debugging this type of issue, I strongly recommend you echo out the actual SQL text that is going to be sent to the MySQL server. And then take that SQL text and run it from a different client, like the mysql command line client.
For debugging a privileges issues, you can use a much simpler statement. Test writing a file to a directory that is known to exist, that is known the mysql server has permissions to write files to, and with a filename that does not exist and that conforms to the rules for the OS and filesystem.
For example, on a normal Linux box, we could test with something like this:
mysql> SELECT 'bar' AS foo INTO OUTFILE '/tmp/mysql_foo.csv'
Before we run that, we can easily verify that the /tmp directory exists, that it is writable by the OS account that is running the mysql server, and that the filename conforms to the rules for the filesystem, and that the filename doesn't exist, e.g.
$ su - mysql
$ ls -l /tmp/mysql_foo.csv
$ echo "foo" >/tmp/mysql_foo.csv
$ cat /tmp/mysql_foo.csv
$ rm /tmp/mysql_foo.csv
$ ls -l /tmp/mysql_foo.csv
Once we get over that hurdle, we can move on to testing writing a file to a different directory, a file with a more more complex filename. Once we get that plumbing working, we can work on getting actual data, into a usable csv format.
The original question seems to indicate that the MySQL server is running on Windows OS, and it seems to indicate that the filename attempting to be written contains semicolon characters. Windows does not allow semicolon as part a filename.
It was simply permission error.

SOURCE error 2?

When i try to source an sql file i get the error:
mysql> source C:/Users/tom/Documents/insert.sql
ERROR:
Failed to open file 'C:/Users/tom/Documents/insert.sql', error: 2
I have checked the file path, which looks fine to me. I have also tried \. C:/Users/etc
I am trying to source the sql file which holds insert statements for particular tables. All the statements in the file work when entered manually. What else could i be doing wrong?
Have tried using both backslash and forward slash when using this command
Probably a problem of access right on the file (the file is being accessed by the mysqld server process, not yourself). Try placing the file into the data folder of MySQL, then import it from this location. The location of data folder depends on your distribution and on your own configuration.
Alternatively, feed the SQL script directly to your mysql client's stdin:
mysql [all relevant options] your_database < C:\path\to\your\script.sql
I am using Ubuntu 14.04 version.
I too faced below error 2.
mysql> SOURCE home/loc/Downloads/AllTables.sql;
Failed to open file 'home/loc/Downloads/AllTables.sql', error: 2
Solution :
mysql> SOURCE /home/loc/Downloads/AllTables.sql;
Just added a '/' in front of home
Hope this helps some one.
Have you checked if the file exits? I have had this problem before.
This:
this:
and this works:

Can't create/write to file (Errcode: 22)

Quite new with SQL I'm looking to export some data from a MySQL database into a csv file. I'm working locally (localhost).
Here is my SQL statement:
SELECT DISTINCT *
INTO
OUTFILE 'C:\Users\Martin\Downloads\result.csv'
FROM provider, location, provider_has_location
WHERE
provider.idprovider = provider_has_location.provider_idprovider AND
location.idLocation = provider_has_location.location_idLocation
LIMIT 20
MySQL return the following error:
Can't create/write to file 'C:UsersMartinDownloads esult.csv' (Errcode: 22)
Thanks for your help.
It looks like the back-slashes may be affecting the command. Try using '\\' instead of '\'.
SELECT * FROM TableName WHERE condition INTO OUTFILE 'E:/test/filename.csv';
You are having issue with the back slash. Using forward slash resolved the issue in windows machine.
But in Ubuntu its not working.
Then I tried with the below mentioned Query and it works:
SELECT * FROM TableName WHERE condition INTO OUTFILE '//tmp/test/filename.csv';
Here the double slash when starting the path is necessary.

MySQL cannot find data file for a load operation

I would like to load a data file into MySQL using the following command:
LOAD DATA LOCAL INFILE '/Users/David/Desktop/popularity20110511/test_data' INTO TABLE test_table
The above command gives me the following error:
#7890 - Can't find file '/Users/David/Desktop/popularity20110511/test_data'.
I've also tried:
LOAD DATA INFILE '/Users/David/Desktop/popularity20110511/test_data' INTO TABLE test_table
I also gives me an error:
#13 - Can't get stat of '/Users/David/Desktop/popularity20110511/test_data' (Errcode: 13)
I've repeatedly checked the file path and name and I've also made sure the file privilege is set to Read & Write for everyone.
I am using a Mac and phpMyAdmin.
Any suggestions on what the problem may be?
I had the same problem using MacOs and tried to change permissions, etc, but I realized you have to use the same directory structure you have using in the Terminal Application. Example: if you have (localhost/myproject/myfile.csv) try using
(Applications/XAMPP/htdocs/myproject/myfile.csv).
LOAD DATA LOCAL INFILE '/Applications/XAMPP/htdocs/myproject/myfile.csv'
INTO TABLE `mytable`
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\r';
I'm not too sure what the problem is but I made it work by moving the file to /tmp/test_data and used LOAD DATA INFILE...
I have had the same issue, trying to import an SQL file that uses LOAD DATA LOCAL INFILE... to import a CSV file in phpMyAdmin and got the same error message:
#7890 - Can't find file 'myfile.csv'
The solution I found was to put the file in the same folder as phpMyAdmin.
I was having the same problem
'C:/Program Files/DatabaseTableHolders/Menu.csv'
7890 - Can't find file '
The first thing I did was move the files to the "Program File" directory
It still wouldn't work
Then I changed the path address from
'C:/Program Files/DatabaseTableHolders/Menu.csv'
to
'C:\Program Files\DatabaseTableHolders\Menu.csv'
THIS WORKS!!!
For me its something to do with the path structure.
By the way I'm using Eclipse and phpMyAdmin on WAMP (windows operating system). I hope this helps.
Yes, I meet the same error.
My situation:
XAMPP + MAC OS 10.9
load data local infile '/Applications/XAMPP/xamppfiles/htdocs/jsonSQL.txt' into table `ttlegs` fields terminated by ',' lines terminated by '\n'
and this works when I put jsonSQL.txt to htdocs.
It is best if you put that text file in 'xammp /phpMyAdmin ' directory ( I assume you work on xammp) That's it. then LOAD DATA LOCAL INFILE will work. Happy Coding