我很难将我的网站连接到主机上的 mySQL 数据库。然而,当我通过 Apache 在 PC 上运行我的网站时,它连接顺利。我用来连接的片段是:
<?php
$conn_error = 'Could not connect';
$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_pass = '';
$mysql_db = 'firstdatabase';
if(!mysql_connect($mysql_host,$mysql_user,$mysql_pass)|| !@mysql_select_db($mysql_db)){
die($conn_error);
}else{
//echo 'Connected';
}
?>
当我尝试连接到 web 主机上的 mysql 时,它给了我以下错误:
Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server
through socket 'MySQL' (2) in /home/umudo/public_html/connectserver.inc.php on line 10
我在网上搜索了解决方案,但找不到任何确切的例子。
最佳答案
帮助:http://dev.mysql.com/doc/refman/5.0/en/connecting.html
说:
On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file.
所以你必须
- 使用“127.0.0.1”而不是“localhost”来使用 tcpip 而不是套接字,或
- 在 mysql 服务器配置中启用套接字(套接字连接可能已禁用,这会在 linux 上导致该错误)
第二种方法更好 - 使用套接字进行本地连接比 TCP/IP 更好(性能更好),但您可能没有该服务器上 mysql 配置的权限。
关于php - "Can' t linux服务器上通过socket连接本地MySQL服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9443893/