All Posts
June 28, 2026ModForge Team

How to Set Up a MySQL Database for Your FiveM Server

Every serious FiveM server needs a MySQL database. This guide covers installing MySQL, creating your database, connecting it via oxmysql, and keeping it healthy long-term.

Almost every FiveM framework and the vast majority of scripts require a MySQL database to store persistent data — player characters, job assignments, inventory contents, vehicle ownership, bank balances, and much more. Setting up your database correctly from the start is one of the most important things you will do for your server. This guide covers everything from installation to long-term maintenance.

Why MySQL?

FiveM server data needs to survive between play sessions. When a player logs off and returns the next day, the server must remember their character, their money, their vehicles, and every item in their inventory. MySQL is the relational database system the FiveM community has standardized on for this purpose.

FiveM connects to MySQL through a connector resource called oxmysql, which bridges the game server and the database. You configure the database once, and every script that needs persistent storage uses it through this single connection.


Option 1: Install MySQL Directly on Your VPS (Recommended)

Installing MySQL on the same machine as your FiveM server is the standard approach for most servers. It is free, has negligible latency since everything is local, and gives you full control.

On Ubuntu or Debian Linux:

sudo apt update
sudo apt install mysql-server -y
sudo mysql_secure_installation

Follow the mysql_secure_installation prompts carefully:

  • Set a strong root password
  • Remove anonymous users — yes
  • Disallow root login remotely — yes
  • Remove test database — yes
  • Reload privilege tables — yes

On Windows Server:

  1. Download MySQL Community Server from dev.mysql.com/downloads/mysql/
  2. Run the installer and select Server only
  3. Set a root password during the configuration wizard
  4. MySQL will install as a Windows service that starts automatically

Option 2: Use a Managed Remote Database

Some hosting providers offer managed MySQL databases as a separate service. This is easier to set up and requires no server administration, but adds a small latency overhead (typically 1–5ms) and a monthly fee. Suitable for operators who do not want to manage MySQL themselves.

If using a remote database, your provider gives you a hostname, username, password, and database name. Use these in your oxmysql connection string.


Step 1: Create a Dedicated Database and User

Never use the root account for your FiveM server in production. Create a dedicated user with permissions limited to only your FiveM database. This limits damage in the unlikely event of a SQL injection vulnerability in a script.

Log in to MySQL:

mysql -u root -p

Create the database:

CREATE DATABASE fivem_server
  CHARACTER SET utf8mb4
  COLLATE utf8mb4_unicode_ci;

Create a dedicated user and grant permissions:

CREATE USER 'fivem'@'localhost' IDENTIFIED BY 'use_a_strong_password_here';
GRANT ALL PRIVILEGES ON fivem_server.* TO 'fivem'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 2: Install HeidiSQL for Visual Database Management

The MySQL command line works but HeidiSQL gives you a visual interface that makes everything easier — especially when importing SQL files, browsing tables, and debugging data issues.

  1. Download HeidiSQL from heidisql.com (Windows) or use TablePlus on Mac
  2. Install and create a new connection:
    • Hostname: 127.0.0.1 (or your server IP if remote)
    • User: fivem
    • Password: your password
    • Database: fivem_server
  3. Connect and confirm you can see the fivem_server database

Step 3: Install oxmysql

oxmysql is the FiveM database connector resource. It is significantly faster than the older mysql-async library and is the current community standard. Download the latest release:

# Download from GitHub releases and extract to:
server-data/resources/[ox]/oxmysql/

Or clone it:

git clone https://github.com/overextended/oxmysql resources/[ox]/oxmysql

Step 4: Configure the Connection String

Add the connection string to server.cfg before any resources that use the database:

set oxmysql_connection_string "mysql://fivem:your_strong_password@localhost/fivem_server?charset=utf8mb4"

ensure oxmysql

Connection string format:

mysql://USERNAME:PASSWORD@HOST/DATABASE?charset=utf8mb4

Important: If your password contains special characters like @, #, !, or %, you must URL-encode them. For example:

  • @ becomes %40
  • # becomes %23
  • ! becomes %21

Step 5: Test the Database Connection

Start your server and look for this line in the console output:

[oxmysql] Database server connection established.

If you see a connection error instead, check these things in order:

  1. Username and password — are they correct in the connection string?
  2. Database name — is it spelled exactly right? Linux is case-sensitive.
  3. MySQL is running — check with sudo systemctl status mysql on Linux
  4. Firewall — is port 3306 blocked? If using a remote database, the firewall must allow the FiveM server's IP to connect

Step 6: Import Your Framework SQL Files

Once connected, import your framework's SQL file to create the required tables:

QBCore:

mysql -u fivem -p fivem_server < resources/[qb]/qb-core/qb-core.sql

ESX:

mysql -u fivem -p fivem_server < resources/[esx]/es_extended/es_extended.sql

Or use HeidiSQL: File → Run SQL File → select the .sql file. After importing, verify the tables were created by expanding the database in the left panel.


Long-Term Database Maintenance

Automated Backups

Schedule a daily database backup so you can recover from data loss. On Linux, add this cron job:

# Edit cron: crontab -e
0 3 * * * mysqldump -u fivem -pfivem_server fivem_server > /backups/fivem_$(date +%Y%m%d).sql

This runs at 3am every night and saves a dated backup file. Store backups off-server when possible.

Database Indexing

As your player count grows, add indexes to frequently-queried columns. The most important ones:

ALTER TABLE players ADD INDEX idx_identifier (identifier);
ALTER TABLE ox_inventory ADD INDEX idx_owner (owner);

Without indexes, queries slow down significantly as table row counts grow.

Periodic Cleanup

Old data accumulates over time. Periodically run maintenance queries like:

-- Remove bans older than 1 year that have been served
DELETE FROM bans WHERE expire < NOW() - INTERVAL 1 YEAR;

-- Remove vehicle records for players who have not logged in for 6 months
DELETE v FROM player_vehicles v
JOIN players p ON p.citizenid = v.citizenid
WHERE p.last_seen < NOW() - INTERVAL 6 MONTH;

With your database set up and healthy, you are ready to install frameworks and scripts that bring your server to life. Browse quality FiveM scripts at modforge.xyz.