.env not used while deploying

Hey all, when i’m deploying my strapi-server, my .env settings from the Envoirenment file are not used. What can I do? (PM2 Logs shows "database login failed for user “strapi”, but i changed it…)

Thx in advance!

Hey @Tobi, welcome to the Cleavr forum!

For a nodejs app, it can be that if you’re using the dotenv package, it needs to be configured to read the .env file. javascript - dotenv file is not loading environment variables - Stack Overflow may give some hints.

If it’s just a variable or two, an alternate method is in web app > settings > build, and then add the env variables to the env section of PM2 Ecosystem. This way PM2 will set the env variables and should be available to the app.

Thx! I tried the way with the PM2 env setting, that worked, but nevertheless i get the error:

[2021-11-04T21:00:20.300Z] debug :no_entry: Server wasn’t able to start properly.
[2021-11-04T21:00:20.301Z] error error: password authentication failed for user “my-admin”

Any idea? I have added a Postgres 13 database and added to the PM2 env the name, username and password.

Are you able to access the database in other ways to verify that db username and password credentials are correct? Such as via SSH into server and use command line (instructions - Postgres login: How to log into a Postgresql database | alvinalexander.com) or to connect via a db client such as TablePlus?

Not really,now added MySql, same problems… hmpf

[2021-11-04T21:33:50.496Z] debug :no_entry: Server wasn’t able to start properly.
[2021-11-04T21:33:50.496Z] error Error: ER_ACCESS_DENIED_ERROR: Access denied for user ‘my-admin’@‘localhost’ (using password: YES)

dont know what i can do


So in the end i just want get a running Strapi CMS , MySql/Postgres doesn’t matter.

What does the database.js file look like?

It should look like:

module.exports = ({ env }) => ({
  defaultConnection: 'default',
  connections: {
    default: {
      connector: 'bookshelf',
      settings: {
        client: 'mysql',
        host: env('DATABASE_HOST', 'localhost'),
        port: env.int('DATABASE_PORT', 3306),
        database: env('DATABASE_NAME', 'strapi'),
        username: env('DATABASE_USERNAME', 'admin'),
        password: env('DATABASE_PASSWORD', 'password'),
      },
      options: {},
    },
  },
});

Exactly:

module.exports = ({ env }) => ({
  defaultConnection: "default",
  connections: {
    default: {
      connector: "bookshelf",
      settings: {
        client: "mysql",
        host: env("DATABASE_HOST", "localhost"),
        port: env.int("DATABASE_PORT", 3306),
        database: env("DATABASE_NAME", "strapi"),
        username: env("DATABASE_USERNAME", "strapi"),
        password: env("DATABASE_PASSWORD", "strapi"),
      },
      options: {},
    },
  },
});

PM2:

{
  "name": "jhj4hw1xbpiafkck2759.cleaver.rocks",
  "script": "npm",
  "args": "start",
  "log_type": "json",
  "cwd": "/home/cleavr/jhj4hw1xbpiafkck2759.cleaver.rocks/current",
  "instances": "max",
  "exec_mode": "cluster_mode",
  "env": {
    "DATABASE_NAME": "dbname",
    "DATABASE_USERNAME": "adminuser",
    "DATABASE_PASSWORD": "adminpw",
    "PORT": 9873,
    "CI": 1,
    "NUXT_TELEMETRY_DISABLED": 1
  }
}

It must be something with the database. Formatting the environment variables that way I’d suspect it should read from the .env file.

Is the target db on the same server or a different server?

If on same, make sure to use localhost. If on a different server, make sure the remote db server allows for remote connections.

Same server and localhodt is set

Ok, great!

I took a peak at your setup, I’m pretty sure it’s env related. If you are using the latest Strapi version (v3.6.8), then there should be no problem using the .env method. Let’s backtrack a bit and I’ll use one of my test repo’s as an example.

Make sure local runs with no errors

On your local, run npm start and make sure Strapi runs with no issues - such as if you switched DB types, it may ask you to install mysql / pg driver

If it runs and tweaks were made, push updates to repo.

GitHub Repo / MySQL connector

Using my repo as an example, GitHub - armgitaar/strapi-mysql, I have the following for my mysql config:


module.exports = ({ env }) => ({
  defaultConnection: 'default',
  connections: {
    default: {
      connector: 'bookshelf',
      settings: {
        client: 'mysql',
        host: env('DATABASE_HOST', 'localhost'),
        port: env.int('DATABASE_PORT', 3306),
        database: env('DATABASE_NAME', 'strapi'),
        username: env('DATABASE_USERNAME', 'root'),
        password: env('DATABASE_PASSWORD', 'password'),
      },
      options: {},
    },
  },
});

Cleavr web > build config

For Strapi, I have the following build settings:

Notice npm and start are used for entry point / arguments and also build as artifacts path setting. Also, no additional env variables added to the PM2 Ecosystem.

Environment configs

I added a MySQL 8.0 database on my server that I’m adding Strapi to and then added the connection variables to the web app > environment section:

NODE_ENV=production
DATABASE_NAME=myspace
DATABASE_USERNAME=facebook
DATABASE_PASSWORD=meta

Deploy

From here, I can deploy successfully and log into the admin section.

Also, here are my installed services if that helps:

CleanShot 2021-11-04 at 20.28.38

The main thing to note there is that Strapi is compatible only with even versions of NodeJS. Though, you probably wouldn’t get past the install npm packages step during deployment if it was an incompatible node version.


I hope that helps! I’m sure it’s a db connection issue per the original error, hopefully running through the db connections / configs bubbles up the issue.

1 Like

after trying and trying - reinstallation of a fresh server etc. i figured out, that one problem is the database password given with cleavr. There are some character which are not valid for a password for an sql user - cleavr should use a validation here.