Access denied error on select into outfile using Zend - mysql

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

Related

Access denied for user `staging`#`%`

I am importing a sql file to database using command prompt.But getting an error of Access denied. Details:
user: staging
host : %
It works with the following details:
user : staginguser
host: localhost
Import command:
mysql -u staging -p dbname < filename.sql
Can anyone tell me how to import a file for user with host % ?
Thanks in advance
First, and only for clarity purposes, you may wish to put the dbname just after the MySQL command. Second, you may wish to check if the contents of the file has references to the source database name (something like .) and the old db name differs from the new. Third, check that user staging has access to the db and if yes, check that it has the required permissions to perform whatever is contained in the file.
After you have done all the above, post an update to your question with all the details.
By the way, if you are using % as the host, it means that the user must have access from any host. Does he/she?

Mac Mysql change password , No user table to run update query

I am able to login with root - "No password", But i can't change password. I checked many links , everybody saying to run update query on user table , but i even can't find user table in phpmyadmin.
Error I am getting is :
$ /usr/local/mysql/bin/mysqladmin -u root -p password
Enter password:
New password:
Confirm new password:
/usr/local/mysql/bin/mysqladmin: Can't turn off logging; error: 'Access denied; you need (at least one of) the SUPER privilege(s) for this operation'
Any help will be highly useful
PHP My ADMIN (Screenshot): http://awesomescreenshot.com/021cxa0fb
Error: http://i47.tinypic.com/1j1b0m.png
Additional screenshot: http://i48.tinypic.com/w9vbtl.png
Additional screenshot 2: http://i48.tinypic.com/4rymps.png
it seems to me, that you use phpmyadmin to look for the table. if so, you won't see all tables. you have to log in to mysql as root with the "mysql" command in the terminal:
$ mysql -u root
then you can run the "update user..."-command (you don't have to see the table containing the user informations)
another idea: when you use the -p in the command line, you are not allowed to write the password next to it (it will ask you later on). if you write something after -p it will think that this is the database name...
From the additional information you have posted in comments it appears that your MySQL root user no longer has root privileges on your system. After performing a quick search looking for mysql reset root privileges I found this blog posting that appears to give detailed instructions on how to restore root privileges to your root account.
BEWARE, I have not tried these steps and they are from 2009 and it's possible that MySQL may have changed internally from when these instructions were created. However, that being said, the comments on the posting are positive (of course, the comments could be fake).
My recommendation is to backup the directory (or directories) containing all of your MySQL data and then trying these steps EXACTLY as they are laid out.
Looking more closely at these instructions they appear valid to me. I noticed that they involve taking down the MySQL daemon, restarting MySQL with an option that turns off all table security and then executing updates to add rights back to the root user. I now recommend giving these steps a try.
To set the root's password:
/usr/bin/mysqladmin -root password 'new-password'
try to know if mysql daemon is up:
/usr/bin/mysqladmin -u root -p ping
if not alive try :
/etc/init.d/mysql start
& verify that mcrypt package is installed.
I solved the Problem , Thanks for all.I reinstalled the mysql server as per following guide , the problem was I updated password directly to the mysql.user table with update query , its wrong , since the password should be stored as encrypted in mysql.user table , if we updating it directly through query then it will be a string.
Excellant guide : http://blog.mclaughlinsoftware.com/2011/02/10/mac-os-x-mysql-install/
Thanks again for everybody :)

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

How can I check the mysql connection for a user/password using batch/shell script

How can I check the mysql connection for a user/password using batch/shell script?
I've user name and password, and I need to authenticate whether they are correct or not, but how?
I tried this way:
I've created a batch file "run.bat" with "mysql -u User--password=UserPassword < commands.sql"
"commands.sql" file contains "\q"
When I run the file "run.bat" the output is nothing when User/Password are correct and "ERROR 1045 (28000): Access denied for user ... " when User/Password are incorrect. Now can we capture this output, and decide whether the connection is successful or not, if yes how?
Regards
I found a solution as below:
#echo OFF
echo \q | mysql -u User --password=UserPassword 2>nul
if "%ERRORLEVEL%" == "0" (
echo CONNECTION SUCCESSFUL
) else (
echo CONNECTION FAILED
)
You can check the return status of mysql. It is stored in the ERRORLEVEL enviroment variable:
mysql -u User--password=UserPassword < commands.sql
if "%ERRORLEVEL%" EQU "0" (
echo OK
) else (
echo FAIL
)
If you are lucky, mysql.exe even returns a specific status for "logon failed" that you can react on. Most applications return 0 on success and something != 0 on failure. Use echo %ERRORLEVEL% right after a command to find out the current value.
A more advanced approach would be to capture and evaluate the STDERR stream of the application. This, however, would be material for a different question.
you could use a ".my.cnf" file
I do this, although id strongly recommend against using your mysql root login
[root#daaorc900c ~]# cat ./.my.cnf
[client]
user=monitoruser
password=whatismonitor
[root#daaorc900c ~]#
Looks like you might be one windows so here is the doc for the "options files" in widnows
http://dev.mysql.com/doc/refman/5.1/en/option-files.html

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?
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;