Mac 中PHP连接MySQL总是失败localhost修改成127.0.0.1解决
连接代码:
<?
$link = mysql_connect('localhost','root','zxc8585');
if(!$link) {
echo "数据库链接失败";
}else {
echo "数据库链接成功";
}
mysql_close();
?>
结果却返回这个:
Warning: mysql_connect(): No such file or directory in /Library/WebServer/Documents/mysql.php on line 4
数据库链接失败
Warning: mysql_close(): no MySQL-Link resource supplied in /Library/WebServer/Documents/mysql.php on line 10
往上找了很多资料都没解决,后来发现配置phpMyAdmin的时候,需要将
$cfg['Servers'][$i]['host'] = 'localhost';
修改为
$cfg['Servers'][$i]['host'] = '127.0.0.1';
不太清楚为什么会这样,看到一篇文章可做参考:http://www.jb51.net/article/50422.htm