Question

After update my Ubuntu to 13.10 in php , mysql can connect by any user but can't select database with correct user and pass. seem the User that sent to mysql is empty Instead root.

$myconn = mysql_connect('localhost' , 'root' , 'pass');
$seldb = mysql_select_db('mydb' , $myconn);
$er = mysql_error();

After run $mycon is:"resource id='2' type='mysql link'"

and mysql_select_db() return false;

and

$er is:"Access denied for user ''@'localhost' to database 'mydb'"

Please help me. Sorry for my bad English.

Was it helpful?

Solution

You see MySQL thinks you're '', not root, which is related to this warning from the manual:

If you permit anonymous users to connect to the MySQL server, you should also grant privileges to all local users as user_name@localhost. Otherwise, the anonymous user account for localhost in the mysql.user table (created during MySQL installation) is used when named users try to log in to the MySQL server from the local machine. For details, see Section 6.2.4, “Access Cotrol, Stage 1: Connection Verification”.

“Access Cotrol, Stage 1: Connection Verification” states:

The server uses sorting rules that order rows with the most-specific Host values first

That means if you have an anonymous ''@localhost user, but a less specific (or just sorted later) root@'%' (or something like it) definition never matches because the 'anonymous' rule matches. Look through that last link for a more thorough explanation.

What this means is you either:

  • GRANT access to root@localhost explicitly.
  • DROP USER ''@'localhost'; (the anonymous user)

I would highly recommend the second one: you should have no need for an anonymous user.

OTHER TIPS

I Solve this problem myself. in php.ini file sql.safe _mode was on. that cause mysql_connect() Always connect as anonymous Ubuntu User.

in php.ini file: sql.safe_mode = Off

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top