Craft CMS Installation : Database Connection Error


Is anyone familiar with Craft CMS?
Installation Process:
I tried installing it through MAMP (not MAMP PRO), but I can’t get past Step 6.

When I try to access http:///index.php?p=admin/install, the page displays

Database Connection Error – craft\errors\DbConnectException
Craft CMS can’t connect to the database with the credentials in config/db.php.

I’m sure that the values match what’s in my database and that the page is loading the db.php I’m working on. Here’s what’s in my db.php.

return [
    'driver' => getenv('DB_DRIVER'),
    'server' => getenv('localhost'),
    'user' => getenv('craft'),
    'password' => getenv('jC8M4MEa38s7OrM4'),
    'database' => getenv('craft'),
    'schema' => getenv('DB_SCHEMA'),
    'tablePrefix' => getenv('craft'),
    'port' => getenv('DB_PORT')

And here’s my .env

# The environment Craft is currently running in ('dev', 'staging', 'production', etc.)

# The secure key Craft will use for hashing and encrypting data

# The database driver that will be used ('mysql' or 'pgsql')

# The database server name or IP address (usually this is 'localhost' or '')

# The database username to connect with

# The database password to connect with

# The name of the database to select

# The database schema that will be used (PostgreSQL only)

# The prefix that should be added to generated table names (only necessary if multiple things are sharing the same database)

# The port to connect to the database with. Will default to 5432 for PostgreSQL and 3306 for MySQL.

I’m a beginner in PHP and I have no idea what is the problem here. I tried changing other possible values for server, user and password, but no luck.

Can someone give me any idea on how I can fix this one? Thanks!

I would assume it’s because you’ve defined something called ‘DB_SERVER’, but then tell the script to look for an environment variable named ‘localhost’.

Revert your db.php to what it was (because i assume it came with ‘DB_SERVER’ listed there), and it should work correctly.

Also you might need to remove the " inside the middle of your security key.

1 Like

Hi, @m_hutley.

What you said fixed it. Thank you very much! :sob:

return [
    'driver' => getenv('DB_DRIVER'),
    'server' => getenv('DB_SERVER'),
    'user' => getenv('DB_USER'),
    'password' => getenv('DB_PASSWORD'),
    'database' => getenv('DB_DATABASE'),
    'schema' => getenv('DB_SCHEMA'),
    'tablePrefix' => getenv('DB_TABLE_PREFIX'),
    'port' => getenv('DB_PORT')

I was following this YouTube tutorial, so I changed the values in my db.php.

Anyway, thanks again!


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.