Query output to a file gives access denied error - mysql

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?

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.

If it's your system (you're admin), and you know how to secure it, this is how you enable those permissions.
USE mysql;
UPDATE user SET File_priv = 'Y' WHERE User = 'db_user';
FLUSH PRIVILEGES;

Related

Exporting data into a table not working with Access Denied error

I have a table in a database myDatabase in Amazon RDS. Let it be myTable.
use myDatabase;
SELECT * from myTable INTO OUTFILE 'myFile.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"'LINES
TERMINATED BY '\n';
I get this error
Error Code: 1045. Access denied for user '<<UserName>>'#'%' (using password: YES)
I tried running this command
GRANT USAGE ON *.* TO '<<UserName>>'#'%' IDENTIFIED BY '<<password>>';
flush privileges;
0 row(s) affected, 1 warning(s): 1287 Using GRANT statement to modify existing user's
properties other than privileges is deprecated and will be removed in future release. Use
ALTER USER statement for this operation.
I get this warning.
And
I am not able to export the table into a .CSV file at all.
Any idea on how to solve it ? Any thoughts on the steps which might have gone wrong ?
Make sure your user has the FILE privilege.
FILE
Affects the following operations and server behaviors:
Enables reading and writing files on the server host using the LOAD DATA and SELECT ... INTO OUTFILE statements and the LOAD_FILE()
function. A user who has the FILE privilege can read any file on the
server host that is either world-readable or readable by the MySQL
server. (This implies the user can read any file in any database
directory, because the server can access any of those files.)
Enables creating new files in any directory where the MySQL server has write access. This includes the server's data directory containing
the files that implement the privilege tables.
manual entry

Download MySQL database from ISP server to localhost

Non-coder here, please advise as newbie. I have a MySQL DB on my ISP's server that provides data for a web site. I want to set up a db on my localhost that I can use for local development. The DBs don't need to be linked, nor do they need to remain synchronized. I just need to start with a current copy of the ISP version.
I have used PHPMyAdmin on ISP to download what I think is a dump file. the file, [mydbname].sql shows the schema and all the data. I just can't figure out how to import it in MySQL Workbench on my local PC.
Using the MySQL Workbench Data Import tab, if I execute "Import from Dump Project Folder" where the .sql file is located, I get "There were no dump files in the selected folder."
If I select "Import from Self-Contained File" and select my ***.sql file, I get "ERROR 1142 (42000) at line 31: CREATE command denied to user 'root'#'localhost' for table 'account' Operation failed with exitcode 1"
I've attempted to give 'root'#'localhost' all privileges.
I'm guessing there's an issue with privileges. If there's another way to do this, I'd be thrilled to know it. Many thanks!
If what you have is a sql dump file then try this on the CLI:
mysql -h hostname -u user --password=password databasename < filename
Assuming of course that has the right privileges.
Non-coder here, please advise as newbie.
In that case, download heidisql : https://www.heidisql.com/
It also makes copying tables and database from server to server very easy.
Apart from that: there is most likely something wrong with your privileges. But you might have made that worse by trying to give privileges to root. The root user can already do everything. So maybe also have a look at this post : How can I restore the MySQL root user’s full privileges?

Table 'DBName.user' doesn't exist when trying to change root user name

I have just installed MySQL on Debian 7.0.0.
I successfully imported by database from another system using
mysql -u root -p DBName <mysql27May13.dump
I then successfully logged onto MySQL using
mysql -u root -p
I then successfully selected the database using
use DBName;
Also
show tables;
showed the tables I imported. However, when I try to change the root user name using
update user set user='SomeNewName' where user='root';
I get the error message
ERROR 1146 (42S02): Table 'DBName.user' doesn't exist
If you want to change a MySQL username you should use RENAME USER
RENAME USER root#localhost TO other_user#localhost
The table you want to update -- user, in this case -- is not within your database (which I assume is called DBName, here). The database you need is, in fact, simply called mysql.
You can work around this in a few ways:
Run your update on mysql.user instead of user.
use mysql before you do the update.
Use the supplied RENAME keyword to do the job instead, as #ExplosionPills suggests.
I'd suggest always taking approach #3 for user management unless you know for sure you're trying to do something the built-in commands can't handle. Chances are, you're not -- and if you are, you'll know it.

How to create a MySQL user without password - needed for remote login - to be used in bash insert script?

My requirement is to create a user for remote login but without a password. At my remote space I use a bash script to do inserts, which is something like:
for i in {1..5000}; do
mysql -h 11.40.3.169 -uUser -pPass -DDatabaseName <<<
"insert into DatabaseTableName values('$i','dummy_$i');" &
echo -n "$i "
sleep 1
date
done
The problem is that each insert is taking almost 4 seconds, and I can not pinpoint the problem to anything but authentication at every insert. So, if I could create a user in MySQL with minimal authentication involved...Something like:
# I'm trying to remove this password
GRANT ALL PRIVILEGES TO 'user'#'%' IDENTIFIED BY 'password';
...Anything you can suggest.
Just remove the IDENTIFIED BY part:
GRANT ALL PRIVILEGES ON *.* TO 'user'#'%'
Note that remote login from anywhere without a password is a very insecure thing. You better limit the allowed IP range for this user:
GRANT ALL PRIVILEGES ON *.* TO 'user'#'allowed_remote_machine'
You can do this by creating a user with a password and then placing a .my.cnf file in the home directory of the account which runs the bash script containing the following:
[mysql]
user=user
password=pass
[mysqladmin]
user=user
password=pass
This might be better than creating a user with no password.
I think your problem lies in the fact that you are starting the mysql client for each insert. You should be doing your inserts from a php, java, etc program - not from a shell script.
The startup time of the client (and connection to the host) is killing you. I routinely do 1000s of inserts per minute from a php or java program to a MySQL database with millions of records on a small (CPU/memory) machine.
It's not so good idea to have a user without password and all privileges. I suggest you to create a user without password but just with some privileges (insert to specific table or specific database).
First off, using a client cnf file on the remote machine running the script wont speed this up. MySQL client is still sending logon information and logging in for each insert, it's just reading.a file for uid/pw instead of using cmd line arguments. AFAIK The network and authentication overhead are identical. Even the network packet contents will be the same.
You should still use a cnf file..
The way to.improve performance is to do multi-line linserts:
MySQL --defaults-file=/some/uid/pw/etc/.client.cnf -e \
"Insert into
tbl_name
('fld1','fld2')
values
('r1-fld1','r1-fld2'),
('r2-fld2','r2-fld2'),
...and so on (up to max_allowed_packet_size)
('r500-fld2','r500-fld2');"
Or READ DATA INFILE on server side after shipping over the data file

Access denied error on select into outfile using Zend

I'm trying to make a dump of a MySQL table on the server and I'm trying to do this in Zend. I have a model/mapper/dbtable structure for all my connections to my tables and I'm adding the following code to the mappers:
public function dumpTable()
{
$db = $this->getDbTable()->getAdapter();
$name = $this->getDbTable()->info('name');
$backupFile = APPLICATION_PATH .
'/backup/' . date('U') .
'_' . $name . '.sql';
$query = "SELECT * INTO OUTFILE '$backupFile' FROM $name";
$db->query( $query );
}
This should work peachy, I thought, but
Message: Mysqli prepare error: Access denied for user 'someUser'#'localhost' (using password: YES)
is what this results in.
I checked the user rights for someUser and he has all the rights to the database and table in question. I've been looking around here and on the net in general and usually turning on "all" the rights for the user seems to be the solution, but not in my case (unless I'm overlooking something right now with my tired eyes + I don't want to turn on "all" on my production server).
What am I doing wrong here? Or, does anybody know a more elegant way to get this done in Zend?
Although I gave "all privileges" to the user in question, I did this on a per database basis instead of globaly. The privilige needed to use outfile however is FILE, an can only be set as a GLOBAL permission
If you have an issue with permissions you need to set the user mysql runs under to have the necessary permissions needed to access that folder. For example you have /tmp/filedumps, with a owner of www-data and group of www-data, you need to add the mysql user account to the group on debian/ubuntu you can do usermod -a -G www-data mysql.
This is how I solved my issues on *nix boxes.
Does a normal SELECT work? If it doesn't either, I would say this is a simple case of a wrong password. If a normal SELECT works, make sure you granted the OUTFILE right to the exact user
'someUser'#'localhost'
and not for example
'someUser'#'%'
if I remember correctly, those two accounts would be treated differently and have separate rights settings.
>$ echo "select count(predicate),subject from TableA group by subject" | mysql -u yourusername -p yourdatabasename > ~/XYZ/outputfile.txt
Snarfed from this post that has a workaround to the FILE permissions not being granted:
Query output to a file gives access denied error