在 php 中将我的 Ubuntu 更新到 13.10 后,mysql 可以通过任何用户连接,但无法选择具有正确用户的数据库并通过。 似乎发送到 mysql 的用户是空的而不是 root。
$myconn = mysql_connect('localhost' , 'root' , 'pass');
$seldb = mysql_select_db('mydb' , $myconn);
$er = mysql_error();
运行后
$mycon 是:"resource id='2' type='mysql link'"
和
mysql_select_db() 返回 false
;
和
$er 是:“用户 ''@'localhost' 对数据库 'mydb' 的访问被拒绝”
请帮帮我。 抱歉我的英语不好。
最佳答案
你看MySQL认为你是''
,而不是root
,也就是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”状态:
The server uses sorting rules that order rows with the most-specific Host values first
这意味着如果你有一个匿名的 ''@localhost
用户,但是一个不太具体的(或只是稍后排序的)root@'%'
(或类似的东西) 定义从不匹配,因为“匿名”规则匹配。查看最后一个链接以获得更详尽的解释。
这意味着你要么:
- 明确授予对
root@localhost
的访问权限。 DROP USER ''@'localhost';
(匿名用户)
我强烈推荐第二个:不需要匿名用户。
关于PHP:mysql_connect() 返回 true 但 mysql_select_db() 返回 false _ 在 Ubuntu 更新后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19501280/