简体   繁体   English

警告:mysql_connect():无法连接到本地 MySQL 服务器

[英]Warning: mysql_connect(): Can't connect to local MySQL server

Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (11) in /home/aa/public_html/bb/db.php on line 2 Could not connect:警告:mysql_connect():无法通过第 2 行 /home/aa/public_html/bb/db.php 中的套接字 '/var/lib/mysql/mysql.sock' (11) 连接到本地 MySQL 服务器无法连接:

iam getting this error but the problem is that this error comes irregularly,sometimes it didnt come all the day and sometimes it comes countless times.我收到此错误,但问题是此错误不定期出现,有时一整天都没有出现,有时会出现无数次。

what could be the possible reason for this?????这可能是什么原因?????

That could mean that MYSQL is down or you use the wrong host name while connect.这可能意味着 MYSQL 已关闭或您在连接时使用了错误的主机名。 One more possible reason for that could be difference in socket configuration of php against mysql, you can check it in by looking at entry socket in mysql config.另一个可能的原因可能是 php 与 mysql 的套接字配置不同,您可以通过查看 mysql 配置中的入口套接字来检查它。 file and by looking at output of phpinfo(), you need just to compare it.文件并通过查看 phpinfo() 的 output,您只需要比较它。 Or it could that someone else on your machine also using that socket.或者它可能是您机器上的其他人也在使用该套接字。

PS.附言。 As well my wild guess, go through your code and check you always get your connection close right and all your queries as well.以及我的疯狂猜测,go 通过您的代码并检查您是否始终正确关闭连接以及所有查询。

Make sure that your MySQL is running.确保您的 MySQL 正在运行。 OR Create a file called: phpinfo.php with the following inside it:或者创建一个名为: phpinfo.php的文件,其中包含以下内容:

<?php phpinfo(); ?>

Load the file in your browser: http://localhost/phpinfo.php and scroll down for mysql .在浏览器中加载文件: http://localhost/phpinfo.php并向下滚动找到mysql Look for the MYSQL_SOCKET and make sure it matches your entry in your my.cnf file .查找MYSQL_SOCKET并确保它与您在my.cnf 文件中的条目匹配。

FYI, my my.cnf file is located in: /etc/my.cnf and it contains something like this:仅供参考,我的 my.cnf 文件位于: /etc/my.cnf ,它包含如下内容:

# The MySQL server
[mysqld]
port            = 3306
socket          = /tmp/mysql.sock

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM