Using the MariaDB (MySQL) Backend

しぐれ edited on Mar 15, 2025.

Warning

⚠️ 💩 ⚠️
Although MySQL database works fine, be aware that our builds are based upon MariaDB client libraries since that is what Debian provides.
⚠️ 💩 ⚠️

To use the MariaDB (MySQL) backend, you can either use the official Docker image or build your own binary with MySQL enabled.

To run the binary or container, ensure the DATABASE_URL environment variable is set (i.e. DATABASE_URL='mysql://<user>:<password>@mysql/vaultwarden[?ssl_mode=(disabled|required|preferred)&ssl_ca=/path/to/cart.(crt|pem)]').

Connection String Syntax:

DATABASE_URL=mysql://[[user]:[password]@]host[:port][/database]

If your password contains special characters, you will need to use percentage encoding.

! # $ % & ' ( ) * + , / : ; = ? @ [ ]
%21 %23 %24 %25 %26 %27 %28 %29 %2A %2B %2C %2F %3A %3B %3D %3F %40 %5B %5D

A complete list of codes can be found on Wikipedia page for percent encoding

Example using Docker

# Start a mysql container docker run --name mysql --net \ -e MYSQL_ROOT_PASSWORD=\ -e MYSQL_DATABASE=vaultwarden\ -e MYSQL_USER=\ -e MYSQL_PASSWORD= -d mysql:5.7 # Start vaultwarden with MySQL Env Vars set. docker run -d --name vaultwarden --net \ -v $(pwd)/vw-data/:/data/ -v :/ssl/\ -p 443:80 -e ROCKET_TLS='{certs="/ssl/",key="/ssl/"}'\ -e RUST_BACKTRACE=1 -e DATABASE_URL='mysql://:@mysql/vaultwarden'\ -e ADMIN_TOKEN=\ -e ENABLE_DB_WAL='false'

Example using Non-Docker MySQL Server

Server IP/Port 192.168.1.10:3306 UN: dbuser / PW: yourpassword / DB: vaultwarden mysql://dbuser:yourpassword@192.168.1.10:3306/vaultwarden

Example using docker-compose

services: vaultwarden-db: image: "mariadb" # or "mysql" container_name: "vaultwarden-db" restart: always env_file: - ".env" volumes: - "vaultwarden-db_vol:/var/lib/mysql" - "/etc/localtime:/etc/localtime:ro" environment: - "MYSQL_ROOT_PASSWORD=" - "MYSQL_PASSWORD=" - "MYSQL_DATABASE=vaultwarden" - "MYSQL_USER=" healthcheck: test: mariadb-admin ping -h 127.0.0.1 -u $$MYSQL_USER --password=$$MYSQL_PASSWORD start_period: 5s interval: 5s timeout: 5s retries: 55 vaultwarden: image: "vaultwarden/server:latest" container_name: "vaultwarden" hostname: "vaultwarden" depends_on: vaultwarden-db: condition: service_healthy restart: always env_file: - ".env" volumes: - "vaultwarden_vol:/data/" environment: - DATABASE_URL=mysql://:${VAULTWARDEN_MYSQL_PASSWORD}@vaultwarden-db/vaultwarden - ADMIN_TOKEN= # https://github.com/dani-garcia/vaultwarden/wiki/Enabling-admin-page - RUST_BACKTRACE=1 ports: - "80:80" volumes: vaultwarden_vol: vaultwarden-db_vol:

Manually create a database (For example, using an existing database server)

Warning
To execute these queries you need a user which has the privileges to create new databases and users.
Most of the time that would be the root user, but it could be different for your database.
Using the docker-compose example above makes these steps unnecessary. Database, collation and charset is created automatically at startup.

Create database and user

  1. Create an new (empty) database for vaultwarden (Ensure the Charset and Collate are correct!):
    CREATE DATABASE vaultwarden CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
  2. Create a new database user and grant rights to database (MariaDB, MySQL):
    CREATE USER 'vaultwarden'@'localhost' IDENTIFIED BY 'yourpassword'; GRANT ALL ON `vaultwarden`.* TO 'vaultwarden'@'localhost'; FLUSH PRIVILEGES;

You might want to try a restricted set of grants:

GRANT ALTER, CREATE, DELETE, DROP, INDEX, INSERT, REFERENCES, SELECT, UPDATE ON `vaultwarden`.* TO 'vaultwarden'@'localhost'; FLUSH PRIVILEGES;

Migrating from SQLite to MySQL

An easy way of migrating from SQLite to MySQL has been described in this issue comment. The steps are repeated below. Please, note that you are using this at your own risk and you are strongly advised to backup your installation and data!

  1. First follow the steps 1 and 2 above
  2. Configure vaultwarden and start it, so diesel can run migrations and set up the schema properly. Do not do anything else.
  3. Stop vaultwarden.
  4. Dump your existing SQLite database using the following command. Double check the name of your sqlite database, default should be db.sqlite.
    Note: You need the sqlite3 command installed on your Linux system.
    We need to remove some queries from the output of the sqlite dump like create table etc.. we will do that here.

    You either can use this one-liner:
    sqlite3 db.sqlite3 .dump | grep "^INSERT INTO" | grep -v "__diesel_schema_migrations" > sqlitedump.sql ; echo -ne "SET FOREIGN_KEY_CHECKS=0;\n$(cat sqlitedump.sql)" > mysqldump.sql
    Or the following right after each other:
    sqlite3 db.sqlite3 .dump | grep "^INSERT INTO" | grep -v "__diesel_schema_migrations" > sqlitedump.sql echo "SET FOREIGN_KEY_CHECKS=0;" > mysqldump.sql cat sqlitedump.sql >> mysqldump.sql
  5. Load your MySQL dump:
    mysql --force --password --user=vaultwarden --database=vaultwarden < mysqldump.sql
  6. Start vaultwarden again.

Note: Loading your MySQL dump with --show-warnings will highlight that the datetime fields are getting truncated during the import which seems to be okay.

Note (Code 1265): Data truncated for column 'created_at' at row 1 Note (Code 1265): Data truncated for column 'updated_at' at row 1

Note 1:Then error loading data mysqldump.sql Load error

error (1064): Syntax error near '"users" VALUES('9b5c2d13-8c4f-47e9-bd94-f0d7036ff581'*********)
fix:
sed -i 's#\"#\#g' mysqldump.sql
mysql --password --user=vaultwarden use vaultwarden source /vw-data/mysqldump.sql exit

Note 2: If MariaDB might complain about mismatched value counts if SQLite database is migrated from a prior, older version, e.g.:

ERROR 1136 (21S01) at line ###: Column count doesn't match value count at row 1
The version jump may have added new database columns. Upgrade vaultwarden using the SQLite backend first to run migrations on the SQLite database, switch to the MariaDB backend, then repeat migration steps above. Alternatively, look for the commits adding migrations since the version you had installed and run migrations manually using sqlite3