How to convert CrowdSec Local API database from SQLite to MySQL or MariaDB.
The official CrowdSec documentation doesn’t provide instructions on data migration from SQLite to MySQL or MariaDB, and expects the user to re-register all machines and bouncers, which is inconvenient.
Let’s try a straightforward approach: dump the SQLite database into plain SQL and import it into MariaDB (we are running Ubuntu 24.04 Noble).
We need to create a database for CrowdSec first. We can follow the instructions from the documentation:
1 2 3 4 |
|
We will need mysql
and sqlite3
on the host where we have CrowdSec installed:
1
|
|
Check the location of the CrowdSec SQLite database file:
1
|
|
Let’s try the dump-pipe-import:
1 2 3 4 5 6 7 8 |
|
Unfortunately, SQLite and MySQL/MariaDB have incompatible syntax.
Let’s look for a solution. There is a Python tool, sqlite3-to-mysql
, which does exactly what we need: transfer data from SQLite3 to MySQL.
1 2 3 4 |
|
Now we can convert the data, but stop the CrowdSec first, so we don’t get inconsistent data.
1
|
|
Now convert the data:
1
|
|
The data conversion runs without errors, and we get the data into MariaDB.
Now we can update the /etc/crowdsec/config.yaml
to use MariaDB (See the documentation):
1 2 3 4 5 6 7 8 9 10 |
|
But when we try to start CrowdSec, it fails with a fatal error:
1 2 |
|
There seems to be a difference in CrowdSec database schemas between SQLite
and MySQL/MariaDB
. Let’s try to work around that.
First, recreate the database to clear all the imported data.
1 2 |
|
Start CrowdSec with systemctl start crowdsec
. It is going to fail, but it will create the correct database schema. (Backup your local_api_credentials.yaml
- it overwrote it with new credentials, but I can’t repeat this in a clean environment.)
Now we can try to convert the data again, but without creating the schema (option -K
) this time (it also uses a hardcoded path to the SQLite database):
1
|
|
Now we can start CrowdSec with systemctl start crowdsec
, and it works without any issues.