Using select info outfile - mysql

I am trying to send the results of a query to a file using
SELECT * from TABLENAME INTO OUTFILE 'spool.txt' ;
but I can't find the resulting file
The command executes, but I don't know where to find the file.
I did some research, tried to use a full path but there are permission problems.
I did
SHOW VARIABLES LIKE "secure_file_priv";
to see the directory where I thought the output would go, it said
C:\ProgramData\MySQL\MySQL Server 8.0\Uploads\
But when I look in that directory the file is not there. What am I missing? Thanks!

Related

Can't find 'secure_file_priv' variable in my.cnf

I'm using mysql, and I want to enable saving queries to text files in any location. Currently, when I run the mysql command:
show variables like 'secure_file_priv';
I receive the value for 'secure_file_priv':
/var/lib/mysql-files/
I am able to save my queries to files in this location but nowhere else of course. (I'm also having trouble accessing this folder, even when I try to access it as root)
I found my 'my.cnf' file in '/etc/my.cnf', but it doesn't contain the variable 'secure_file_priv'!
I added it in myself as:
secure_file_priv=""
But this didn't do anything.
I also tried changing it through the mysql terminal, but it is a readonly variable, so I wasn't able to.
Where is this variable located and how can I change it to allow me to write the results of queries to files anywhere?

File not found in MySQL data directory

I am trying to load an XML file into a table on my localhost MySQL server. Per MySQL 5.6 refman page I have my file loaded in the Data directory however I keep getting the error that my file is not found. I executed the SHOW VARIABLES WHERE Variable_Name LIKE "%dir" command and found where my data dictionary is located (C:\ProgramData\MySQL\MySQL Server 5.6\Data) and it's where I put my xml file but still get the same error:
mysql> USE test
Database changed
mysql> LOAD XML LOCAL INFILE 'testXML.xml'
-> INTO TABLE testxml
-> ROWS IDENTIFIED BY '<Data>';
ERROR 2 (HY000): File 'testXML.xml' not found (Errcode: 2 - No such file
or directory)
Any suggestions/direction would be appreciated. Thanks.
I remember having such a problem once and the problem was the spaces in the path name.
If really the file is at the right place and a file not found occur, I would highly suspect that the folder name 'MySQL Server 5.6' is the problem. You can validate this easily, put the file in another folder instead of the data dir and try adding/removing spaces to the folder name and see if it works.
An other possibility is that your folder is located in an Admin directory and that you don't have right to access.

why does SELECT INTO OUTFILE give file exists error even though file does not exist?

The file definitely does not exist, but I am getting an error anyway.
I do:
$ rm /tmp/records_materialized_view.txt;
$ mysql ...
> SELECT * FROM records_materialized_view INTO OUTFILE '/tmp/records_materialized_view.txt';
ERROR 1086 (HY000): File '/tmp/records_materialized_view.txt' already exists
SELECT INTO OUTFILE writes results to a server file.
Are you checking for the file existence on server?
If you want to select into a local file on your client machine, just redirect mysql output:
mysql mydb < script.sql > /tmp/records_materialized_view.txt
Came across this answer when I had a similar issue.
I realized that SELECT INTO OUTFILE does not overwrite files, you have to clean them up yourself. So you will get this error the next time you write it. Not sure what is meant by in the above answer, that the files are written to a server file.
Also, SELECT INTO OUTFILE by default writes files relative to your db data directory. The default location i.e.
/var/lib/mysql/
So if you check your db data directory, you should find your files there. Use an absolute path to control exactly where you want the file to end up. This is exactly what I was after, hopefully it helps somebody. Cheers.

Sql to text file

I have a SQL database and want to write a script, which saves the table "European_option_info" as a txt (or excel) file to a local folder.
I use the following code:
use database;
select * into outfile 'C:\[...]\Dropbox\Employees.txt' FIELDS TERMINATED BY ';' from European_option_info;
This gives me the following error:
"Error Code: 1. Can't create/write to file 'C:[]\Employees.txt' (Errcode: 22)"
Regards,
Daniel
It looks like your file name is incorrect. Use a full path for the path to Employees.txt.
For example
USE database;
SELECT * INTO OUTFILE 'C:\Users\Username\Dropbox\Employees.txt'
FIELDS TERMINATED BY ';'
FROM European_option_info;
Or whatever the full path to your Dropbox folder is.
One problem may be of the file path that your are specifying, you need to to specify entire path and also that file must not be open when executing this query.
The other problem may be of permission to create/change something in C:\\( Here I am assuming that you are using Windows OS and if not then just try this solution for other OS as well) as it may require administrative rights as the application that is executing your query may not be in run as administrator mode of windows. (Assuming C: drive contains your windows installation).
So try changing the directory to some other directory of your PC.

What is the 'Query' MySQL data file used for?

I am having some real difficulties finding out exactly what a certain file in the MySQL data directory is used for. (Using Google with its file name is pointless!)
Basically, I need to create some space on the drive that hosts all MySQL data and have noticed a file almost 16GB in size!!
I cant see any reference to a Query file in my config file nor can I match its size up to that of any log files, etc (in case its a log file missing the .log extension). I'm totally stumped!
I would like to know what this file is and how to reduce its size if at all possible?
Thanks in advance for your assistance!
That could be the general query log (I said "could" because the name can be configured by yourself). Look in your my.ini for an entry
log=/path/to/query
Or start the MySQL Administrator, goto "Startup Variables->Log Files" and look for "Query Logfile"
That file is completely unnessasary for your server to run (if you confirmed that the entry log=... exists in your config.
It is just good for debugging.
Try stopping your mysql server, delete it and restart your server again. The file will be recreated.
I also noticed that the slow-query-log ("diamond-slow-log") is large, too.
That file only logs queries that take longer than x seconds (2 by default). That file can be deleted or deactivated, too. But I would keep it since it contains queries that could easily be optimized with an extra index.
Update
there is another way, to confirm that this is the general query log.
Download a windows port of the tail unix command. E.g. this one http://tailforwin32.sourceforge.net/
I often use this on my dev machine to see what is goning on.
Open a shell (cmd.exe) and navigate the folder where that file exists.
Then type
tail -f query
That will print the last few lines of the file and if the file changes every new line.
So if you do a SELECT * FROM table you should see the query in the console output.