php 如何解决“无法通过套接字连接到本地MySQL服务器”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15177742/
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
How to solve "Can't connect to local MySQL server through socket"
提问by MD. ABDUL Halim
This is my config file:
这是我的配置文件:
$mysql_hostname = "localhost";
$mysql_user = "xyz";
$mysql_password = "abc";
$mysql_database = "mno";
$IT = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Opps some thing went wrong");
mysql_select_db($mysql_database, $IT) or die("Opps some thing went wrong");
Here is the warning that I get from it:
这是我从中得到的警告:
Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/03/10531303/html/iqamah.org/config.php on line 13
警告:mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/03/10531303/html/ iqamah.org/config.php 第 13 行
How can I fix this?
我怎样才能解决这个问题?
回答by Jav Rok
Please specify the system you're running on. If credentials are ok, this is probably a problem with mysql server. If you're on linux, check if this line returns something on the console. If not, your server is not running:
请指定您正在运行的系统。如果凭据没问题,这可能是 mysql 服务器的问题。如果您使用的是 linux,请检查此行是否在控制台上返回某些内容。如果没有,您的服务器没有运行:
# netstat -apn | grep mysql
You can also try to connect to mysql here too, so that you check your credentials are ok:
您也可以尝试在此处连接到 mysql,以便检查您的凭据是否正常:
# mysql -h localhost -u user database
If you're on windows, just check if the service is running:
如果您使用的是 Windows,只需检查服务是否正在运行:
- WindowsKey + R
- Type
services.msc - Check
msyqlservice is running.
- Windows 键 + R
- 类型
services.msc - 检查
msyql服务是否正在运行。
回答by Franck
I had exactly the same issue
我有完全相同的问题
- I verified the mysql.sock existed
- Update the localhost to 127.0.0.1 in the database.php config file. (user166390 comment)
- I also created a folder for the logs in admin/expressionengine/ folder
- Configured the error log levels in the config.php file
- And also the path to the log folder in this same config.php file
- 我验证了 mysql.sock 存在
- 在 database.php 配置文件中将 localhost 更新为 127.0.0.1。 (user166390 评论)
- 我还在 admin/expressionengine/ 文件夹中为日志创建了一个文件夹
- 在 config.php 文件中配置错误日志级别
- 以及同一个 config.php 文件中日志文件夹的路径
Hope that helps anyone having the same issue. Especially when the log folder didn't exist to troubleshoot.
希望对遇到同样问题的人有所帮助。特别是当日志文件夹不存在进行故障排除时。
回答by Trinimon
First check if your database is really listening; do this by calling
首先检查你的数据库是否真的在监听;通过调用来做到这一点
netstat -a
You should see something like this
你应该看到这样的东西
TCP 0.0.0.0:3306 MyComputer:0 LISTEN
If you don't seen a listening process on port 3306 your database is not (properly) started or uses another port. Check your environment carefully. If it's a WAMP/LAMP system you can check your admin pages for the phpinfo()section (mysqlisection)
如果您没有看到端口 3306 上的侦听进程,则您的数据库未(正确)启动或使用其他端口。仔细检查您的环境。如果它是 WAMP/LAMP 系统,您可以检查该phpinfo()部分的管理页面(mysqli部分)
If your port differs from the standard port try adding the appropriate port, e.g.
如果您的端口与标准端口不同,请尝试添加适当的端口,例如
$link = mysql_connect('example.com:3331', 'mysql_user', 'mysql_password');
Hope this helps.
希望这可以帮助。
回答by Manish Goyal
The error comes only when connection parameters are wrong, Make sure following credentials are correct
错误仅在连接参数错误时出现,请确保以下凭据正确
$mysql_hostname = "localhost";
$mysql_user = "xyz";
$mysql_password = "abc";
回答by Stepo
If you can't connect the database it means that somewhere in the execution process is a problem. Revise your hostname, user, password and database name. Then ensure if the database is really on your server with user data you have written to your code - log in with terminal or something like this...
如果无法连接数据库,则意味着执行过程中的某个地方出了问题。修改您的主机名、用户、密码和数据库名称。然后确保数据库是否真的在您的服务器上,其中包含您写入代码的用户数据 - 使用终端或类似方式登录...
But on line 13means, that you have to search problem on line 13. Check this line.
但这on line 13意味着,您必须在第 13 行搜索问题。检查此行。
回答by isaac.hunta
< Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'
<警告:mysql_connect():无法通过套接字'/var/lib/mysql/mysql.sock'连接到本地MySQL服务器
After reading some good advice from here. mine worked out for me. i tried removing the "localhost" and add 127.0.0.1 and it worked like magic vouch everyone contributed here
在阅读了这里的一些好的建议之后。我的为我工作。我尝试删除“本地主机”并添加 127.0.0.1 并且它的工作就像魔法保证每个人都在这里贡献

