<p>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!</p>

CC BY-SA 3.0
2017-05-16T14:23:38.893Z
2017-05-16T14:23:38.893Z
ingernet
<p>This happens when you go from old craft versions, in the latest versions you have to have the <code>config/db.php</code> file like this:</p>
<pre><code>&lt;?php
use craft\helpers\App;

return [
    'dsn' =&gt; App::env('DB_DSN') ?: null,
    'driver' =&gt; App::env('DB_DRIVER'),
    'server' =&gt; App::env('DB_SERVER'),
    'port' =&gt; App::env('DB_PORT'),
    'database' =&gt; App::env('DB_DATABASE'),
    'user' =&gt; App::env('DB_USER'),
    'password' =&gt; App::env('DB_PASSWORD'),
    'schema' =&gt; App::env('DB_SCHEMA'),
    'tablePrefix' =&gt; App::env('DB_TABLE_PREFIX'),
];
</code></pre>
<p>Also make sure you have the correct database credentials in the <code>.env</code> file and delete a line called DSN if you have it.</p>
<p>I used composer.</p>

CC BY-SA 4.0
2021-02-18T17:35:35.577Z
2021-02-26T15:20:10.610Z
2021-02-26T15:20:10.610Z
Alexis Dalai Waldo Jiménez
<p>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.</p>
<p><strong>Issues</strong></p>
<ul>
<li>Craft control panel: &quot;Empty value for 'port' specified&quot; when attempting to create database backup</li>
<li>Craft Copy: &quot;SSH Remote error: 255&quot; when attempting to <code>copy/db/down</code></li>
</ul>
<p><strong>Solutions</strong></p>
<p>We found two ways to fix this (choose one):</p>
<ol>
<li>Add the ENV var <code>DB_PORT=3306</code> - OR -</li>
<li>In <code>config/db.php</code> add the line <code>'dsn' =&gt; App::env('DB_DSN') ?: null</code>, the DB_DSN should be present for this (Craft 4 syntax)</li>
</ol>
<p>What you require may depend on your configuration and the version of Craft CMS you came and you are right now.</p>
<p><strong>Possible explanation</strong></p>
<p>As explained <a href="https://craftcms.stackexchange.com/a/37237/4504">above</a>, 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 <code>/config/*</code> 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.</p>
<p>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).</p>
<p>The error from Craft Copy (our deployment tool) is related, since it's using the Craft internal backup functionality.</p>

CC BY-SA 4.0
2024-01-12T07:52:50.203Z
2024-01-27T10:33:26.767Z
2024-01-27T10:33:26.767Z
Frank Lämmer
<p>It's likely one of two things:</p>

<ol>
<li><p>'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.</p></li>
<li><p>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. </p></li>
</ol>

CC BY-SA 3.0
2017-05-19T20:59:07.223Z
2017-05-19T20:59:07.223Z
Noah Yamen
<p>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"</p>

<p>I have created the database in phpmyadmin along with the username and password. </p>

<p>Also my hosting provider is HostGator</p>

<p>This is what I have locally for the <code>db.php</code>.</p>

<pre><code>return array(

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

    // The name of the database to select.
    'database' =&gt; 'simple-portfolio',

    // The database username to connect with.
     'user' =&gt; 'portfolio-rc',

    // The database password to connect with.
    'password' =&gt; 'r*****',

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

);
</code></pre>

<p>And the server's <code>db.php</code>. </p>

<pre><code>return array(

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

    // The name of the database to select.
    'database' =&gt; 'rchohan9_website',

    // The database username to connect with.
    'user' =&gt; 'rchohan9_portfol',

    // The database password to connect with.
    'password' =&gt; 'p************',

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

);
</code></pre>

<p>Also the whole site is put in a folder so its not sitting in the root directory</p>

<p>Locally <code>general.php</code> looks like this </p>

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

<p>and on the server it's:</p>

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

<p>I am completely out of ideas of why its not able to connect to the db.</p>

CC BY-SA 3.0
2016-10-02T17:47:11.347Z
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.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-10-02T17:49:34.340Z
Also, you should look into [Multi-Enviroment config setups](https://craftcms.com/docs/multi-environment-configs#adding-multi-environment-configs-to-db-php), 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).
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-10-31T19:52:44.283Z
I am having a similar problem with my GoDaddy account: http://craftcms.stackexchange.com/questions/17489/how-to-get-godaddy-to-connect-to-db

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`.
Steven Thate
<database>
Craft can't connect to db
# Craft can't connect to db

(Asked by: username_0 on 2016-10-02)

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.

**Tags:** database
> **Comments:**
> **username_1**: 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.  
> **username_1**: Also, you should look into [Multi-Enviroment config setups](https://craftcms.com/docs/multi-environment-configs#adding-multi-environment-configs-to-db-php), 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).  
> **username_2**: I am having a similar problem with my GoDaddy account: http://craftcms.stackexchange.com/questions/17489/how-to-get-godaddy-to-connect-to-db  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`.  

---

## 4 answers

### Answer by username_3 on 2017-05-16. Score: 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!


---

### Answer by username_4 on 2021-02-18. Score: 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:
 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.


---

### Answer by username_5 on 2024-01-12. Score: 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](https://craftcms.stackexchange.com/a/37237/4504), 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.


---

### Answer by username_6 on 2017-05-19. Score: 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.


---

