Error: select command denied to user '<userid>'@'<ip-address>' for table '<table-name>'
Asked Answered
Y

18

75

In my website, I am using MySQL database. I am using a webservice where in I do all my database related manipulations.

Now In one of the methods of that webservice, I get the following Error.

select command denied to user '<userid>'@'<ip-address>' for table '<table-name>'

What could be wrong?

Below is the code where I get that error. I tried debugging and found that it fails at the line

MySqlDataReader result1 = command1.ExecuteReader();

Here is my code:

        String addSQL = "Select Max(`TradeID`) from `jsontest`.`tbl_Positions";
        MySqlConnection objMyCon = new MySqlConnection(strProvider);
        objMyCon.Open();
        MySqlCommand command = objMyCon.CreateCommand();

        command.CommandText = addSQL;
         MySqlDataReader result = command.ExecuteReader();
        //int j = command.ExecuteNonQuery();
         while (result.Read())
         {
             MaxTradeID = Convert.ToInt32(result[0]);
         }
        objMyCon.Close();
        for (i = 1; i <= MaxTradeID; i++)
        {
            String newSQL = "Select `Strike`,`LongShort`,`Current`,`TPLevel`,`SLLevel` from `json`.`tbl_Position` where `TradeID` = '" + i + "'";
            MySqlConnection objMyCon1 = new MySqlConnection(strProvider);
            objMyCon1.Open();
            MySqlCommand command1 = objMyCon1.CreateCommand();

            command1.CommandText = newSQL;
            MySqlDataReader result1 = command1.ExecuteReader();
           objMyCon2.Close();
Yeoman answered 22/1, 2011 at 9:13 Comment(0)
T
32

database user does not have the permission to do select query.

you can grant the permission to the user if you have root access to mysql

http://dev.mysql.com/doc/refman/5.1/en/grant.html

Your second query is on different database on different table.

 String newSQL = "Select `Strike`,`LongShort`,`Current`,`TPLevel`,`SLLevel` from `json`.`tbl_Position` where `TradeID` = '" + i + "'";

And the user you are connecting with does not have permission to access data from this database or this particular table.

Have you consider this thing?

Tricyclic answered 22/1, 2011 at 9:18 Comment(0)
D
200

I'm sure the original poster's issue has long since been resolved. However, I had this same issue, so I thought I'd explain what was causing this problem for me.

I was doing a union query with two tables -- 'foo' and 'foo_bar'. However, in my SQL statement, I had a typo: 'foo.bar'

So, instead of telling me that the 'foo.bar' table doesn't exist, the error message indicates that the command was denied -- as though I don't have permissions.

Danas answered 14/4, 2011 at 19:29 Comment(11)
In my case (Java Hibernate app) a stray @Table.catalog annotation was overriding the database name supplied in the connection string.Hawsepiece
This was also the root cause for me, a typo. From a security perspective it makes sense to give an access denied error because if a cracker is trying to figure out table names by trying various names getting a “doesn’t exist” error would indicate which table names do in fact exist at which point they could apply brute force to figure out the username and password. Given in my experience how simple usernames and passwords are (usually <10 characters of concat'd dictionary words) it probably wouldn't take that long either...Tullis
In my case, it was because some query code (like the data) had been copied from a SQL Server database, and the dbo. didn't work in the MySQL database: when dbo.tablename was changed to tablename, it executed without error.Endarch
same here yesterday select * from dbo.table worked, today only select * from table if i add dbo prefix then access deny :)Trula
This happens also on cross-database joins if you forget to specify the databasename explicitly on one of the joins.Dogfish
could you please add "selecting from wrong database schema" as well to the answer? Instead of the error saying I had no such database schema, the error kept saying "user has no select privileges".Eschatology
Same happened with me. I was using <database>.<table> instead of <Database>.<table>Extenuation
My problem was using the name of the [Dev database].table, which wasnt there in QA.Sheepherder
I was driving crazy, the problem was that i changed the name of an external DB referred by the query through a join. Wrong error message! FIXED! Thanks.Cally
How weird! I ran into the same issue, I had SELECT q.* FROM q.quote, and correcting it to SELECT q.* FROM quote AS q worked.Anytime
BAD CASE! @PavanTej exemplified it, but subtly. Thought I'd post here more blatantly that bad Case on a Case-sensitive file system (Unix, etc.) will cause this. Reading all the answers here is a bit daunting. SUMMARY: Yes, it could be permission. Change permissions. However, irritatingly, it seems that ANY BAD REFERENCE to a Schema or Table will cause this - misspellings/wrong special characters, or bad CASE on a Case Sensitive system. RANT: it's absurd that this not only useless, but very misleading, error message remains to this day (2021-Novemer). Oracle: C'Mon, Man!Piatt
T
32

database user does not have the permission to do select query.

you can grant the permission to the user if you have root access to mysql

http://dev.mysql.com/doc/refman/5.1/en/grant.html

Your second query is on different database on different table.

 String newSQL = "Select `Strike`,`LongShort`,`Current`,`TPLevel`,`SLLevel` from `json`.`tbl_Position` where `TradeID` = '" + i + "'";

And the user you are connecting with does not have permission to access data from this database or this particular table.

Have you consider this thing?

Tricyclic answered 22/1, 2011 at 9:18 Comment(0)
O
11

This problem happened to me because I had the hibernate.default_schema set to a different database than the one in the DataSource.

Being strict on my mysql user permissions, when hibernate tried to query a table it queried the one in the hibernate.default_schema database for which the user had no permissions.

Its unfortunate that mysql does not correctly specify the database in this error message, as that would've cleared things up straight away.

Outreach answered 13/10, 2015 at 21:11 Comment(1)
I had a catalog attribute set to the wrong schema name in the XML mapping of an old application - different flavour, but in the end a similar culprit. Thanks for pointing me in the right direction.Naivete
C
10

select command denied to user ''@'' for table ''

This problem is a basically generated after join condition are wrong database name in your join query. So please check the your select query in join table name after database.

Then solve it for example its correct ans ware

string g = " SELECT `emptable`.`image` , `applyleave`.`id` , `applyleave`.`empid` , `applyleave`.`empname` , `applyleave`.`dateapply` , `applyleave`.`leavename` , `applyleave`.`fromdate` , `applyleave`.`todate` , `applyleave`.`resion` , `applyleave`.`contact` , `applyleave`.`leavestatus` , `applyleave`.`username` , `applyleave`.`noday` FROM `DataEMP_ems`.`applyleave` INNER JOIN `DataEMP_ems`.`emptable` ON ( `applyleave`.`empid` = `emptable`.`empid` ) WHERE ( `applyleave`.`leavestatus` = 'panding' ) ";

The join table is imputable and applyleave on the same database but online database name is diffrent then given error on this problem.

Concise answered 31/1, 2016 at 7:24 Comment(0)
K
4

try grant privileges again.

GRANT ALL PRIVILEGES ON the_database.* TO 'the_user'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Knit answered 16/12, 2020 at 22:6 Comment(0)
T
3

You need to grant SELECT permissions to the MySQL user who is connecting to MySQL. See:

http://dev.mysql.com/doc/refman/5.0/en/privilege-system.html

http://dev.mysql.com/doc/refman/5.0/en/user-account-management.html

Township answered 22/1, 2011 at 9:17 Comment(0)
S
3

I had the exact same error message doing a database export via Sequel Pro on a mac. I was the root user so i knew it wasn't permissions. Then i tried it with mysqldump and got a different error message: Got error: 1449: The user specified as a definer ('joey'@'127.0.0.1') does not exist when using LOCK TABLES

Ahh, I had restored this database from a backup on the dev site and I hadn't created that user on this machine. "grant all on . to 'joey'@'127.0.0.1' identified by 'joeypass'; " did the trick.

hth

Sucker answered 9/12, 2014 at 15:51 Comment(0)
U
3

If you are working from a windows forms application this worked for me

"server=localhost; user id=dbuser; password=password; database=dbname; Use Procedure Bodies=false;"

Just add the "Use Procedure Bodies=false" at the end of your connection string.

Unicorn answered 15/1, 2015 at 18:34 Comment(0)
N
2

Disclaimer

  • Backup first.
  • Check your query sentence before executing.
  • Make sure you've added a WHERE (filter) clause before updating.

In case you have root access or enough privileges, you can do the following directly:

Log into your MySQL as root,

$ mysql -u root -p

Show databases;

mysql>SHOW DATABASES;

Select MySQL database, which is where all privileges info is located

mysql>USE mysql;

Show tables.

mysql>SHOW TABLES;

The table concerning privileges for your case is 'db', so let's see what columns it has:

mysql>DESC db;

In order to list the users' privileges, type the following command, for example:

mysql>SELECT user, host, db, Select_priv, Insert_priv, Update_priv, Delete_priv FROM db ORDER BY user, db;

If you can't find that user or if you see that that user has a 'N' in the Select_priv column, then you have to either INSERT or UPDATE accordingly:

INSERT:

INSERT INTO db (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv) VALUES ('localhost','DBname','UserName','Y' ,'N','N','N');

UPDATE:

UPDATE db SET Select_priv = 'Y' WHERE User = 'UserName' AND Db = 'DBname' AND Host='localhost';

Finally, type the following command:

mysql>FLUSH PRIVILEGES;

Ciao.

Neibart answered 8/11, 2017 at 16:40 Comment(0)
M
2

For me, I accidentally included my local database name inside the SQL query, hence the access denied issue came up when I deployed.

I removed the database name from the SQL query and it got fixed.

Mayor answered 10/5, 2020 at 10:4 Comment(1)
adding a point to that, when we generate the SQL from DB tools, there may be a chance that it will add database names to the query. Please consider a review on generated SQL since it will not work on remote machines since database names may vary.Mayor
V
1

The problem is most probably between a . and a _. Say in my query I put

SELECT ..... FROM LOCATION.PT

instead of

SELECT ..... FROM LOCATION_PT

So I think MySQL would think LOCATION as a database name and was giving access privilege error.

Volgograd answered 22/4, 2012 at 4:5 Comment(0)
A
1

I had the same problem. This is related to hibernate. I changed the database from dev to production in hibernate.cfg.xml but there were catalog attribute in other hbm.xml files with the old database name and it was causing the issue.

Instead of telling incorrect database name, it showed Permission denied error.

So make sure to change the database name everywhere or just remove the catalog attribute

Andres answered 15/7, 2014 at 12:7 Comment(0)
U
1

my issues got fixed after upgrading to MySQL workbench latest version 8.0.18

Ut answered 18/10, 2019 at 16:56 Comment(0)
T
1

I had this scenario:

I developed a database on my localhost, named test. Then I bought a hosting plan with cpanel where I needed to create a new database with some prefix (the prefix was my account name). So the new database name was user_test.

In my procedures, I wrote the statements like:

select * from test.t_table_name;

So when I imported the database I forgot to change the test with user_test and it gave me this message: SELECT command denied to user 'user'@'localhost' for table 't_table_name'

So what happened is that the server tried to access some other database (in my case test database) which was not created in cpanel (where only user_test existed) and the server gave me this error.

I think it will be better to tell that there isn't such a database on this server because this is the actual thing that needs to be changed.

Hope it helps someone.

Cheers.

Thyrotoxicosis answered 10/7, 2023 at 14:5 Comment(0)
B
0

I had the same problem. I was very frustrating with it. Maybe this is not answering the question, but I just want to share my error experience, and there may be others who suffered like me. Evidently it was just my low accuracy.

I had this:

SELECT t_comment.username,a.email FROM t_comment
LEFT JOIN (
    SELECT username,email FROM t_un
) a
ON t_comment.username,a.email

which is supposed to be like this:

SELECT t_comment.username,a.email FROM t_comment
LEFT JOIN (
    SELECT username,email FROM t_un
) a
ON t_comment.username=a.username

Then my problem was resolved on that day, I'd been struggled in two hours, just for this issue.

Bedspring answered 10/11, 2015 at 17:2 Comment(1)
As with Pisces22 (and myself), another problem that actually had nothing to do with the error message re' permissions. (Note: others have just as much difficulty seeing this difference, as you did, so for others: in the ON clause, ,a.email was corrected to =a.username)Endarch
S
0

I am sure this has been resolved, just want to point out I had a typo in the database name and it was still throwing this error on the table name. So you might want to check for typos in this case.

Shepard answered 2/6, 2021 at 2:49 Comment(0)
C
0

Try this to resolve error: SELECT command denied to user 'myuser'@'132.11.%'

1. CREATE USER 'myuser'@'%' IDENTIFIED BY 'Password@123';
2. GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'@'%';
3. FLUSH PRIVILEGES;
Cauthen answered 6/1 at 11:17 Comment(0)
S
-1

Similar to other answers I had miss typed the query.

I had -

SELECT t.id FROM t.table LEFT JOIN table2 AS t2 ON t.id = t2.table_id

Should have been

SELECT t.id FROM table AS t LEFT JOIN table2 AS t2 ON t.id = t2.table_id

Mysql was trying to find a database called t which the user didn't have permission for.

Superficial answered 19/6, 2014 at 12:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.