0

I've moved my site to the staging server but now I'm getting a craft can't connect to the database with credentials in craft/config/db.php

Thing is I uploaded this script with the same credentials and it connects just fine. Any idea what I could be doing wrong

<?php
# Fill our vars and run on cli
# $ php -f db-connect-test.php
$dbname = 'name';
$dbuser = 'user';
$dbpass = 'pass';
$dbhost = 'localhost';
$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
mysql_select_db($dbname) or die("Could not open the db '$dbname'");
​
echo "connected to the database";

My full db.php file with actual db creds removed - updated to include domain from live site.

<?php
​
/**
 * Database Configuration
 *
 * All of your system's database configuration settings go in here.
 * You can see a list of the default settings in craft/app/etc/config/defaults/db.php
 */
​
return array(
    '*' => array(
        'tablePrefix' => 'craft',
    ),
    'nec.dev' => array(
        'server' => 'localhost',
        'user' => 'root',
        'password' => 'root',
        'database' => 'nec',
    ),
    'nec.dev.stagingdomain.com' => array(
        'server' => 'localhost',
        'user' => 'staging-user',
        'password' => 'staging-password',
        'database' => 'staging-database',
    ),
    'nec-inc.com' => array(
        'server' => 'localhost',
        'user' => '',
        'password' => '',
        'database' => '',
    ),
);

I can't figure out why this doesn't work on staging when the same credentials work with the connection testing script.

4
  • The domain you're getting the error on is nec.dev.stagingdomain.com?
    – Brad Bell
    Commented Jul 21, 2016 at 22:22
  • @BradBell yes. Updating snippet with new, still non working code, based on a response in slack
    – CreateSean
    Commented Jul 22, 2016 at 13:42
  • The staging url seems strange. Would 'nec.stagingdomain.com' => array(...) work?
    – Damon
    Commented Jul 22, 2016 at 13:45
  • I'm such an idiot. I realized I had the staging subdomains in the wrong order. it should be dev.nec.stagingdomaain.com now all working
    – CreateSean
    Commented Jul 22, 2016 at 13:47

1 Answer 1

3

It looks as if you've sorted thing out (dev.nec... vs nec.dev...)

In general, I find the domain name matching in the config files to be confusing and error prone. I always follow the advice in the Foolproof Multi-Environment Config recipe. Put this at the top of your index.php:

switch (strtolower($_SERVER['SERVER_NAME'])) {
  case 'nec.dev':
    define('CRAFT_ENVIRONMENT', 'DEV');
    break;
  case 'nec.dev.stagingdomain.com':
    define('CRAFT_ENVIRONMENT', 'STAGE');
    break;
  case 'nec-inc.com':
    define('CRAFT_ENVIRONMENT', 'LIVE');
    break;
  default:
    exit('No environment defined for '.$_SERVER['SERVER_NAME']);
}

Then your db.php and general.php will look like this:

return array(
  '*' => array(...),
  'DEV' => array(...),
  'STAGE' => array(...),
  'LIVE' => array(...),
);

And when you hit dev.nec.stagingdomain.com you will see that No environment defined error and fairly quickly see what is wrong.

(There is also an Enhanced Foolproof Multi-Environment Config recipe)

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.