When running many tests involving the database one might encounter the following issue:

Illuminate\Database\QueryException: SQLSTATE[HY000] [2006] MySQL server has gone away

The issue occurred reliably and always at the same test. Temporarily deleting test classes just made another test triggering the issue.

When debugging locally it turns out that the maximum connection limit of MariaDB is the root cause. By default MariaDB comes with a connection limit of 100 connections. Laravel however does not use persistent connections by default and therefore consumes a lot of connections when running tests.

One might use a custom my.cnf. Another workaround might be the following:

before_script:
  - echo "SET GLOBAL max_connections=1000;" | mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mysql