Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query output to a file gives access denied error

I am trying to capture the output of a SQL query in MySQL, to a text file using the following query.

select count(predicate),subject from TableA group by subject into outfile '~/XYZ/output.txt';

I get the following error.

ERROR 1045 (28000): Access denied for user 'username'@'%' (using password: YES)

Any idea, where am I going wrong? Is it some permission related issue?

like image 325
Arnkrishn Avatar asked Mar 21 '09 02:03

Arnkrishn


People also ask

How do I fix access denied error?

Right-click the file or folder, and then click Properties. Click the Security tab. Under Group or user names, click your name to see the permissions you have. Click Edit, click your name, select the check boxes for the permissions that you must have, and then click OK.

How do I fix access denied in MySQL?

You will get this error when the user user_name does not have the right to access your MySQL database. To resolve the error, you must create a user with the following command: mysql> GRANT ALL ON *. * to user_name@localhost IDENTIFIED BY 'password';

Why do I get an access denied error message?

The “Access Denied” error appears when your browser uses different proxy settings or VPN instead of what's really set on your Windows 10 PC. Thus, when a website detects that there is something wrong with your browser cookies or your network, it blocks you and this is why you can't open it.

Why does excel Say Access Denied?

This issue occurs because Excel tries to delete the temporary copy of the original workbook on the UNC share. When a user saves an Excel workbook to a UNC share location, Excel creates a temporary copy of the workbook.


1 Answers

Outfile is it's own permission in mysql.

If you have ALL it's included.

But if you just have a safe collection such as SELECT, INSERT, UPDATE, DELETE, DROP, CREATE, but not OUTFILE, "into outfile" will not work in queries.

The reason for this is that accessing files from within MySQL, even for write purposes, has certain security risks, because if you access a file from mysql you can access any file the mysql user has access to, thereby bypassing user-based file permissions.

To get around this, you can run your query directly into the output of whatever shell/language you're using to run the sql with.

Here is a *nix example

>$ echo "select count(predicate),subject from TableA group by subject"  | mysql -u yourusername -p yourdatabasename > ~/XYZ/outputfile.txt

But do it all on one line without the "\" or use the "\" to escape the line break.

What's happening here is that you're running a query into the mysql client and it's spitting out the result, then you're directing the output to a file. So the file is never called from within mysql, it's called after mysql runs.

So use mysql to get the information and THEN dump the data to the file from your own user shell and you'll be fine.

Or find a way to get yourself the outfile mysql permission, either way.

like image 110
Fire Crow Avatar answered Sep 28 '22 04:09

Fire Crow