dongzh1988 2019-03-22 17:25
浏览 331

Mysql CLI在localhost上连接正常,但mysqli_connect将localhost解析为机器名

I have a seemingly straightforward call using mysqli_connect() that is failing due to host substitution of the name 'localhost'.

Example:

$username = 'foo';
$host = 'localhost';
$passwd = 'secret';

$link = mysqli_connect($host,$username, $passwd)...

Calling mysqli_connect with either 'localhost' or '127.0.0.1' as the host returns something like this:

(HY000/1045): Access denied for user 'foo'@'servername.mydomain.com'

I can connect using the same account with the locally-installed MySQL client just fine, so the user's permissions are valid:

SHOW GRANTS FOR 'foo'@'localhost';
GRANT USAGE ON *.* TO 'foo'@'localhost'

Evidently mysqli_connect is trying to connect using the DNS name of the local machine instead of localhost.

I know I could opt to issue a grant for the user with the host explicitly defined as the machine name, or allow from all hosts, but neither of those options is acceptable in this instance. I need it to get it to work using localhost.

Related: /etc/hosts defines localhost as follows:

127.0.0.1       localhost 
  • 写回答

1条回答 默认 最新

  • doulan6150 2019-03-23 03:11
    关注

    Passing the path to the socket as the last argument to mysqli_connect() was probably working all along. A subsequent call to a remote database was also failing with an identical error, and I didn't notice until now that the error was occurring at a later line number.

    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)