Can#39;t connect to local MySQL server through socket(无法通过套接字连接到本地 MySQL 服务器)
问题描述
当我上传或提交页面时,我的网站出现以下错误:
I'm getting the following error on my site when I upload it or submit a page:
mysql_real_escape_string() [function.mysql-real-escape-string]: 无法通过socket '/var/lib/mysql/mysql.sock'连接本地MySQL服务器(2)
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() 的调用失败).
Since the error is being thrown by the call to mysql_real_escape_string() it rather implies that you didn't call mysql_connect() first and pass a valid db handle to mysql_real_escape_string (or the call to mysql_connect() failed).
在某些情况下,mysql 扩展将尝试使用 php.ini 中的默认设置自动连接,如果这些不可用,则故障转移到 my.cnf - 这显然是无效的.或者可能是设置有效但mysqld没有运行.
In some circumstances, the mysql extension will attempt to connect automatically using the default settings in php.ini, failing over to my.cnf if these are not available - which obviously are not valid. Or it may be that the settings are valid but the mysqld is not running.
你有成功连接到数据库的脚本吗?
Have you got scripts which are connecting to the database successfully?
你有数据库的用户名和密码吗?
Do you have a username and password for the database?
试试:
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 />
";
} else {
print "Failed with $host, $user, $password<br />
";
}
}
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 />
";
} else {
print "Cannot check mysqld process<br />
";
}
}
这篇关于无法通过套接字连接到本地 MySQL 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法通过套接字连接到本地 MySQL 服务器
基础教程推荐
- 在 SQL 中连接多个表 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 无法解决整理冲突 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
