0

I used to connect to my database over SSL by directly modifying the createDbConnection method in Craft's AppBehavior.php:

$dbConnection = new DbConnection();

$dbConnection->connectionString = $this->_processConnectionString();
$dbConnection->emulatePrepare   = true;
$dbConnection->username         = craft()->config->get('user', ConfigFile::Db);
$dbConnection->password         = craft()->config->get('password', ConfigFile::Db);
$dbConnection->charset          = craft()->config->get('charset', ConfigFile::Db);
$dbConnection->tablePrefix      = $dbConnection->getNormalizedTablePrefix();
$dbConnection->driverMap        = array('mysql' => 'Craft\MysqlSchema');

// additional attributes to connect over SSL
if (getenv('DB_SSL_ENABLED') == 'true') 
{
    $dbConnection->attributes   = array(
        PDO::MYSQL_ATTR_SSL_CIPHER => 'DHE-RSA-AES256-SHA:AES128-SHA',
        PDO::MYSQL_ATTR_SSL_CA     => CRAFT_CONFIG_PATH . 'rds-combined-ca-bundle.pem',
    );
}

$dbConnection->init();

But as of a new Craft release there is a native Craft way to do this by setting a new "attributes" key in the database config file. Unfortunately, Craft won't make a connection when I connect using this new method. I get the dreaded Craft can’t connect to the database with the credentials in craft/config/db.php error. My revised db.php looks like this:

$url = parse_url(getenv("DATABASE_URL"));

define(SERVER, $url["host"]);
define(USER, $url["user"]);
define(PASSWORD, $url["pass"]);
define(DATABASE, substr($url["path"], 1));

return array(
    'server' => SERVER,
    'user' => USER,
    'password' => PASSWORD,
    'database' => DATABASE,
    'tablePrefix' => 'craft',
    'attributes' => array(
        PDO::MYSQL_ATTR_SSL_CIPHER => 'DHE-RSA-AES256-SHA:AES128-SHA',
        PDO::MYSQL_ATTR_SSL_CA     => CRAFT_CONFIG_PATH . 'rds-combined-ca-bundle.pem',
     )
);

Not sure what could be going wrong here. Are there additional attributes that need to be set when doing this the Yii way?

7
  • You're saying your first examples works when modifying AppBehavior directly? Craft is passing the attributes config setting directly into $dbConnection->attributes just like you're doing: github.com/pixelandtonic/Craft-Release/blob/master/app/etc/…
    – Brad Bell
    Commented Oct 25, 2016 at 18:40
  • Any chance you need to set PDO::MYSQL_ATTR_SSL_KEY and PDO::MYSQL_ATTR_SSL_CERT, too? dev.mysql.com/doc/refman/5.5/en/using-secure-connections.html
    – Brad Bell
    Commented Oct 25, 2016 at 18:42
  • When connecting to RDS it is recommended to use only use the ca cert. And I can confirm the connection works without the other keys since the first code block in AppBehavior can connect.
    – Leah Scott
    Commented Oct 25, 2016 at 20:19
  • I'm trying to think of other things that could be going wrong. Does PDO need to be escaped from the Craft namespace? I'm grasping at straws here.
    – Leah Scott
    Commented Oct 25, 2016 at 20:20
  • Good question... does \PDO:: work?
    – Brad Bell
    Commented Oct 25, 2016 at 20:35

1 Answer 1

0

Silly mistake. I forgot to update AppBehaviors.php to the newest release after moving the attributes logic to db.php:

$dbConnection = new DbConnection();

$dbConnection->connectionString = $this->_processConnectionString();
$dbConnection->emulatePrepare   = true;
$dbConnection->username         = craft()->config->get('user', ConfigFile::Db);
$dbConnection->password         = craft()->config->get('password', ConfigFile::Db);
$dbConnection->charset          = craft()->config->get('charset', ConfigFile::Db);
$dbConnection->tablePrefix      = $dbConnection->getNormalizedTablePrefix();
$dbConnection->driverMap        = array('mysql' => 'Craft\MysqlSchema');

// Support for Yii's $initSQLs
if ($initSQLs = craft()->config->get('initSQLs', ConfigFile::Db))
{
    $dbConnection->initSQLs = $initSQLs;
}
// See if we have any extra PDO attributes passed in.
if ($attributes = craft()->config->get('attributes', ConfigFile::Db))
{
    $dbConnection->attributes = $attributes;
}

$dbConnection->init();

After adding the new lines of code, I was able to connect :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.