通过本地主机连接MySQL不起作用,但127.0.0.1起作用 [英] Connect MySQL through localhost not working but 127.0.0.1 is working
问题描述
操作系统: Ubuntu 14.04 LTS
OS: Ubuntu 14.04 LTS
php版本控制: phpbrew
php版本: 5.5.10
我对解析为127.0.0.1的本地主机执行ping操作.
I pinged localhost which resolved to 127.0.0.1.
这表明我的主机(/etc/hosts)文件是正确的.
This indicates that my host (/etc/hosts) file is correct.
127.0.0.1 localhost
每当我尝试使用如下所示的php脚本连接到MySQL时,它都无法正常工作,并给我一个错误:没有这样的目录.
Whenever I try connecting to MySQL using a php script like the one below it doesn't work and gives me the error: no such directory.
//connect to the database
mysql_connect("localhost","root","password") or die(mysql_error());
但是,当我通过127.0.0.1连接时,它将起作用
However, when I connect via 127.0.0.1 it will work
//connect to the database
mysql_connect("127.0.0.1","root","password") or die(mysql_error());
此外,使用"localhost"登录时,我的phpmyadmin无法正常工作,我不得不在登录期间更改文件以添加127.0.0.1选项.
Additionally, my phpmyadmin does not work when logging in using "localhost" i had to change the file to add 127.0.0.1 option during log on.
如何使用localhost连接到MySQL数据库?
How can I use localhost to connect to the MySQL database?
推荐答案
此问题的答案是位于my.cnf中
The answer to this problem was that in the my.cnf located within
/etc/mysql/my.cnf
该行是
bind-address = 127.0.0.1
或
bind-address = localhost
应该是
bind-address = 0.0.0.0
这将允许所有连接
这篇关于通过本地主机连接MySQL不起作用,但127.0.0.1起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!