1

我最初为我的 cakephp 3 应用程序使用了一个本地 mysql 数据库,并且一切正常。我正在部署我的 cakephp3 应用程序,所以我创建了一个 RDS MySql 实例并将其连接到 MySql 工作台,然后将我之前拥有的数据库复制到 RDS 数据库中。我在 app.php 中的设置是这样的。

要定义我的常量,我有

if (!defined('RDS_HOSTNAME')) {
  define('RDS_HOSTNAME', $_SERVER['myendpoint.us-west-1.rds.amazonaws.com']);
  define('RDS_USERNAME', $_SERVER['myusername']);
  define('RDS_PASSWORD', $_SERVER['mypassword']);
  define('RDS_DB_NAME', $_SERVER['my_db']);
}

然后在 app.php 文件的数据源部分

'Datasources' => [
    'default' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => RDS_HOSTNAME,
        /**
         * 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' => 'non_standard_port_number',
        'username' => RDS_USERNAME,
        'password' => RDS_PASSWORD,
        'database' => RDS_DB_NAME,
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'flags' => [],
        'cacheMetadata' => true,
        'log' => false,

在 AWS 控制台上,它说我与 RDS 实例建立了连接。但是,当我尝试注册/登录时,出现了 CakePHP 内部错误。然后我检查了错误日志,并在我的 error.log 文件中找到了它。

2016-06-20 17:25:35 Error: [PDOException] SQLSTATE[HY000] [1045] Access denied for user ''@'localhost' (using password: NO)
Request URL: /users/register
Referer URL: http://localhost:8765/
Client IP: 127.0.0.1
Stack Trace:
#0 /home/danielparkk/Desktop/SubReminder/vendor/cakephp/cakephp/src/Database/Driver/PDODriverTrait.php(47): PDO->__construct('mysql:host=;por...', NULL, NULL, Array)
#1 /home/danielparkk/Desktop/SubReminder/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php(90): Cake\Database\Driver\Mysql->_connect('mysql:host=;por...', Array)

我不明白我做错了什么?当我切换回我拥有的数据库的本地副本时,一切正常。任何帮助,将不胜感激。

===================编辑=======================

2016-06-21T00:22:17.815360Z 24 [Note] Access denied for user 'root'@'108-83-57-226.lightspeed.irvnca.sbcglobal.net' (using password: NO)
2016-06-21T00:34:38.776248Z 26 [Note] Aborted connection 26 to db: 'subscriptions_db' user: 'root' host: '108-83-57-226.lightspeed.irvnca.sbcglobal.net' (Got timeout reading communication packets)
2016-06-21T00:34:42.872265Z 25 [Note] Aborted connection 25 to db: 'subscriptions_db' user: 'root' host: '108-83-57-226.lightspeed.irvnca.sbcglobal.net' (Got timeout reading communication packets)

在我的实例的 AWS 控制台中读取错误日志时,我得到了这个输出。坦率地说,这是我第一次使用 RDS,所以我不知道这意味着什么,但它可能对其他人有所帮助。

4

1 回答 1

0

你应该只写这个不需要$_SERVER。我也连接 cakephp 3.01 AWS RDS 下面是我的代码。

   'Datasources' => [
    'default' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
         'host' => 'yourendpoint.us-west-1.rds.amazonaws.com',
         'username' => 'rdsusername',
         'password' => 'rdspassword',
         'database' => 'rdsdatabasename',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'cacheMetadata' => true,

        /**
         * Set identifier quoting to true if you are using reserved words or
         * special characters in your table or column names. Enabling this
         * setting will result in queries built using the Query Builder having
         * identifiers quoted when creating SQL. It should be noted that this
         * decreases performance because each query needs to be traversed and
         * manipulated before being executed.
         */
        'quoteIdentifiers' => false,

        /**
         * During development, if using MySQL < 5.6, uncommenting the
         * following line could boost the speed at which schema metadata is
         * fetched from the database. It can also be set directly with the
         * mysql configuration directive 'innodb_stats_on_metadata = 0'
         * which is the recommended value in production environments
         */
        //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
    ]
   ],
于 2016-06-21T06:13:06.917 回答