How to Set Up a Database for Your FiveM Server ​
Many FiveM scripts and frameworks store their data in a MySQL database. For your server to access it, you need two things: a database including its credentials, and a database resource that establishes the connection.
We recommend oxmysql. It is actively maintained and replaces the outdated resources mysql-async and ghmattimysql, whose functionality it also provides.
Create the database ​
Create a database
In the dashboard, go to Databases and create a new database using the plus icon. You can find the detailed instructions under Create Database.Read the credentials
Click the eye icon next to your database. You will need these values in a moment:- Database Host (e.g.
db1.cgn1.emeraldhost.de) - Database Port (default:
3306) - Database Username
- Database Password
- Database Name
- Database Host (e.g.
Important
Never share your database credentials with anyone, and never post them in screenshots, console logs or third-party support tickets. Anyone with these credentials can read and delete all data on your server.
Upload oxmysql ​
Download the resource
Download the latest release of oxmysql. Use theoxmysql.zipfile and not the source code download.Extract the archive
Extract the archive. You will get a folder namedoxmysql.Warning
The folder name must be exactly
oxmysql. If you rename it, FiveM will not find the resource.Stop the server
Stop your server via the dashboard.Connect via SFTP
Connect to your server via SFTP.Upload the folder
Upload theoxmysqlfolder into theresourcesfolder located next to your server'sserver.cfg.Setup Path Default (recommended) /home/container/resourcestxAdmin Recipe / Deployer /home/container/txData/<ProfileName>/resources
Enter the connection string ​
The connection string bundles all credentials in a single line. oxmysql reads it from the convar mysql_connection_string.
Open server.cfg
Open theserver.cfgyour server uses. Which one that is depends on your txAdmin setup – you can find the overview under Set Port.Set the connection string
Add the following line and replace the placeholders with your credentials from the eye icon:set mysql_connection_string "mysql://USERNAME:PASSWORD@HOST:PORT/DATABASENAME"Alternatively, oxmysql also accepts the semicolon-separated format:
set mysql_connection_string "user=USERNAME;password=PASSWORD;host=HOST;port=PORT;database=DATABASENAME"Only use set
Always define the convar withset.Important
Never use
setrorsetsfor the connection string.setrsends the value to all connected players, andsetsdisplays it publicly in the server list and in your server'sinfo.json. In both cases your database password would be visible to everyone.Start oxmysql
Below the connection string, add the start line for the resource:ensure oxmysqlWarning
The order in the
server.cfgmatters. Theset mysql_connection_stringmust be placed before allensurelines, andensure oxmysqlmust be placed before any script that uses the database. Otherwise those scripts start before the database connection is ready.Start the server
Save theserver.cfgand start your server via the dashboard.
Note
On every server start, the dashboard automatically rewrites some lines of the server.cfg, among them endpoint_add_tcp, endpoint_add_udp, sv_hostname, set sv_licenseKey, set steam_webApiKey and sv_maxclients. Your set mysql_connection_string and ensure oxmysql lines are not affected and will not be lost.
Verify the connection ​
After the start, open the console of your server in the dashboard. If the connection succeeded, you will find this message:
Database server connection established!It is preceded by the version of your database server in square brackets.
If the connection fails, oxmysql prints a message in this form instead:
Unable to establish a connection to the database (ERRORCODE)!In that case, double-check the username, password, host, port and database name via the eye icon in the dashboard.
Special characters in the password ​
Depending on the format you use, certain special characters are reserved or unsupported. If your database password contains one of these characters, the connection may fail:
; , / ? : @ & = + $ #Tip
If your generated password contains one of these characters, use Generate New Password in the dashboard until you get a password without them. Alternatively, you can switch to the other connection string format.
Warning
Do not encode the password (for example %40 instead of @). oxmysql does not decode such encodings and passes them to the database unchanged, so the login will fail as well.
Additional convars ​
For troubleshooting, oxmysql offers additional convars that you also add to the server.cfg using set:
| Convar | Description |
|---|---|
set mysql_debug true | Prints all database queries in the console |
set mysql_slow_query_warning 150 | Warns if a query takes longer than the given time in milliseconds |
Tip
Create a Backup of your server before making larger changes to your database. The content of the database itself is backed up via phpMyAdmin.
What comes next? ​
Your database is connected now, but still empty. Most frameworks and scripts ship with a .sql file that creates the required tables. How to import it is explained under Import Database.