但是mysql可以正常工作

但是mysql可以正常工作

本文介绍了使用mysqli或PDO时无法通过PHP脚本连接到MySQL,但是mysql可以正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临着一个奇怪的情况.当我尝试使用"mysql"连接连接到MySql数据库时,它可以正常工作.

I am facing a weird situation. When I am trying to connect to MySql database using a "mysql" connection, it works.

mysql connection string -> mysql_connect($HOST, $USER, $PASSWORD, $DB);

但是当我使用"mysqli"或"PDO"时,连接失败立即失败

But the connection fails immediately fails when I use either "mysqli" or "PDO"

mysqli connection string -> mysqli_connect($HOST, $USER, $PASSWORD, $DB);
PDO Connection string -> new PDO("mysql:host=$HOST;dbname=$DB", $USER, $PASSWORD);

它引发的特定错误是

SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'localhost' (10061

你们能帮我吗?谢谢.

推荐答案

我经常遇到这个问题.最常见的解释是MySQL Server被配置为在一个路径上使用套接字文件,但是php.ini在mysqli或pdo_mysql上的部分正在寻找在另一路径上的套接字文件.

I've run into this from time to time. The explanation is most often that MySQL Server is configured to use a socket file at one path, but php.ini's section on mysqli or pdo_mysql is looking for the socket file at another path.

即使我从MacPorts安装了PHP和MySQL,这也发生在我身上.您可能以为他们已经同意了这两个端口的配置.

This happens to me even though I install both PHP and MySQL from MacPorts. You'd think they'd have made the configurations for these two ports agree.

要么编辑您的php.ini以设置套接字文件的正确位置,要么在启动与mysqli或pdo_mysql的连接时指定套接字.

Either edit your php.ini to set the correct location of the socket file, or else specify the socket when you initiate a connection with mysqli or pdo_mysql.

pdo = new PDO("mysql:dbname=test;unix_socket=/opt/local/var/run/mysql5/mysqld.sock",
  "username", "password")

$mysqli = new mysqli("localhost", "username", "password", "test",
  ini_get("mysqli.default_port"), "/opt/local/var/run/mysql5/mysqld.sock")

另请参阅我写的文章 Error2003-CantConnectToMySQLServer

See also the article I wrote Error2003-CantConnectToMySQLServer.

这篇关于使用mysqli或PDO时无法通过PHP脚本连接到MySQL,但是mysql可以正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 08:27