沃梦达 / 编程问答 / php问题 / 正文

令人困惑的仅 PDO 问题:无法通过套接字连接/访问被拒绝/无法连接到服务器(共享主机)

Confusing PDO-only problem : Can#39;t connect through socket/Access denied/Can#39;t connect to server (shared host)(令人困惑的仅 PDO 问题:无法通过套接字连接/访问被拒绝/无法连接到服务器(共享主机))

本文介绍了令人困惑的仅 PDO 问题:无法通过套接字连接/访问被拒绝/无法连接到服务器(共享主机)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以问题改变了原来的问题,我将把原来的问题留在下面,以防止在有人编辑我回答的问题后对我的答案产生差评:

So the problem changed from what it was, i'll leave the original question below to prevent bad reviews on answers like I had after someone editing his question I answered :

所以我正在开发一个安装了 PDO 的(非常蹩脚的)共享主机,但它不起作用.带默认参数

So I am working on a (really lame) shared hosting which has PDO installed, but it doesn't work. With default parameters

<?php
try {
    $dbh = new PDO('mysql:host=localhost;dbname=THE_DB_NAME', 'THE_USER', 'THE_PASSWORD');
    echo 'Connected to database';
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
?>

它抛出这个消息:

SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

通过一个简单的 mysql_connect,它就可以工作了.

With a simple mysql_connect, it works.

而且套接字路径似乎是正确的(phpinfo 和这个查询:

And the socket path seems correct (both phpinfo and this query :

show variables like 'socket';

确认.

Localhost 重定向到 10.103.0.14(此数据来自 mysql_get_host_info() 和 phpMyAdmin)

Localhost redirects to 10.103.0.14 (this data comes from mysql_get_host_info() and in phpMyAdmin)

在 PDO 中,如果我将 localhost 替换为 127.0.0.1 我会得到

In the PDO, if i replace localhost by 127.0.0.1 i will get

SQLSTATE[HY000] [2003] Can't connect to MySQL server on '127.0.0.1' (111) 

如果我将 localhost 替换为 10.103.0.14 :

And if i replace localhost by 10.103.0.14 :

Access denied for user 'USER_NAME'@'10.103.0.14' (using password: YES

两个 IP 地址(127.0.0.1 和 10.103.0.14)都可以与 mysql_connect 一起使用.

Both IP adress (127.0.0.1 and 10.103.0.14) work with mysql_connect.

显然问题出在 PDO 连接上.

So apparently the problem comes from the PDO connection.

有人知道这可能来自哪里,或/和任何解决方法吗?

Does somebody knows where this could come from, or/and any way to fix it ?

一些服务器数据:

PHP 版本:5.2.10你可以看到服务器的phpinfo:http://web.lerelaisinternet.com/abcd.php?v=5没有命令行可能.(我知道这应该是技术支持的工作,但他们真的很慢)

The PHP Version : 5.2.10 You can see the server's phpinfo : http://web.lerelaisinternet.com/abcd.php?v=5 No command line possible. (i know it should be the tech suport's job, but they're reaaaaaly slow)

谢谢

上一个问题:

所以今天的问题是:PDO 连接在共享主机上不起作用,它应该(它安装在服务器上).只是一个基本的 PDO 连接:

So today's problem is : The PDO connection doesn't work on a shared host, and it's supposed to (it's installed on the server). Just a basic PDO connection :

<?php
try {
    $dbh = new PDO('mysql:host=localhost;dbname=THE_DB_NAME', 'THE_USER', 'THE_PASSWORD');
    echo 'Connected to database';
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
?>

抛出此消息:

SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

一个常规的mysql连接:

A regular mysql connection :

mysql_connect("localhost", "THE_USER", "THE_PWD") or die(mysql_error()); 
mysql_select_db("24DLJLRR1") or die(mysql_error());;
echo 'Connected to database <br/>';

工作正常.

显然它找不到.sock.我认为指定正确的地址应该有效,我尝试了一些在互联网上找到的经典"mysql 路径,但没有成功.phpinfo 说它在这个地址(/var/lib/mysql/mysql.sock)(PHP 版本为 5.2.10)你可以看到服务器的phpinfo:http://web.lerelaisinternet.com/abcd.php?v=5

So apparently it cannot find the .sock. I think specifying the correct address should work, i tried some "classic" mysql path that I found on internet, without success. The phpinfo says it is at this adress (/var/lib/mysql/mysql.sock) (The PHP Version is 5.2.10) You can see the server's phpinfo : http://web.lerelaisinternet.com/abcd.php?v=5

所以我想弄清楚它到底在哪里!!!我试图查看 phpMyAdmin 界面,但我找不到信息,而且似乎 phpMyAdmin 连接到不同的服务器(它有不同的 IP 地址,并且尝试使用 php 连接到它会给出密码错误"错误).mysql_connect 也连接到这个地址,我认为它使用一些内部密码/登录名重定向到不同的服务器.

So i am trying to figure out where the hell it is !!! I tried to look in the phpMyAdmin interface, but i couldn't find the info, plus it seems that phpMyAdmin connects to a different server (it has a different IP adress, and trying to connect to it with php gives a "Wrong password" error). The mysql_connect also connects to this adress, i think it redirects to a different server with some internal password/login.

如果您对如何获取此信息有任何想法(提供商的技术支持是解决问题"...已经 1 个月...).也可能问题来自其他地方,但同样的东西适用于其他共享主机......

Well if you have any idea of how to obtain this info (the provider's technical support is "fixing the problem"... it's been 1 month...). Also maybe the problem comes from somewhere else, but the same stuff works on other shared hosts...

需要PDO是因为我这个网站使用Symfony框架和Doctrine,而Doctrine插件需要PDO...我不想从头重做网站!

The need of PDO is because I use the Symfony framework with Doctrine for this website, and the Doctrine plugin needs PDO... I don't want to redo the website from scratch !

感谢您的帮助!

推荐答案

一年后,我找到了解决这个问题的方法:使用 SQLite 数据库.PDO 工作正常,但不适用于 MySQL

One year later, I found a solution for this issue : using a SQLite database. PDO worked fine, but not with MySQL

** 编辑 ** 因为每个人都对此表示反对:这解决了我的问题(我是 OP).我使用的是 Doctrine,所以切换 RDBMS 既简单又快捷.此外,该网站是一些自制的 CMS,流量很少,所以 SQLite 很好.

** EDIT ** as everyone is downvoting this: This solved my issue (I'm the OP). I was using Doctrine, so switching RDBMS was easy and quick. Also the website was some a home made CMS, with very few trafic, so SQLite was fine.

我知道这不是问题的真正答案",但如果有人处于相同的环境中:一个糟糕的共享主机,你无法用这个奇怪的 PDO-MySQL 错误来改变它并且正在使用教义.这是一个解决方案.我可以删除这个答案,但如果我在 OP 的时候想到这一点,我会节省很多时间.

I know it's not a real "Answer" to the problem, but if someone is in the same context: a crappy shared hosting which you can't change with this weird PDO-MySQL bug AND is using doctrine. This IS a solution. I can delete this answer, but if I had thought of this at the time of the OP, I would have saved a lot of time.

这篇关于令人困惑的仅 PDO 问题:无法通过套接字连接/访问被拒绝/无法连接到服务器(共享主机)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:令人困惑的仅 PDO 问题:无法通过套接字连接/访问被拒绝/无法连接到服务器(共享主机)