GUIDES
Guides & Tutorials

How to Install oxmysql and Set Up the Connection String

October 22, 2024 · 4 min read

To optimize your FiveM server, integrating a reliable database solution is crucial, and oxmysql is one of the best options available. In this guide, we will explore how to install oxmysql and set up the connection string efficiently, ensuring your server can communicate seamlessly with your database.

What is oxmysql?

Before we dive into the installation process, let’s briefly discuss what oxmysql is. oxmysql is a MySQL library specifically designed for FiveM, providing high-performance and easy-to-use methods for database interactions. It supports prepared statements, connection pooling, and is compatible with both ESX and QBCore frameworks.

Prerequisites for Installation

Prior to installing oxmysql, ensure you have the following ready:

  • A working FiveM server.
  • MySQL server running (like MySQL or MariaDB).
  • Basic knowledge of your server's directory structure.
  • Access to your server's configuration files (server.cfg, fxmanifest.lua).

Step 1: Download oxmysql

  1. Visit the official oxmysql GitHub repository.
  2. Click on the Code button and select Download ZIP.
  3. Extract the downloaded ZIP file on your local machine.

Step 2: Move oxmysql to Your Server

After downloading and extracting oxmysql, you need to move it to your server files:

  1. Navigate to the resources folder in your FiveM server directory.
  2. Create a new folder named oxmysql.
  3. Copy the contents of the extracted oxmysql folder into this new oxmysql folder.

Step 3: Modify the fxmanifest.lua

To enable oxmysql on your server, you’ll need to edit the fxmanifest.lua file:

  1. Open the fxmanifest.lua file located within your oxmysql folder.
  2. Ensure it looks something like this:
    fx_version 'cerulean'
    game 'gta5'
    
    author 'YourName'
    description 'oxmysql: MySQL connector for FiveM (optimized)'
    
    client_scripts {
        'client/main.lua',
    }
    
    server_scripts {
        '@oxmysql/init.lua',
        'server/main.lua',
    }
    
  3. Save the changes.

Step 4: Configure the Database Connection String

Your connection string is crucial for oxmysql to connect to your MySQL database. Here’s how you can set it up:

  1. Open your server.cfg file located in your server root directory.
  2. Add the following line:
    set mysql_connection_string "mysql://username:password@localhost:3306/database_name"
    
    Replace username, password, localhost, and database_name with your actual MySQL credentials.
  3. Ensure that you have correctly set up your MySQL database and that it’s accessible from your FiveM server.

Step 5: Testing the Connection

Once you have modified the configuration files, it’s essential to test if the connection works properly. Here’s how you can do this:

  1. Start your FiveM server.
  2. Check the console for errors related to database connections.
  3. You can use a simple script to test database interactions, ensuring that basic SELECT queries function correctly.

Sample Testing Script

You might want to create a simple resource to test database interactions:

  • Create a folder named db_test in your resources directory.
  • Inside db_test, create fxmanifest.lua and server.lua files.
  • In fxmanifest.lua, add:
    fx_version 'cerulean'
    game 'gta5'
    
    server_scripts {'server.lua'}
    
  • In server.lua, add:
    MySQL.Async.fetchAll('SELECT * FROM users', {}, function(result)
        print(json.encode(result))
    end)
    
  • Add start db_test to your server.cfg.
  • Restart the server and monitor the console for outputs.

Troubleshooting Common Issues

In case you encounter problems during the installation process, consider the following common issues:

  • Connection errors: Double-check your connection string for typos.
  • Resource not starting: Ensure oxmysql is referenced correctly in your server.cfg.
  • Database access denied: Verify that the user in your connection string has the correct permissions for the specified database.

Frequently Asked Questions

Q1: Can I use oxmysql with ESX and QBCore frameworks?
A1: Yes, oxmysql is compatible with both ESX and QBCore frameworks, making it a versatile choice for various server types.

Q2: How do I check if oxmysql is functioning correctly?
A2: You can test its functionality by running simple database queries through scripts and checking for no errors in the server console.

Q3: What should I do if my database connection fails?
A3: Review your connection string for accuracy and check your database server status and permissions.

Q4: Is there any additional configuration needed for specific database types?
A4: No, the default setup generally works fine, but ensure that your MySQL server is configured correctly for remote access if needed.

Q5: Where can I find more FiveM scripts and assets?
A5: Discover more scripts and assets on our Fivemania resources page.

By following these steps, you should now have a fully operational oxmysql installation integrated with your FiveM server, ready to enhance your database management capabilities. Happy scripting!

#oxmysql#fivem#database#installation#connection string

Keep reading