当我上传或提交页面时,我的网站出现以下错误:
mysql_real_escape_string() [function.mysql-real-escape-string]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
这到底是什么意思?
最佳答案
由于调用 mysql_real_escape_string()
会引发错误,这意味着您没有先调用 mysql_connect()
并将有效的数据库句柄传递给mysql_real_escape_string
(或调用 mysql_connect()
失败)。
在某些情况下,mysql 扩展将尝试使用 php.ini 中的默认设置自动连接,如果这些设置不可用,则故障转移到 my.cnf - 这显然是无效的。或者可能是设置有效但 mysqld 未运行。
你有成功连接数据库的脚本吗?
您有数据库的用户名和密码吗?
尝试:
check_running();
$user=''; // fill in your details
$password=''; // fill in your details
$hosts=array(
'localhost', '127.0.0.1', $_SERVER['HTTP_HOST'], $_SERVER['SERVER_ADDR']
);
foreach ($hosts as $addr) {
try_con($addr, $user, $password);
try_con($addr . ':3306', $user, $password);
}
function try_con($host, $user, $password)
{
$dbh=mysql_connect($host, $user, $password);
if ($dbh) {
print "Connected OK with $host, $user, $password<br />\n";
} else {
print "Failed with $host, $user, $password<br />\n";
}
}
function check_running()
{
// this assumes that you are using Apache on a Unix/Linux box
$chk=`ps -ef | grep httpd | grep -v grep`;
if ($chk) {
print "Checking for mysqld process: " . `ps -ef | grep mysqld | grep -v grep` . "<br />\n";
} else {
print "Cannot check mysqld process<br />\n";
}
}
关于mysql - 无法通过套接字连接到本地MySQL服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2182919/