SERVER
Server Setup

How to Set Up a FiveM Database with MySQL (oxmysql)

September 2, 2025 · 4 min read

Setting up a database is crucial for any FiveM server, especially if you’re running a framework like ESX or QBCore which relies on persistent data. In this guide, we’ll explore how to set up a FiveM database with MySQL (oxmysql) effectively, ensuring your server runs smoothly with real-time data storage. Let’s get started!

Prerequisites for Setting Up MySQL Database

Before diving into the setup process, ensure you have the following prerequisites installed:

  1. MySQL Server: You can use MySQL Workbench or phpMyAdmin to manage your databases easily.
  2. oxmysql Resource: This is a highly optimized MySQL library for FiveM, allowing asynchronous database operations.
  3. Basic Knowledge of SQL: Familiarity with SQL commands will help manage your database effectively.

Step-by-Step Guide to Set Up MySQL with oxmysql

Step 1: Install MySQL Server

  1. Download and install MySQL from the official MySQL website.
  2. During installation, remember the root password as you will need it later.
  3. Once installed, you can manage your databases using MySQL Workbench or any SQL client.

Step 2: Create a New Database

  1. Open your SQL client (like MySQL Workbench).
  2. Execute the following SQL command to create a new database:
    CREATE DATABASE fivem_db;
    
  3. Make sure to replace fivem_db with a name of your choice, preferably something relevant to your server.

Step 3: Set Up the oxmysql Resource

  1. Navigate to your FiveM server resources folder.
  2. Clone or download the oxmysql resource from its GitHub repository.
  3. Extract the downloaded file and place the oxmysql folder in your resources directory.
  4. In your server.cfg, add the following line to enable oxmysql:
    start oxmysql
    

Step 4: Configure the Database Connection

  1. Inside the oxmysql folder, locate config.lua.
  2. Open it and fill in your database connection details as follows:
    local config = {
        host = "127.0.0.1",
        user = "root",
        password = "YOUR_PASSWORD",
        database = "fivem_db",
        port = 3306,
    }
    
  3. Replace YOUR_PASSWORD with your actual MySQL root password and ensure the database name matches the one you created earlier.

Step 5: Create Necessary Tables

Now that your database is set up, it’s time to create tables that your resources will need. For ESX, a basic users table can be created:

CREATE TABLE IF NOT EXISTS users (
   id INT NOT NULL AUTO_INCREMENT,
   identifier VARCHAR(64) NOT NULL UNIQUE,
   name VARCHAR(50) NOT NULL,
   money INT DEFAULT 0,
   PRIMARY KEY (id)
);

Adjust the table structure based on your specific needs and the framework you are using.

Step 6: Test the Database Connection

  1. Start your FiveM server.
  2. Check the server logs to see if there are any errors related to the database connection. Look specifically for logs related to oxmysql.
  3. You can run commands in your server console to test database interactions:
    mysql:execute('SELECT * FROM users WHERE id = ?', {1})
    

Troubleshooting Common Issues

Even seasoned server owners may run into issues. Here’s how to troubleshoot some common problems:

  • MySQL Connection Errors: Make sure your MySQL server is running and accessible from your FiveM server.
  • Script Errors: Ensure your scripts are compatible with the oxmysql library. Consult the documentation for each specific script.
  • Permissions Issues: If a script cannot access the database, check the MySQL user permissions and ensure it has rights to the database.

Best Practices for Database Management

  • Regular Backups: Always back up your database regularly to prevent loss of data.
  • Optimize Queries: Use indexing and optimized queries to enhance performance.
  • Monitor Performance: Use MySQL performance monitoring tools to keep an eye on database performance and optimize as necessary.

Additional Resources

For further customization and scripting, check out our scripts collection or explore MLO maps that can complement your database functionalities.

Frequently Asked Questions

What is oxmysql?

oxmysql is an asynchronous MySQL library for FiveM that improves performance by handling database interactions efficiently.

How do I know if my database is set up correctly?

You can test the connection by running commands in your server console to fetch data from your database.

Can I use another database management system?

Yes, while this guide focuses on MySQL, you can also use alternatives like SQLite, but it may require different configurations.

Is it necessary to have MySQL for my FiveM server?

While not strictly necessary, using a database helps manage player data persistently across server restarts, especially for roleplay servers.

How can I add more tables for my scripts?

You can create new tables using SQL commands and ensure your scripts are configured to interact with them as necessary.

#fivem#mysql#oxmysql#server setup#tutorial

Keep reading