0

I'm trying to upgrade from 2.7.10 to Craft 3. I follow the instructions/ docs to the letter, go to https://###.com/admin and am greeted with the above can't connect to database message.

So far, I have pretty much done everything to eliminate possibilities and the conclusion I have come to is that the .env file isn't being loaded. Obviously, I could well be wrong, but I tried a die Hello World on the db.php file and it worked. I put the database details directly into the db.php file and it worked, albeit took me to the current install rather than the upgrade page. I even created a new database (just to try it) and still got the same error.

The db file looks like this:

<?php
/**
 * Database Configuration
 *
 * All of your system's database connection settings go in here. You can see a
 * list of the available settings in vendor/craftcms/cms/src/config/DbConfig.php.
 *
 * @see craft\config\DbConfig
 */

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')
];

And the env file:

# 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="*************"

# 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="####"

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

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

# 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="craft"

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

Can anyone shed anymore light on how I can resolve this please? I am new to this so please be gentle and let me know if you need more info. Thanks.

12
  • Is this error on the command line, or on front-end?
    – dmatthams
    Sep 17 '19 at 9:08
  • Front-end. I'm not using Composer or anything if that helps.
    – deebie77
    Sep 17 '19 at 9:25
  • It's strange that it works directly from the db.php. What's your local server setup, is it MAMP or something else? It's a long shot (I don't think it's this) but have you tried 127.0.0.1 as your server setting?
    – dmatthams
    Sep 17 '19 at 9:30
  • I'm not using MAMP, I have uploaded it via FTP. I also tested it on another server (for a different website) and go the same message, but, again, when I put the details directly into the db.php it worked. I did try changing the server to 127.0.0.1 but got the same issue.
    – deebie77
    Sep 17 '19 at 9:36
  • Hopefully, someone else may be able to help, but I think my next step would be to try a fresh install of Craft 3 in the same environment to see if it's an issue with your install or the environment.
    – dmatthams
    Sep 17 '19 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.