使用 Zend 连接到 SQL 进程
我正在尝试将我的 Zend 应用程序连接到共享服务器上运行的 MySQL 进程。基本配置应该没问题,因为它与 LAMP 服务器一起使用。
问题是,我需要将主机指定为 sql 进程:myprocess.db
,而不是 localhost
:
resources.db.adapter = PDO_MYSQL
resources.db.params.charset = "utf8"
resources.db.params.host = mysqlprocess.db
resources.db.params.username = username
resources.db.params.password = password
resources.db.params.dbname = dbname
但是,当我这样做时,我得到
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]:
Can't connect to local MySQL server through socket 'please_see_the_faq' (2)
in /f5/metamusic/protected/application/controllers/SearchController.php on line 418
:我使用的主机是 NearlyFreeSpeech,当尝试连接到 SQL 而不指定您感兴趣的进程时,显然会触发此消息: http://faq.nearlyfreespeech.net/section/mysql/mysqllocalhost#mysqllocalhost
使用相同的详细信息和 mysql_connect($server, $user)
可以正常工作,因此看起来 Zend 不知何故没有使用正确的主机参数。
有什么想法出了什么问题吗?任何帮助将不胜感激。
I'm trying to connect my Zend application to a MySQL process running on a shared server. The basic config should be fine, as it was working with a LAMP server.
The problem is, I need to specify the host as being the an sql process: myprocess.db
, rather than localhost
:
resources.db.adapter = PDO_MYSQL
resources.db.params.charset = "utf8"
resources.db.params.host = mysqlprocess.db
resources.db.params.username = username
resources.db.params.password = password
resources.db.params.dbname = dbname
However, when I do, I get this:
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]:
Can't connect to local MySQL server through socket 'please_see_the_faq' (2)
in /f5/metamusic/protected/application/controllers/SearchController.php on line 418
The host I'm using is NearlyFreeSpeech, and this message is apparently triggered when attempting to connect to SQL without specifying the process you're interested in:
http://faq.nearlyfreespeech.net/section/mysql/mysqllocalhost#mysqllocalhost
Using the same details and mysql_connect($server, $user)
works without issue, so it looks like Zend is somehow not using the correct host parameter.
Any ideas what's going wrong? Any help would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用
resources.db.params.host = myprocess.db
try using
resources.db.params.host = myprocess.db
数据库配置中的主机必须指向数据库服务器。
localhost
或127.0.0.1
是与应用程序位于同一服务器上的数据库的引用。在托管环境中,服务器通常位于远程服务器上,因此主机必须是主机的 IP 地址或 DNS 名称。检查常见问题解答中的第二个问题。
更新
不好的是,这是关于 DSN 而不是 DNS。不过,这就是问题所在。配置中的
resources.db.params.host
指令需要对数据库服务器的引用,而myprocess.db
既不是 DNS 名称,也不是 IP 地址。您可能需要 localhost,但您仍然会丢失 DSN。我目前不知道如何在 PHP 中为 MySQL 以及 Zend 设置 DSN。进一步查看MYSQL DSN。更新2
您对套接字的看法是正确的,并且这是相关的。我认为问题出在 Zend PDO_MYSQL 适配器上。 Zend 将其直接传递给 PDO()。我上面提到的附加配置选项(MYSQL DSN)在 Zend 实现中缺失。尽管 PDO_MYSQL 适配器覆盖 connect() 方法,但它不会查找此选项。
然而,还有另一个适配器
mysqli
,它直接连接到MySQL,实际上与使用mysql_connect()
进行测试的方式相同。它使用 mysqli_real_connect() 来代替,并且该连接可能会理解套接字的进程名称。因此,您可以在配置中尝试以下操作:The host in the db config has to point to a database server.
localhost
or127.0.0.1
are references for the database being on the same server as the application. In a hosting environment you usually have the server on a remote server so the host has to be either an IP address or a DNS name for the host.Check the second question in the FAQ.
Update
My bad, that is about DSN and not DNS. Still, that's where the problem is. The
resources.db.params.host
directive in the config expects a reference to the database server andmyprocess.db
is neither a DNS name nor a IP address. You probably need localhost for that but then you will still be missing the DSN. I currently don't see how you set a DSN in PHP for MySQL and therefore Zend. Have a further look at this MYSQL DSN.Update 2
You are correct with the socket and that this is related. I think the problem is the Zend PDO_MYSQL adapter. Zend funnels this directly to PDO(). There are this additional config options I mentioned above (MYSQL DSN) which is missing in the Zend implementation. Although the PDO_MYSQL adapter overrides the connect() method it does not look for this options.
However, there is another adapter
mysqli
which connects directly to MySQL and actually the same way as your test withmysql_connect()
. It usesmysqli_real_connect()
instead and that connection might understand the process name for the socket. So, you can try the following in your config:我在这里发布我的最终解决方案以供将来参考:
事实证明,数据库连接已经工作。但是,我对 mysql_real_escape_string() 的调用失败,并且生成的错误消息表明整个数据库连接失败。
解决方案很简单,用 Zend_DB_Adapter 的
quote()
替换上面的调用,突然一切就正常了。为什么这适用于 LAMP 机器而不是共享服务器,我不知道。但就目前而言,这是一个足够好的解决方案!
I'm posting my eventual solution here for future reference:
It turns out, the database connection was already working. However, my call to
mysql_real_escape_string()
was failing, and the resulting error message suggested that the entire database connection had failed.The solution was simply to replace the above call with Zend_DB_Adapter's
quote()
, and suddenly everything works.Why this works on a LAMP machine and not a shared server, I have no idea. For now though, this is a good enough solution!