Logo

Connecting a FiveM Server to MySQL

Connecting a FiveM Server to MySQL

This article is a complete guide to connecting a FiveM server to a MySQL/MariaDB database.

1) MySQL Prerequisites (Database Server)

1.1. Create the Database and Minimal User

On your MySQL/MariaDB host:

-- 1) Create the database
CREATE DATABASE bdd_test CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- 2) Create a dedicated user (replace FIVEM_SERVER_IP with your server’s IP)
CREATE USER 'user'@'FIVEM_SERVER_IP' IDENTIFIED BY 'StrongPasswordHere';

-- 3) Minimal privileges (principle of least privilege)
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, INDEX, ALTER 
ON bdd_test.* TO 'user'@'FIVEM_SERVER_IP';

FLUSH PRIVILEGES;

Security Notes

  • One user per application (never use root!).
  • Restrict the host to the FiveM server’s IP, not %.
  • Use a strong password (≥ 16 characters, random).
  • Firewall: open port 3306 only between your FiveM server and MySQL.
  • TLS (recommended if traffic is outside LAN/VPN): enable SSL on MySQL and add ssl=true in oxmysql connection string.

1.2. Allow Remote Connections (if MySQL is not local)

Check bind-address in /etc/mysql/mysql.conf.d/mysqld.cnf (or equivalent).
To allow controlled remote access:

bind-address = 0.0.0.0

Restart MySQL:

systemctl restart mysql

1.3. Test Connectivity from the FiveM Server

mysql -h 89.213.175.25 -u user -p bdd_test -e "SELECT 1;"

2) Choose Your MySQL Resource for FiveM

There are two common connection string formats:

a) mysql-async (legacy/older)

Semicolon DSN format:

set mysql_connection_string "server=89.213.175.25;database=bdd_test;userid=user;password=password1"

URI format (more reliable and feature-rich):

set mysql_connection_string "mysql://user:[email protected]/bdd_test?charset=utf8mb4"

⚠️ About oxmysql and the = character
Avoid = in the password. If you cannot change it, encode it with URL encoding (= becomes %3D). Example:
mot=de=passe → mot%3Dde%3Dpasse

Other encodings:

  • @%40
  • :%3A
  • /%2F
  • ?%3F
  • &%26
  • #%23

3) Configuration in server.cfg

Place the connection string at the top of server.cfg, before resources that require it.

For mysql-async

# --- MySQL Base (mysql-async) ---
set mysql_connection_string "server=89.213.175.25;database=bdd_test;userid=user;password=password1;ssl=false"
# Optional: port (if not 3306)
# ;port=3307

# Load the resource
ensure mysql-async
# then your dependent scripts...

For oxmysql

# --- MySQL Base (oxmysql) ---
set mysql_connection_string "mysql://user:[email protected]/bdd_test?charset=utf8mb4"

# Useful convars:
setr mysql_debug false
setr mysql_log_level 2
setr mysql_slow_query_warning 200
# setr mysql_transaction_isolation_level 2

ensure oxmysql
# then your dependent scripts...

Tip: use set for secrets (connection string) and setr for non-sensitive variables.


4) Start / Restart Properly

After modifying server.cfg:

  • Restart the FiveM stack.
  • Watch logs at startup for SQL errors.

5) Health Checks

  1. Connection should initialize without errors at startup.
  2. Simple test (inside a test resource):
-- mysql-async (example)
MySQL.Async.fetchAll('SELECT 1 AS ok', {}, function(rows)
  print(('MySQL test: %s'):format(rows and rows[1] and rows[1].ok or 'fail'))
end)

-- oxmysql (example)
exports.oxmysql:fetch('SELECT 1 AS ok', {}, function(rows)
  print(('MySQL test: %s'):format(rows and rows[1] and rows[1].ok or 'fail'))
end)
  1. Ensure encoding is set to utf8mb4.

6) Performance & Robustness

  • Use utf8mb4 everywhere (DB/Table/Connection).
  • Index columns used in WHERE / JOIN.
  • Use transactions for atomic operations.
  • Monitor slow queries.
  • Avoid excessive synchronous queries; prefer async.
  • Adjust wait_timeout and max_allowed_packet.
  • Run migrations before starting the server.

7) Troubleshooting (Checklist)

SymptomLikely CauseFix
ER_ACCESS_DENIED_ERRORWrong user/pass, host not allowedRecreate user with correct IP, check password
connect ETIMEDOUTFirewall/port blockedOpen 3306 between hosts
Unknown databaseDatabase not createdCreate DB and verify name
MySQL server has gone awayTimeouts or large packetsAdjust wait_timeout and max_allowed_packet
Garbled charactersWrong charsetForce utf8mb4
Crash on first queryResource not loadedensure oxmysql before scripts
Error with = in passwordParsing issueEncode with %3D or change password

8) Ready-to-Use Examples

8.1. mysql-async

set mysql_connection_string "server=89.213.175.25;database=bdd_test;userid=user;password=password1;ssl=false"
ensure mysql-async
ensure your_resource

8.2. oxmysql

set mysql_connection_string "mysql://user:[email protected]/bdd_test?charset=utf8mb4"
setr mysql_debug false
setr mysql_log_level 2
setr mysql_slow_query_warning 200
ensure oxmysql
ensure your_resource

9) Operational Best Practices

  • Regular, tested backups.
  • Monitor MySQL metrics (CPU, InnoDB buffer pool, connections).
  • Rotate error logs.
  • Plan updates.
  • Separate dev / staging / production environments.

Join our Discord community server

For any questions, suggestions, or just to chat with the community, join us on Discord!

900+Members