docs: MM_CONFIG is ignored when storing the configuration in the database

Summary

MM_CONFIG is used to connect to the database initially. Mattermost then reads SqlSettings.Datasource from the configurations table and ignores MM_CONFIG from that point forward. That means two separate configurations need to be manually kept in sync when rotating passwords.

Steps to reproduce

Using Mattermost version 8.0.1 9.4.1:

  1. Ensure that you’ve pointed to the configuration from the database, not the config.json.
  2. Validate that MM_CONFIG and SqlSettings.Datasource are identical
  3. Change the password for the database user, and adjust MM_CONFIG accordingly.
  4. Restart Mattermost
  5. Observe that the logs indicate that connecting to the database failed.
  6. Manually adjust SqlSettings.Datasource in the configurations table to the new password.
  7. Restart Mattermost
  8. Observe, that Mattermost starts up fine now.

Expected behavior

There are two alternatives that feel appropriate in this situation:

  1. When MM_CONFIG is set, SqlSettings.Datasource should always be ignored.
  2. It should be possible to reset the field SqlSettings.Datasource without it being immediately filled from MM_CONFIG again: running the command mmctl config reset SqlSettings.DataSource claims that Value/s reset successfully but reading the value immediately after, shows that it has immediately returned

Observed behavior (that appears unintentional)

MM_CONFIG is only used to connect to the database to read the actual database configurations. Then a new connection is started with the connection string from the database it has already connected to.

Possible fixes

Not sure.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16 (7 by maintainers)

Commits related to this issue

Most upvoted comments

I think we have never explicitly mention that the configuration database and Mattermost application database are two different things. So that means you can use different databases for those, you can even use postgres on one and mysql on the other.

Quoting from @aboukhal

There are two alternatives that feel appropriate in this situation:
1. When MM_CONFIG is set, SqlSettings.Datasource should always be ignored. 2. It should be possible to reset the field SqlSettings.Datasource without it being immediately filled from MM_CONFIG again: running the command mmctl config reset SqlSettings.DataSource claims that Value/s reset successfully but reading the value immediately after, shows that it has immediately returned

  1. The explanation above is the reason that field has to be explicitly set. Maybe we can document this;, it is a requirement that SqlSettings.Datasource should be updated first (considering that updating this value is much easier when application is running), then changing the MM_CONFIG to the new DSN.
  2. If application is not working, due to not being able to connect to the DB, the APIs won’t work hence it won’t be able to use any mmctl command remotely. mmctl config reset would just set everything to default value anyway, so it wouldn’t make sense to go with that command.

The alternative route can be explicitly setting MM_SQLSETTINGS_DATASOURCE environment variable to override what has been defined in the configuration (whether it’s in a DB, or in a file). This would allow to pass correct DSN to the application.

I think this explains more or less what happened to @corang.

The improvement here would be explicitly documenting this. I’m not sure whether this designed to be work on two different databases but it can be useful time to time. cc @agnivade @cwarnermm

This issue introduced a significant problem for us during a database migration. Our aim was to transition to a new DB server.

Initially, we provisioned a new database on a fresh server and copied the MM DB there. Subsequently, we updated the MM_CONFIG environment variable (in the Helm chart) to utilize the new connection string. As a check, we hadn’t yet configured the firewall for the new DB. As expected, MM couldn’t start due to the inability to connect to the new DB. After fixing the firewall, MM successfully started, indicating that everything was in order.

However, upon startup, MM retrieved the old credentials from the new DB and established a connection to the old database server. Since we kept the old DB instance running for a few days (call it a good habit…), we didn’t immediately notice that MM wasn’t writing to the new DB until we decommissioned the old DB several days later.

Fortunately, we could restore from a recent backup.

@isacikgoz good point, I will test MM_SQLSETTINGS_DATASOURCE. If that works, I’ll be happy.

@amyblais Saw the note that it wasn’t clear how to reproduce, so I’ll sum up a quick repro here:

  1. Create postgres RDS instance
  2. Connect MM to rds instance using MM_CONFIG env var for db connection string
  3. Put some data in MM
  4. Shut down mattermost
  5. Take snapshot of RDS instance
  6. Delete RDS instance
  7. Create new RDS instance from snapshot
  8. Reconfigure MM_CONFIG env var to point to new RDS instance
  9. Start MM - errors with can’t connect to DB

Our current workaround is to manually edit the configurations table by replacing all old db connection strings with the new/correct one when restoring from a snapshot.

Yes, we will upgrade after the holidays.