简体   繁体   中英

Amazon RDS and Zend Framework 2

I am having problem connecting to my Amazon RDS database from a Zend Framework 2 tutorial application.

I keep getting the following error:

PDOException: SQLSTATE[28000] [1045] Access denied for user 'db_user'@'localhost' (using password: YES) in C:\wamp\www\zf2-tutorial\vendor\ZendFramework\library\Zend\Db\Adapter\Driver\Pdo\Connection.php on line 214

I have no problem connecting to the database using MySQL Workbench or Toad so I don't think the DB Security Group is the issue. Also, the PHP application connects to the local database just fine too.

I am wondering why the error message says db_user'@'localhost . Shouldn't it say db_user'@'RDS host url ?

My connection string is:

'driver' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:hostname=xxxxxxxx.xxxxxxxxx.us-east-1.rds.amazonaws.com;dbname=zf2-tutorial',
'username' => 'db_user',
'password' => 'xxxxxxx',
'driver_options' => array(
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),

Turns out the problem is in the dsn string, unlike other MySQL servers, Amazon RDS does not understand "hostname". If changed to "host" it works just fine!

'dsn' => 'mysql: host =xxxxxxxx.xxxxxxxxx.us-east-1.rds.amazonaws.com;dbname=zf2-tutorial',

Thanks for all the suggestions.

Add the host variable to your config, as per Zend's documentation

'driver' => array(
    'driver' => 'Pdo',
    'dsn' => 'mysql:hostname=xxxxxxxx.xxxxxxxxx.us-east-1.rds.amazonaws.com;dbname=zf2-tutorial',
    'username' => 'db_user',
    'password' => 'xxxxxxx',
    'host'  => 'xxxxxxxx.xxxxxxxxx.us-east-1.rds.amazonaws.com', // Possible here
    'driver_options' => array(
        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
    ),
),
'host'  => 'xxxxxxxx.xxxxxxxxx.us-east-1.rds.amazonaws.com', // Most likely here

The MySQL PDO DSN expects host , not hostname:

http://php.net/manual/en/ref.pdo-mysql.connection.php

I've just made the same mistake following a ZF2 tutorial, baffled as to why the config was ignoring a hostname change, until I read the PHP PDO doc, changed it to host (as you have) and magically everything worked as it should

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