MySQL 远程数据库服务器连接失败

MySQL remote database server connect failed

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

但是显示如下错误

Error: SQLSTATE[HY000] [2002] No route to host

我在 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 is ok for this IP from my PC
  • There is no name server for this IP address

我该如何解决这个问题?

最后我通过一些步骤解决了这个问题-

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

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

查找并更改以下行

bind-address = 127.0.0.1

bind-address = 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 service apache2 restart

步骤 4(重启 mysql)

sudo service mysql restart

现在,我可以使用 CakePHP 3

连接远程数据库

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

ufw allow mysql