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. Commented 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). Commented 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. Commented Oct 31, 2016 at 19:52

4 Answers 4

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!

2

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.

2

Frank from fortrabbit (hosting) here. I am using this thread to share some details about how we have been able to help some clients running into similar maybe more specific issues.

Issues

  • Craft control panel: "Empty value for 'port' specified" when attempting to create database backup
  • Craft Copy: "SSH Remote error: 255" when attempting to copy/db/down

Solutions

We found two ways to fix this (choose one):

  1. Add the ENV var DB_PORT=3306 - OR -
  2. In config/db.php add the line 'dsn' => App::env('DB_DSN') ?: null, the DB_DSN should be present for this (Craft 4 syntax)

What you require may depend on your configuration and the version of Craft CMS you came and you are right now.

Possible explanation

As explained above, the config of your Craft CMS config has not been updated during a Craft CMS update. With Craft CMS updates (specifically major versions) your config files in /config/* are not getting updated. That's because you may have custom config in there. Yet, still the default syntax with major Craft CMS versions changes. So you need to manually patch that. With Craft 4 the new DSN syntax to connect to the database was introduced. It's stored in an ENV var.

With fortrabbit hosting, when you create a Craft CMS App, we pre-populate ENV vars, using the syntax for the current Craft CMS version. When you update Craft CMS, ENV vars will not be updated along (impossible for us to catch).

The error from Craft Copy (our deployment tool) is related, since it's using the Craft internal backup functionality.

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.

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.