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=truein 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"
b) oxmysql (recommended today)
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
3.1. Recommended Loading Order
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
- Connection should initialize without errors at startup.
- 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)
- Ensure encoding is set to
utf8mb4.
6) Performance & Robustness
- Use
utf8mb4everywhere (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_timeoutandmax_allowed_packet. - Run migrations before starting the server.
7) Troubleshooting (Checklist)
| Symptom | Likely Cause | Fix |
|---|---|---|
| ER_ACCESS_DENIED_ERROR | Wrong user/pass, host not allowed | Recreate user with correct IP, check password |
| connect ETIMEDOUT | Firewall/port blocked | Open 3306 between hosts |
| Unknown database | Database not created | Create DB and verify name |
| MySQL server has gone away | Timeouts or large packets | Adjust wait_timeout and max_allowed_packet |
| Garbled characters | Wrong charset | Force utf8mb4 |
| Crash on first query | Resource not loaded | ensure oxmysql before scripts |
Error with = in password | Parsing issue | Encode 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.


















