简体   繁体   中英

MySQL Grant User Privileges Error

I have a question regarding how to grant privileges MySQL user accounts. I have a MySQL user account, and in my example code I would like to grant privileges to the whole database. When I run the code, I get the following error:

Access denied for user 'user'@'10.%' to database 'testDB'

Here is my source code:

<?php

  $connection = mysql_connect("sql.example.com", "user", "password");

  mysql_select_db("test");

  $query = mysql_query("GRANT ALL PRIVILEGES ON test.* To 'user'@'localhost' 
                         IDENTIFIED BY 'password'");

if (!$query) {
  die(mysql_error());
}

echo "Success!";

?>

Why am I getting that error? Could someone please help my with this? I would really appreciate it!

Looks like you have no access to your mysql server with that credentials:

"sql.example.com", "user", "password"

Try first to create a user with the password and after that connect to mysql.

Enter to the mysql server with root user that you have, that way:

shell> mysql --user=root mysql

After that create the user that you going to connect with:

shell> CREATE USER 'monty'@'localhost' IDENTIFIED BY 'some_pass';

And then give him permissions:

shell> GRANT ALL PRIVILEGES ON *.* TO 'monty'@'localhost' WITH GRANT OPTION;

Don`t forget to set you host for connection, in the example it is localhost, but in your case it could be something else.

I guess it's the same problem with php like a native query --> you need to execute "flush privileges" after the grant statement - otherwise the user table doesn't get updated.

http://dev.mysql.com/doc/refman/5.5/en/adding-users.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM