简体   繁体   中英

CakePHP3 Application error with AWS RDS Database

I initially used a local mysql database for my cakephp 3 application and everything was working as it should. I'm in the process of deploying my cakephp3 application so I created a RDS MySql instance and connected it to MySql workbench and just copied the database I had before to the RDS database. My settings in my app.php are like so.

To define my constants I have

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']);
}

Then in the datasources section of the app.php file

'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,

On the AWS Console it says that I have a connection to the RDS instance. However, when I try to register/login I get a CakePHP internal error has occurred. I then checked the error logs and I found this in my error.log file.

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)

I don't understand what I'm doing wrong? When I switch back to the local copy of the database I have, everything functions as should. Any help would be appreciated.

===================EDIT=======================

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)

When reading the error logs in the AWS console for my instance, I got this output. Frankly, this is my first time using RDS so I have no idea what this means but it may help to others.

You should only write this no need to $_SERVER. i am also connect cakephp 3.01 AWS RDS below is my code.

   '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'],
    ]
   ],

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM