3

Hope you guys can help. I was working on my Craft site locally and now trying to upload it to the server but no matter what it says "Craft can’t connect to the database with the credentials in craft/config/db.php"

I have created the database in phpmyadmin along with the username and password.

Also my hosting provider is HostGator

This is what I have locally for the db.php.

return array(

    // The database server name or IP address. Usually this is 'localhost' or '127.0.0.1'.
    'server' => 'localhost',

    // The name of the database to select.
    'database' => 'simple-portfolio',

    // The database username to connect with.
     'user' => 'portfolio-rc',

    // The database password to connect with.
    'password' => 'r*****',

    // The prefix to use when naming tables. This can be no more than 5 characters.
    'tablePrefix' => 'craft',

);

And the server's db.php.

return array(

    // The database server name or IP address. Usually this is 'localhost' or '127.0.0.1'.
    'server' => 'localhost',

    // The name of the database to select.
    'database' => 'rchohan9_website',

    // The database username to connect with.
    'user' => 'rchohan9_portfol',

    // The database password to connect with.
    'password' => 'p************',

    // The prefix to use when naming tables. This can be no more than 5 characters.
    'tablePrefix' => 'craft',

);

Also the whole site is put in a folder so its not sitting in the root directory

Locally general.php looks like this

return array(
    'devMode' => true,
    'siteUrl' => array(
        'en' => 'http://localhost/portfolio',
    ),
    'environmentVariables' => array(
        'baseUrl'  => 'http://localhost/portfolio/',
    )
);

and on the server it's:

return array(
    'devMode' => true,
    'siteUrl' => array(
        'en' => 'http://rachnachohan.ca/website',
    ),
    'environmentVariables' => array(
        'baseUrl'  => 'http://rachnachohan.ca/website/',
    )
);

I am completely out of ideas of why its not able to connect to the db.

3
  • 2
    Based on your info, this is pretty much impossible to answer :) My only ideas are 1) Craft isn't loading the appropriate database config on your server – you can try adding a die("Hello world"); statement to the appropriate db.php file, just above the return statement, and refresh your browser – if the "Hello world" message displays, then you'll at least know that Craft is loading the correct config file. 2) The hostname, database name, username and/or password could be wrong. You should be able to confirm if the credentials are correct or not by logging in and out of phpMyAdmin. Oct 2, 2016 at 17:47
  • 3
    Also, you should look into Multi-Enviroment config setups, which is built into Craft. There's no need to have different general.php and db.php for your server and local environments; Craft makes it super easy to have different config values or different database credentials for multiple enviroments, all in the same file(s). Oct 2, 2016 at 17:49
  • I am having a similar problem with my GoDaddy account: craftcms.stackexchange.com/questions/17489/… What's mystifying me is that die('hello world') confirms the correct config file is being used, and I am able to login to phpMyAdmin using the exact same credentials in db.php. Oct 31, 2016 at 19:52

3 Answers 3

2

I know it's been a minute since you asked this question, but just in case - I encountered this same issue - the correct credentials but still not able to connect with the db.php config file. I changed "localhost" to "127.0.0.1" and that seemed to work, or at least get me to the next snag that I hit. Cheers!

1

It's likely one of two things:

  1. 'localhost' won't work because you may need to specific a the specific server. Sometimes this is just the IP address of the hosting server. Other times it's a hostname. Search HostGator's site or control panel for anything that specifies a mysql hostname. Or you can reach out to their support to see if they can provide it to you.

  2. It's also possible that you have not been given mysql db connection privileges. This is an odd one because it's unique circumstances that warrant this. But reaching out to HostGator support will determine whether or not this is the case.

1

This happens when you go from old craft versions, in the latest versions you have to have the config/db.php file like this:

<?php
use craft\helpers\App;

return [
    'dsn' => App::env('DB_DSN') ?: null,
    'driver' => App::env('DB_DRIVER'),
    'server' => App::env('DB_SERVER'),
    'port' => App::env('DB_PORT'),
    'database' => App::env('DB_DATABASE'),
    'user' => App::env('DB_USER'),
    'password' => App::env('DB_PASSWORD'),
    'schema' => App::env('DB_SCHEMA'),
    'tablePrefix' => App::env('DB_TABLE_PREFIX'),
];

Also make sure you have the correct database credentials in the .env file and delete a line called DSN if you have it.

I used composer.

Your Answer

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

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