无法连接到 Kohana 中的远程 MySql 服务器
我正在尝试使用 Kohana 作为 php 应用程序,并且在连接到我的本地主机安装的 MySql 时进行了一个简单的数据库测试。但是,当我尝试更改与 Web 主机(Surftown)上的远程 MySql 数据库的连接时,它不起作用。
以下是来自 database.php 的连接数据(“默认”连接):
'hostname' => 'mydb23.surftown.se',
'database' => 'andclic_app',
'username' => 'myusername',
'password' => 'mypassword',
'persistent' => FALSE,
然后我在控制器中尝试此操作:
$default = Database::instance('default');
$result = $default->query(Database::SELECT, 'SELECT * FROM products', TRUE);
但我收到一条错误,提示不存在连接:
62 catch (Exception $e)
63 {
64 // No connection exists
65 $this->_connection = NULL;
66
67 throw new Database_Exception(':error',
68 array(':error' => $e->getMessage()),
69 $e->getCode());
70 }
71
72 // \xFF is a better delimiter, but the PHP driver uses underscore
那么我做错了什么?我还尝试使用 ip 号而不是主机名,并尝试附加端口 (:3306),但都没有帮助。
I'm trying out Kohana for a php application, and I have a simple database test working when connecting to my localhost installation of MySql. But when I try to change the connection to a remote MySql database on a web host (Surftown), it doesn't work.
Here are the connection data from database.php (the 'default' connection):
'hostname' => 'mydb23.surftown.se',
'database' => 'andclic_app',
'username' => 'myusername',
'password' => 'mypassword',
'persistent' => FALSE,
And then I try this in my controller:
$default = Database::instance('default');
$result = $default->query(Database::SELECT, 'SELECT * FROM products', TRUE);
But I get an error saying no connection exists:
62 catch (Exception $e)
63 {
64 // No connection exists
65 $this->_connection = NULL;
66
67 throw new Database_Exception(':error',
68 array(':error' => $e->getMessage()),
69 $e->getCode());
70 }
71
72 // \xFF is a better delimiter, but the PHP driver uses underscore
So what am I doing wrong? I also tried using the ip number instead of the host name, and I tried appending the port (:3306), but neither helped.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您激活 php 以显示已弃用的错误时,就会出现此问题。 mysql 驱动程序基于已弃用的 mysql_connect 库。当您的 php 在此库上抛出已弃用的错误时,Kohana 类无法处理该错误。
为了避免此错误,您需要在中添加以下代码行:
module->database->classes->kohana->database->mysql.php
查找以下代码行:
将以下行添加到文件中紧接上一行之后:
添加此行后,已弃用的错误代码将被抑制,您将能够使用 mysql_connect 扩展。
这是一个临时解决方法,因为最终您需要将代码迁移到新支持的扩展 mysqlite
此问题主要出现在最新版本的 php 中。在旧版本的 php 中您不会遇到此行为。
希望这对我有帮助,我花了一段时间才弄清楚这个问题。
This issue occurs when you have php activated to show deprecated errors. The mysql driver is based on the mysql_connect library which has been deprecated. When your php throws deprecated error on this library the Kohana class cannot handle the error.
To avoid this error you need to add the following line of code in :
modules->database->classes->kohana->database->mysql.php
Look for the following line of code:
Add the following line to the file right after the previous line:
After adding this line, the deprecated error code will be suppressed and you will be able to use the mysql_connect extension.
This is a temporary workaround since eventually you need to migrate your code to the new supported extension mysqlite
This issue occurs primariy with the latests versions of php. You won't experience this behavior in an older version of php.
Hope this helps it took me a while to figure this one out.
确保您能够在没有 Kohana 的情况下连接到远程 MySQL 服务器。其他一些提示:
您不必指定
default
,默认情况下是default
。确保调用
->execute()
方法来运行查询。Make sure you're able to connect to the remote MySQL server without Kohana. Few other tips:
You don't have to specify
default
, it'sdefault
by default.Make sure you call the
->execute()
method to run the query.