令人困惑的 PDO 问题:无法通过套接字连接/访问被拒绝/无法连接到服务器(共享主机)
所以问题发生了变化,我将把原来的问题留在下面,以防止对答案进行不好的评论,就像有人编辑我回答的问题后得到的那样:
所以我正在开发一个安装了 PDO 的(非常蹩脚的)共享主机,但它不起作用。 使用默认参数,
<?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,它就可以工作。
并且套接字路径似乎是正确的(phpinfo和此查询:
show variables like 'socket';
确认。
本地主机重定向到10.103.0.14(此数据来自mysql_get_host_info()和phpMyAdmin)
在PDO中,如果我用127.0.0.1替换本地主机,我将得到
SQLSTATE[HY000] [2003] Can't connect to MySQL server on '127.0.0.1' (111)
如果我将 localhost 替换为 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 一起使用。
问题来自 PDO 连接。
所以显然 一些
服务器数据:
PHP 版本:5.2.10 您可以看到服务器的 phpinfo : http://web.lerelaisinternet.com/abcd.php? v=5 无法使用命令行。 (我知道这应该是技术支持的工作,但他们真的很慢)
谢谢
上一个问题:
如何在共享主机上找到mysql.sock(需要棘手的方法......)
所以今天的问题是:PDO连接不不能在共享主机上工作,它应该可以(它安装在服务器上)。 只是一个基本的 PDO 连接:
<?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 连接:
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
所以我想弄清楚它到底在哪里! 我尝试查看 phpMyAdmin 界面,但找不到信息,而且 phpMyAdmin 似乎连接到不同的服务器(它有不同的 IP 地址,并且尝试使用 php 连接到它会给出“密码错误”)错误)。 mysql_connect 也连接到这个地址,我认为它使用一些内部密码/登录重定向到不同的服务器。
好吧,如果您知道如何获取此信息(提供商的技术支持正在“解决问题”……已经 1 个月了……)。 也可能问题来自其他地方,但同样的东西可以在其他共享主机上工作...
PDO 的需要是因为我在这个网站上使用带有 Doctrine 的 Symfony 框架,而 Doctrine 插件需要 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 :
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();
}
?>
it throws this message :
SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
With a simple mysql_connect, it works.
And the socket path seems correct (both phpinfo and this query :
show variables like 'socket';
confirm.
Localhost redirects to 10.103.0.14 (this data comes from mysql_get_host_info() and in phpMyAdmin)
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)
And if i replace localhost by 10.103.0.14 :
Access denied for user 'USER_NAME'@'10.103.0.14' (using password: YES
Both IP adress (127.0.0.1 and 10.103.0.14) work with mysql_connect.
So apparently the problem comes from the PDO connection.
Does somebody knows where this could come from, or/and any way to fix it ?
Some server datas :
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)
Thanks
Previous question :
How to find the mysql.sock on a shared host (tricky way needed...)
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();
}
?>
throws this message :
SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
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/>';
works fine.
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
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.
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...
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 !
Thanks for your help !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
这已经被标记为已回答,但尚未真正解决(无需更改数据库)。
所以,以防万一像我这样的人也遇到这个问题...
解决这个问题的最简单方法是首先获取套接字路径(通过查看 php.ini 文件或使用: phpmyadmin 或控制台(或构建它)在 mysql 或 mysqli 中)
...运行以下查询(除 PDO 之外的任何查询):
然后,在 PDO 连接字符串中,将其更改为使用套接字而不是主机名:
这对我有用。
This was already marked as answered, but not really solved (without changing databases).
So, just in case someone like me also experiences this problem...
The easiest way to fix this is to first get the socket path (either by looking in the php.ini file or by using: phpmyadmin or the console (or construct it in mysql or mysqli)
...to run the following query (anything but PDO):
THEN, in the PDO connection string, change it to use the socket instead of a hostname:
This worked for me.
FWIW,我遇到了这个问题,并将主机从“localhost”更改为“127.0.0.1”。
我不知道为什么 localhost 不起作用,但这确实解决了问题。
奇怪的是,我们有大量的服务器,并且它几乎可以在所有使用“localhost”的服务器上运行
FWIW, I had this issue and changed my host from 'localhost' to '127.0.0.1'.
I have no clue why localhost wasn't working, but that did the trick.
Odd thing is, we have tons of servers and it works on almost every one using 'localhost'
您的服务器是否在启用(强制)SeLinux 的情况下运行?如果是,请尝试以 root 身份运行:
Is your server running with SeLinux enabled (enforcing)? If it is, try running as root:
您可以尝试使用
127.0.0.1
作为服务器名称而不是localhost
吗?IIRC,对于一些 mySQL 驱动程序/适配器,这决定了套接字是否用于建立连接。
Can you try
127.0.0.1
as the server name instead oflocalhost
?IIRC, with some mySQL drivers / adapters, this decides whether the socket is used for establishing the connection or not.
使用有效的连接,运行查询:(
这就像一个 select 语句)...您将获得正在运行的套接字的路径。
然后检查文件权限。
Using the connection which works, run the query:
(this behaves just like a select statement)...and you'll get the path of the running socket.
Then check the file permissions.
我遇到的问题是,生产版本工作正常,而测试版本无法连接 PDO:/
两个版本位于同一服务器上,在子目录中进行测试。
修复方法是在 DSN 中将 localhost 替换为 ip。
成为
I had the problem that production version worked just fine and a test version wasn't able to connect PDO :/
both versions was located at same servers, test in a sub directory.
The fix was replacing in DSN the localhost for ip.
became
尝试:
这应该显示配置的套接字。
try:
this should show you the configured socket.
我找到了奇怪行为的原因。如果绑定地址不同于 127.0.0.1 或 0.0.0.0(所有地址),PDO 无法连接到 127.0.0.1。
I found the reason for the strange behaviour. If bind-address is different to 127.0.0.1 or 0.0.0.0 (all addresses) PDO can't connect to 127.0.0.1.
无论如何,我在遇到完全相同的问题后找到了此页面。我在运行 Apache & 的服务器上仅限 PHP - MySQL 安装在另一台计算机上。我尝试了服务器的 DNS 名称及其 IP,并确认可以 ping 通它。同一台机器上的 PHP 应用程序正在使用旧语法 mysql_connect( ) 与数据库正常通信。但是 CLI 中的 PDO 抛出了这个错误。
我的解决方案是检查我的 DSN。 DSN 本身中的任何拼写错误都会被忽略,并且 PDO 假定您指的是 localhost。我的问题是 DSN 中的“name=”而不是“dbname=”。
For what it's worth, I found this page after having the exact same issue. I am on a server running Apache & PHP only - MySQL is installed on another machine. I tried both the DNS name of the server and its IP and confirmed I could ping it. A PHP app on the same machine is talking to the database fine, using old syntax mysql_connect( ). But PDO from the CLI was throwing this error.
The solution for me was to check my DSN. Any typo in the DSN itself is ignored silently, and PDO assumes you mean localhost. My issue was I had "name=" instead of "dbname=" in the DSN.
问题在Mysql配置中,您需要禁用
skip-networking
选项在 my.conf 配置文件中,这应该可以正常工作
参考
http://www.wolfcms.org/forum/post7098.html#p7098
The Issue In the Mysql configuration It you need to disable the option of
skip-networking
in my.conf configuration file this should work fine
reference
http://www.wolfcms.org/forum/post7098.html#p7098
我刚刚解决了类似的问题。我的猜测是您可能将 mysql_connect() 语句替换为 PDO 等效语句。不要忘记您还有许多其他代码依赖于旧的连接语句。在编写 PDO 代码时尝试将 mysql_connect 保持在适当的位置。
I just solved a similar issue. My guess is you probably replaced your mysql_connect() statement with the PDO equivalent. Don't forget you still have lots of other code dependent on that old connection statement. Try keeping the mysql_connect in place while writing in the PDO code.
对我有用的是指定端口号,如下所示:
mysql:hostname;port=3306;dbname=dbname;
这使它在连接到本地数据库时可以工作。现在我正在努力让它与远程数据库一起使用。
What worked for me was specifying the port number like so:
mysql:hostname;port=3306;dbname=dbname;
This got it to work when connecting to a local database. Now I'm working on getting it to work with a remote db.
我的问题可能与OP不同,但我认为值得发布。我在虚拟机上进行了软件升级,然后重新启动并收到了 OP 的错误消息。原来是内存不足的问题导致mysql无法启动。删除一些大文件就可以解决问题。
My problem may be different to the OP, but I thought it was worth posting. I did a software upgrade on a VM, then rebooted and got the OP's error message. It turned out to be an out-of-memory problem preventing mysql from starting. Deleting a few large files made the problem go away.
一年后,我找到了这个问题的解决方案:使用 SQLite 数据库。 PDO 工作正常,但不适用于 MySQL
** 编辑 **,因为每个人都对此表示反对:这解决了我的问题(我是OP)。我使用的是 Doctrine,因此切换 RDBMS 既简单又快捷。而且该网站是一些自制的 CMS,流量很少,所以 SQLite 就很好。
我知道这不是问题的真正“答案”,但如果有人处于相同的环境中:一个蹩脚的共享主机,你无法用这个奇怪的 PDO-MySQL 错误来改变它并且正在使用学说。这是一个解决方案。我可以删除这个答案,但如果我在OP时就想到了这一点,我会节省很多时间。
One year later, I found a solution for this issue : using a SQLite database. PDO worked fine, but not with MySQL
** 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.
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.