无法连接到 Kohana 中的远程 MySql 服务器

发布于 2024-12-03 06:54:46 字数 1034 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光无声 2024-12-10 06:54:46

当您激活 php 以显示已弃用的错误时,就会出现此问题。 mysql 驱动程序基于已弃用的 mysql_connect 库。当您的 php 在此库上抛出已弃用的错误时,Kohana 类无法处理该错误。
为了避免此错误,您需要在中添加以下代码行:
module->database->classes->kohana->database->mysql.php

查找以下代码行:

    // Prevent this information from showing up in traces
    unset($this->_config['connection']['username'], $this->_config['connection']['password']);

将以下行添加到文件中紧接上一行之后:

    //Added to suppress deprecated error due to deprecated driver
    error_reporting(E_ALL ^ E_DEPRECATED);

添加此行后,已弃用的错误代码将被抑制,您将能够使用 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:

    // Prevent this information from showing up in traces
    unset($this->_config['connection']['username'], $this->_config['connection']['password']);

Add the following line to the file right after the previous line:

    //Added to suppress deprecated error due to deprecated driver
    error_reporting(E_ALL ^ E_DEPRECATED);

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.

你列表最软的妹 2024-12-10 06:54:46

确保您能够在没有 Kohana 的情况下连接到远程 MySQL 服务器。其他一些提示:

Database::instance('default');

您不必指定 default,默认情况下是 default

$default->query(Database::SELECT, 'SELECT * FROM products', TRUE);

确保调用 ->execute() 方法来运行查询。

Make sure you're able to connect to the remote MySQL server without Kohana. Few other tips:

Database::instance('default');

You don't have to specify default, it's default by default.

$default->query(Database::SELECT, 'SELECT * FROM products', TRUE);

Make sure you call the ->execute() method to run the query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文