Strapi shutting down and not able to restart

System Information
  • Strapi Version: 4.9.0
  • Operating System: Ubuntu 22.04.1 LTS (Jammy Jellyfish)
  • Database: mysql Ver 10.6.12-MariaDB
  • Node Version: 16.19.0
  • NPM Version: 9.2.0
  • Yarn Version: 1.22.19 (Using yarn, not npm)

Hello,

I have some problems with strapi shutting down for no obvious reasons.
For the context I deploy my work from my localhost to a live test server (see “System Information”), I never met those problems on my localhost running with a docker. This is happening for quite a while, I was in strapi 4.5 or 4.4 back then, and I had thoses problems in any versions, I update strapi when there’s a new release.

First problem :
Strapi randomly loses his connection with MySQL and decide to not respond to client request anymore, without killing himself. This is totally random, this can happen after 48 hours as well as after 20 minutes of uptime. It’s always a mysql timeout.

[2023-03-27 22:44:37.814] debug: ⛔️ Server wasn't able to start properly.
[2023-03-27 22:44:37.814] error: select `t0`.`id` from `admin_users` as `t0` limit 1 - read ETIMEDOUT
Error: read ETIMEDOUT
    at TCP.onStreamRead (node:internal/stream_base_commons:217:20)
    at TCP.callbackTrampoline (node:internal/async_hooks:130:17)
    --------------------
    at Protocol._enqueue (/datas/www/strapi-project/releases/20230324135501/node_modules/mysql/lib/protocol/Protocol.js:144:48)
    at Connection.query (/datas/www/strapi-project/releases/20230324135501/node_modules/mysql/lib/Connection.js:198:25)
    at /datas/www/strapi-project/releases/20230324135501/node_modules/knex/lib/dialects/mysql/index.js:132:18
    at new Promise (<anonymous>)
    at Client_MySQL._query (/datas/www/strapi-project/releases/20230324135501/node_modules/knex/lib/dialects/mysql/index.js:126:12)
    at executeQuery (/datas/www/strapi-project/releases/20230324135501/node_modules/knex/lib/execution/internal/query-executioner.js:37:17)
    at Client_MySQL.query (/datas/www/strapi-project/releases/20230324135501/node_modules/knex/lib/client.js:146:12)
    at Runner.query (/datas/www/strapi-project/releases/20230324135501/node_modules/knex/lib/execution/runner.js:123:36)
    at ensureConnectionCallback (/datas/www/strapi-project/releases/20230324135501/node_modules/knex/lib/execution/internal/ensure-connection-callback.js:13:17)
    at Runner.ensureConnection (/datas/www/strapi-project/releases/20230324135501/node_modules/knex/lib/execution/runner.js:300:20)
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Second problem :
Happens when I merge new work on the server from my localhost. When I do, I kill strapi process then yarn installyarn buildyarn develop. When trying to start, strapi tries to edit a table/field on MySQL, but this modification seems to already have been done, it results in a MySQL error and strapi is shutting down. It’s always an error about the key does not exist, or the key already exist. The table and fields concerned varies.

[2023-03-22 17:03:18.747] debug: ⛔️ Server wasn't able to start properly.
[2023-03-22 17:03:18.750] error: alter table `files` add constraint `files_created_by_id_fk` foreign key (`created_by_id`) references `admin_users` (`id`) on delete SET NULL - ER_FK_DUP_NAME: Duplicate FOREIGN KEY constraint name 'strapi_project@002dstrapi/files_created_by_id_fk'
Error: ER_FK_DUP_NAME: Duplicate FOREIGN KEY constraint name 'strapi_project@002dstrapi/files_created_by_id_fk'
    at Query.Sequence._packetToError (/datas/www/strapi-project/releases/20230322160245/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)
    at Query.ErrorPacket (/datas/www/strapi-project/releases/20230322160245/node_modules/mysql/lib/protocol/sequences/Query.js:79:18)
    at Protocol._parsePacket (/datas/www/strapi-project/releases/20230322160245/node_modules/mysql/lib/protocol/Protocol.js:291:23)
    at Parser._parsePacket (/datas/www/strapi-project/releases/20230322160245/node_modules/mysql/lib/protocol/Parser.js:433:10)
    at Parser.write (/datas/www/strapi-project/releases/20230322160245/node_modules/mysql/lib/protocol/Parser.js:43:10)
    at Protocol.write (/datas/www/strapi-project/releases/20230322160245/node_modules/mysql/lib/protocol/Protocol.js:38:16)
    at Socket.<anonymous> (/datas/www/strapi-project/releases/20230322160245/node_modules/mysql/lib/Connection.js:88:28)
    at Socket.<anonymous> (/datas/www/strapi-project/releases/20230322160245/node_modules/mysql/lib/Connection.js:526:10)
    at Socket.emit (node:events:513:28)
    at Socket.emit (node:domain:489:12)

Both problems may be symptoms of the same origin.

Hi dear,
I just see your problem and i have exactly the same first problem, did u got any answer or solved your problem ?
Thanks in advance

The (production?) server is started in development mode. This is problematic as it is doing a lot of overhead processing (looking for file changes), but mainly it’s allowing you to make code changes by configuring content types etc.

When you modify content types, you’re also modifying code. Those code changes are replaced when you upload new code and if one element is out of place it could cause Strapi to reset entirely.

In short, development mode is not designed to run for days and it certainly shouldn’t be used in production.

Change yarn develop to yarn start. See Deployment | Strapi Documentation more details.

Thank you for the late answer. The yarn start did nothing different.

But I finally found the solution with a lot of digging. It was because of MariaDB which kills any idle connections.
So must set min to 0 in the database config in Strapi like this :

config/database.js

module.exports = ({ env }) => ({
  connection: {
    client: 'mysql',
    connection: {
      ...
      min: 0,
      ...
    },
  },
});