Craft CMS Installation : Database Connection Error

Hi!

Is anyone familiar with Craft CMS?
Installation Process: https://docs.craftcms.com/v3/installation.html#step-1-download-craft
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.)
ENVIRONMENT="dev"

# The secure key Craft will use for hashing and encrypting data
SECURITY_KEY="F2zBz*4kyCxaH"$r"

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

# The database server name or IP address (usually this is 'localhost' or '127.0.0.1')
DB_SERVER="localhost"

# The database username to connect with
DB_USER="root"

# The database password to connect with
DB_PASSWORD="root"

# The name of the database to select
DB_DATABASE="craft"

# The database schema that will be used (PostgreSQL only)
DB_SCHEMA="public"

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

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

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!

2 Likes

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