我最初使用本地mysql数据库为我的cakephp 3应用程序,一切都正常工作。我正在部署我的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)
我不明白我做错了什么?当我切换回我拥有的数据库的本地副本时,一切都正常运行。任何帮助将不胜感激。
=================== 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)
在我的实例的AWS控制台中读取错误日志时,我得到了此输出。坦率地说,这是我第一次使用RDS,所以我不知道这意味着什么,但它可能对其他人有所帮助。
答案 0 :(得分: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'],
]
],