php mysql_connect(): 没有那个文件或目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12584762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:51:59  来源:igfitidea点击:

mysql_connect(): No such file or directory

phpmysqlredhatmysql-connect

提问by Maxbester

I have just installed a MySQL server (version 3.23.58) on an old RedHat7. I cannot install a more recent MySQL version because of the dependencies. I cannot update librairies on this RedHat server.

我刚刚在旧的 RedHat7 上安装了 MySQL 服务器(版本 3.23.58)。由于依赖关系,我无法安装更新的 MySQL 版本。我无法更新此 RedHat 服务器上的库。

However, I have a problem connecting to the database with PHP. First I used PDO but I realized that PDO was not compatible with MySQL 3.23...

但是,我在使用 PHP 连接到数据库时遇到问题。首先我使用 PDO 但我意识到 PDO 与 MySQL 3.23 不兼容......

So I used mysql_connect(). Now I have the following error:

所以我使用了mysql_connect(). 现在我有以下错误:

Warning: mysql_connect(): No such file or directory in /user/local/apache/htdocs/php/database.php on line 9
Error: No such file or directory

My code is:

我的代码是:

$host = 'localhost';
$user = 'root';
$password = '';
$database = 'test';
$db = mysql_connect($host, $user, $password) or die('Error : ' . mysql_error());
mysql_select_db($database);

I checked twice that the database exists and the login and password are correct.

我检查了两次数据库是否存在并且登录名和密码正确。

This is strange because the code works fine on my Windows PC with Wampp. I cannot figure out where the problem comes from.

这很奇怪,因为代码在带有 Wampp 的 Windows PC 上运行良好。我无法弄清楚问题来自哪里。

Any idea?

任何的想法?

回答by GTodorov

Yes you can not connect like that!

是的,你不能那样连接!

@PLB and @jammypeach mysqli is after v4.1, he is using v3 :) Guys read the specs, if you want really to help!

@PLB 和@jammypeach mysqli 是在 v4.1 之后,他使用的是 v3 :) 如果您真的想提供帮助,请阅读规范!

You can't connect, because your socket file is a bit wrong. I remember now that the old RH had this issue before. Your socket is probably as /var/mysql/mysql.sock or /tmp/mysql.sock but one or more apps are looking for the other.

你无法连接,因为你的套接字文件有点错误。我现在记得旧的 RH 以前有过这个问题。您的套接字可能是 /var/mysql/mysql.sock 或 /tmp/mysql.sock 但一个或多个应用程序正在寻找另一个。

If yours is /tmp/mysql.sock but no /var/mysql/mysql.sock you should:

如果你的是 /tmp/mysql.sock 但不是 /var/mysql/mysql.sock 你应该:

cd /var 
mkdir mysql
cd mysql
ln -s /tmp/mysql.sock mysql.sock

If you have /var/mysql/mysql.sock but no /tmp/mysql.sock then:

如果您有 /var/mysql/mysql.sock 但没有 /tmp/mysql.sock 则:

cd /tmp
ln -s /var/mysql/mysql.sock mysql.sock

You'll need permissions to make the changes. Just sudo, if needed before the commands above!

您将需要权限才能进行更改。如果在上述命令之前需要,只需 sudo!

ANOTHER SOLUTION (easier):

另一个解决方案(更简单):

Create file and call phpinfo(); Look for 'mysql.default_socket'; or 'pdo_mysql.default_socket'; Open My.ini or My.cnf find the socket value e.g. socket=/tmp/mysql.sock Open your php.ini file (which is also found on your phpinfo() page as ‘Loaded Configuration File‘) and change all the occurrences of the incorrect socket location to the correct socket location from MySQL.

创建文件并调用 phpinfo(); 寻找“mysql.default_socket”;或 'pdo_mysql.default_socket'; 打开 My.ini 或 My.cnf 找到套接字值,例如 socket=/tmp/mysql.sock 打开您的 php.ini 文件(也可以在您的 phpinfo() 页面上找到“加载的配置文件”)并更改所有出现的内容错误的套接字位置从 MySQL 到正确的套接字位置。

ANOTHER SOLUTION (easiest): DSN for PDO:

另一个解决方案(最简单的): PDO 的 DSN:

mysql:unix_socket=/tmp/mysql.sock;dbname=...

mysql_connect:

mysql_connect:

$db = mysql_connect('localhost:/tmp/mysql.sock', ...

Your system is really scary when it comes to security, if you're hosting sensitive data, I'd upgrade to the latest versions.

你的系统在安全方面真的很可怕,如果你托管敏感数据,我会升级到最新版本。

---- UPDATE ----

- - 更新 - -

Aaahhhh PHP 5.0 and MySQL 3.23 :)

Aaahhhh PHP 5.0 和 MySQL 3.23 :)

PHP 5 has a mysql client packaged that cannot connect to a MySQL database less than version 4.1. Starting with version 4.1, MySQL uses a new way of password hashing that is not compatible with pre-4.1 databases. The server your configuration is connecting to is version 3.23. So you need to get yourself a higher version of MySQL. Sorry, but there is no other practical solution for your case. If I was you, I'd upgrade the whole system and install the most recent OS version, if I had to I'd go with Debian and the most recent stable versions of PHP and MySQL.

PHP 5 打包了一个 mysql 客户端,无法连接到低于 4.1 版本的 MySQL 数据库。从 4.1 版开始,MySQL 使用一种新的密码散列方法,该方法与 4.1 之前的数据库不兼容。您的配置连接到的服务器是 3.23 版。所以你需要给自己一个更高版本的MySQL。抱歉,您的情况没有其他实用的解决方案。如果我是你,我会升级整个系统并安装最新的操作系统版本,如果必须我会使用 Debian 和最新稳定版本的 PHP 和 MySQL。

回答by Shashank Agarwal

On a MAC OSX just restart the mysql server

在 MAC OSX 上只需重新启动 mysql 服务器

sudo /usr/local/mysql/support-files/mysql.server restart

It worked for me

它对我有用

回答by Sina

Fortunatly this is worked well for me:

幸运的是,这对我来说效果很好:

$db = mysql_connect('localhost:/var/lib/mysql/mysql.sock', 'Username', 'Password');

回答by Code_Help

I had a similar issue that took me several hours to solve. I placed Mysql / Mariadb on a separate drive and the PHP driven website was looking for the socket in the default location. So, I created a symbolic link but I think SELinux would not give the permission. So, then I added these lines to the /etc/php.ini file and it works.

我有一个类似的问题,我花了几个小时才解决。我将 Mysql / Mariadb 放在一个单独的驱动器上,PHP 驱动的网站正在寻找默认位置的套接字。所以,我创建了一个符号链接,但我认为 SELinux 不会给予许可。所以,然后我将这些行添加到 /etc/php.ini 文件中,它可以工作。

pdo_mysql.default_socket = /data/mysql/mysql.sock
mysql.default_socket = /data/mysql/mysql.sock
mysqli.default_socket = /data/mysql/mysql.sock

回答by lzdaniel

I use MAMP and has the sample problem. I found that there are no mysql.sock in tmp. so use this code

我使用 MAMP 并且有示例问题。我发现tmp中没有mysql.sock。所以使用这个代码

cd /tmp && ln -s /Applications/MAMP/tmp/mysql/mysql.sock

It worked for me

它对我有用