2

我正在尝试Cakephp 3.x从本地计算机连接远程数据库服务器。

但它显示以下错误

错误:SQLSTATE[HY000] [2002] 没有到主机的路由

我的本地数据库配置app.php看起来像 -

'Datasources' => [
    'default' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => '192.168.1.19',
        /**
         * CakePHP will use the default DB port based on the driver selected
         * MySQL on MAMP uses port 8889, MAMP users will want to uncomment
         * the following line and set the port accordingly
         */
        'port' => '3306',
        'username' => 'remote_db_user',
        'password' => 'my_password',
        'database' => '********',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'flags' => [],
        'cacheMetadata' => true,
        'log' => false,
     ],
     /*Other configs*/
]

关于远程服务器:

  • ping 192.168.1.19我的电脑上的这个 IP 没问题
  • IP此地址没有名称服务器

我该如何解决这个问题?

4

2 回答 2

3

请检查防火墙规则。例如,如果您的 MySQL 运行端口是 3306,请检查它是否打开。请检查此规则是否允许。

ufw允许mysql

于 2021-06-28T07:18:59.437 回答
3

最后我按照一些步骤解决了这个问题-

第 1 步(编辑 mysql 配置文件)

/etc/mysql/mysql.conf.d/mysqld.cnf

查找并更改以下行

绑定地址 = 127.0.0.1

绑定地址 = 0.0.0.0

步骤 2(授予用户数据库权限)

CREATE USER 'remote_db_user'@'%' IDENTIFIED BY 'my_password';

GRANT ALL PRIVILEGES ON *.* TO 'remote_db_user'@'%' WITH GRANT OPTION;

第 3 步(重新启动 Apache)

sudo 服务 apache2 重启

第四步(重启mysql)

sudo 服务 mysql 重启

现在,我可以使用连接远程数据库了CakePHP 3

于 2016-10-26T10:17:42.747 回答