Database Tools
Guide to using and optimizing database operations in CitizenFX.
Proper database management is crucial for any FiveM server. This guide covers the most popular database libraries, optimization techniques, and best practices.
Overview of Database Libraries
OxMySQL (Recommended)
OxMySQL is the recommended database library for CitizenFX projects. It provides async functionality, improved performance, and better error handling.
MySQL-Async (Legacy)
MySQL-Async was the standard for many years. While still functional, it's recommended to migrate to OxMySQL.
Ghmattimysql (Deprecated)
Ghmattimysql is deprecated and should not be used in new projects.
Setting Up Your Database
Connection Configuration
For OxMySQL, add to your server.cfg
:
For MySQL-Async, configure in config.json
:
Database Structure Best Practices
-
Use Proper Indexes:
-
Use Appropriate Data Types:
-
Foreign Keys for Relationships:
Database Operations
Reading Data
OxMySQL
MySQL-Async
Writing Data
OxMySQL
MySQL-Async
Transactions
OxMySQL
MySQL-Async
Performance Optimization
Query Optimization
-
Use Prepared Statements:
-
Select Only Needed Columns:
-
Use Proper WHERE Clauses:
Connection Pooling
OxMySQL handles connection pooling automatically. For MySQL-Async, configure the pool size in config.json
:
Batch Operations
Caching Strategies
Common Database Patterns
Repository Pattern
Data Models
Troubleshooting
Common Database Errors
-
Connection Issues:
Solutions:
- Check if MySQL server is running
- Verify connection credentials
- Check if database exists
- Check network connectivity
- Verify port configuration
-
Query Errors:
Solutions:
- Verify table schema
- Check query syntax
- Make sure tables are created
- Update queries after schema changes
-
Performance Issues:
Solutions:
- Add proper indexes
- Optimize queries
- Use async operations
- Implement caching
- Check for database locks
Debug Logging
Migration Guide
From MySQL-Async to OxMySQL
Comprehensive migration table:
MySQL-Async | OxMySQL |
---|---|
MySQL.Async.fetchAll | exports.oxmysql:execute |
MySQL.Async.fetchScalar | exports.oxmysql:scalar |
MySQL.Async.execute | exports.oxmysql:execute |
MySQL.Async.insert | exports.oxmysql:insert |
MySQL.Sync.fetchAll | exports.oxmysql:executeSync |
MySQL.Sync.fetchScalar | exports.oxmysql:scalarSync |
MySQL.Sync.execute | exports.oxmysql:executeSync |
MySQL.Sync.insert | exports.oxmysql:insertSync |
For parameter styles:
- MySQL-Async:
@identifier
- OxMySQL:
?
(positional parameters)
Additional Resources
Avoid using synchronous database operations in production environments, as they can block the main thread and cause performance issues.
For more information about server performance, see our Server Optimization guide.