0

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.

Mohamad Khani
  • 332
  • 4
  • 13

2 Answers2

1

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.

Wrikken
  • 69,272
  • 8
  • 97
  • 136
  • I remove mysql anonymous user but Now php cannot connect to mysql with this error : "Access denied for user 'mohammad'@'localhost' (using password: NO)" mohammad is my Ubuntu user instead of root that as argument of mysql_connect function. – Mohamad Khani Oct 21 '13 at 20:51
  • So, you used the anonymous user before, use your admin account to create that account (`GRANT ... whatever priviliges you like.. ON ... TO mohammad@localhost` (possibly with an `IDENTIFIED BY 'a password'`, which you can store in your `~/.my.cnf` so you don't have to type it all the time). – Wrikken Oct 21 '13 at 20:53
  • If that user has access, yes. If no account you have has access anymore, in default Ubuntu you can probably use `sudo mysql --defaults-file=/etc/mysql/debian.cnf` – Wrikken Oct 21 '13 at 20:58
  • Why I cannot change my mysql user in php code? mysql_connect('localhost' , 'newuser' , 'pass') I think mysql_connect in php has a problem – Mohamad Khani Oct 21 '13 at 21:01
  • You have to create that users account first of course (as soon as you're connected with a currently already configured user, and you have the `GRANT` privilege, you can create users and `GRANT`'s all you like). – Wrikken Oct 21 '13 at 21:03
  • For each user in mysql_connect() function mysql connect as my Ubuntu User. I cannot solve this. – Mohamad Khani Oct 21 '13 at 21:12
  • Wait, so _all_ `mysql_connect("localhost","randomname","somepass");`'s connect like `mohammad@localhost`? Could you [pastebin](http://pastebin.com/) the ouput of `SELECT user,host FROM mysql.user` somewhere? – Wrikken Oct 21 '13 at 21:32
  • Yes this is connect like mohammad@localhost. – Mohamad Khani Oct 21 '13 at 21:36
  • That's mean mysql_connect() always try to connect mysql with my ubuntu user and without pass. if I create this user without password connection works. – Mohamad Khani Oct 21 '13 at 21:42
  • 1
    I Solve this problem. in php.ini file sql.safe _mode was on. {sql.safe_mode = Off} – Mohamad Khani Oct 21 '13 at 21:50
  • Ooh, nice catch! Never encountered anyone actually having that enabled. So, both an anonymous user AND a user-rewrite problem here. – Wrikken Oct 21 '13 at 21:53
0

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

Mohamad Khani
  • 332
  • 4
  • 13