PHP:mysql_connect() 返回 true 但 mysql_select_db() 返回 false _ 在 Ubuntu 更新后

标签 php mysql linux mysql-select-db

在 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/

相关文章:

php - template.php 中的 hook_nodeapi

Java REST Api(运行在tomcat上)-在客户端和服务器之间实现实时同步的最简单方法

c - 使用 sendfile 而不是 sendto 的零复制 udp 套接字

linux - 通过 wget 从 ftp 服务器下载文件失败

java - MPJ - 使用 mpjboot 时出现 Express 错误

php - 向MySQL表插入大量数据只插入1行?

php - 寻找一种方法来创建链接到 2 个独立数据库的动态 QR 码

MySQL错误使用BTREE

PHP explode 并赋值给一个多维数组

c++ - mysql_query 无法正确保存数据