วิธีติดตั้ง oxmysql FiveM เชื่อม Database ให้ Server ใช้งานได้
การติดตั้ง oxmysql ใน FiveM คือการสร้างตัวกลางให้ FXServer และ Scripts สามารถอ่านหรือบันทึกข้อมูลลง Database ได้ เช่น Character, เงิน, รถ, Inventory, บ้าน, Jobs และข้อมูลอื่นที่ต้องอยู่ต่อหลังผู้เล่นออกจาก Server
โครงสร้างที่ถูกต้องคือ
FiveM / FXServer
↓
oxmysql
↓
MariaDB
↓
Database
สิ่งสำคัญคือ
oxmysql
≠ Database Server
คุณต้องติดตั้ง Database Server เช่น MariaDB แยกต่างหาก แล้วจึงให้ oxmysql เชื่อมต่อไปยัง Database นั้น
① สิ่งที่ต้องเตรียมก่อนติดตั้ง oxmysql
ควรมีอย่างน้อย
FiveM FXServer
server.cfg
MariaDB
Database
Database User
oxmysql
และต้องรู้ข้อมูล
Database Host
Database Port
Database Name
Username
Password
ตัวอย่าง
Host: 127.0.0.1
Port: 3306
Database: fivem
User: fivem_server
Password: StrongPassword
ข้อมูลเหล่านี้จะนำไปสร้าง mysql_connection_string
② ควรใช้ MySQL หรือ MariaDB
Current oxmysql Documentation แนะนำ
MariaDB
อย่างชัดเจน
เหตุผลหนึ่งคือ FiveM Resources จำนวนมากถูกออกแบบมาจากสภาพแวดล้อม MySQL 5.7 และอาจเจอ Compatibility Differences กับ MySQL 8 เช่น Reserved Keywords และพฤติกรรมของ JSON/LONGTEXT บางส่วน
สำหรับ FiveM Server ใหม่จึงแนะนำ
MariaDB
เป็นตัวเลือกเริ่มต้น
③ ควรใช้ XAMPP หรือไม่
ไม่แนะนำสำหรับ Production FiveM Server
Current Documentation ระบุว่า XAMPP เป็น Web Development Stack และไม่ควรติดตั้ง XAMPP เพียงเพื่อเอา Database มาใช้กับ FiveM
แนวทางที่สะอาดกว่าคือ
ติดตั้ง MariaDB โดยตรง
เพราะ FiveM ไม่จำเป็นต้องมี Apache/PHP จาก XAMPP เพียงเพื่อใช้งาน Database
④ ติดตั้ง MariaDB ก่อน
หลังติดตั้ง MariaDB ให้ตรวจว่า Service ทำงาน
แนวคิด
MariaDB Service
↓
Listening Port 3306
↓
Database พร้อมรับ Connection
Port ปกติมักเป็น
3306
แต่สามารถเปลี่ยนได้
ถ้าเปลี่ยน Port ต้องใส่ Port จริงใน Connection String
⑤ สร้าง Database สำหรับ FiveM
ตัวอย่างชื่อ Database
fivem
หรือ
rpserver
ชื่ออะไรก็ได้ที่เหมาะสม
แต่ควรใช้ชื่อสั้นและชัดเจน
ตัวอย่าง SQL
CREATE DATABASE fivem
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
Framework บางตัวอาจมี Requirement หรือ Recipe ของตัวเอง จึงควรตรวจ Framework Documentation ประกอบ
⑥ สร้าง Database User แยก
Production Server ไม่ควรใช้ root ถ้าไม่จำเป็น
แนวทางที่ดีกว่า
MariaDB
├── root
│ └── Database Administration
│
└── fivem_server
└── FXServer
ตัวอย่าง
CREATE USER 'fivem_server'@'localhost'
IDENTIFIED BY 'StrongPassword';
จากนั้นให้ Permissions กับ Database ที่ Server ใช้
GRANT ALL PRIVILEGES
ON fivem.*
TO 'fivem_server'@'localhost';
แล้ว
FLUSH PRIVILEGES;
⑦ ทำไมไม่ควรใช้ root
ถ้า FiveM ใช้
root
และ Credential หลุด ผู้โจมตีอาจได้สิทธิ์มากกว่าที่ Server จำเป็นต้องมี
การใช้ Dedicated Database User ช่วยจำกัด Scope
หลักคือ
ให้สิทธิ์เท่าที่ระบบต้องใช้
⑧ ดาวน์โหลด oxmysql
สำหรับ Server Owner ทั่วไปควรใช้
Latest Release
ของ oxmysql
Current Release ที่ตรวจสอบขณะเขียนบทความคือ
2.14.1
เผยแพร่วันที่ 4 พฤษภาคม 2026
แต่ Version สามารถเปลี่ยนได้ จึงควรใช้ Current Stable Release ในวันที่ติดตั้งจริง
⑨ อย่าใช้ oxmysql จาก Server Pack เก่าโดยไม่ตรวจ
Server Pack เก่าอาจมี
oxmysql รุ่นเก่า
mysql2 รุ่นเก่า
Custom Fork
ไฟล์ถูกแก้ไข
Framework ใหม่อาจต้องการ API หรือ Behavior ที่ใหม่กว่า
ดังนั้น Core Dependency อย่าง oxmysql ควรรู้ว่า
มาจากไหน
Version อะไร
อัปเดตเมื่อไร
⑩ แตกไฟล์ oxmysql
หลังแตกไฟล์ควรได้ Folder ชื่อ
oxmysql
แล้วนำไปไว้ใน
resources/
เช่น
resources/
└── [ox]/
└── oxmysql/
หรือ
resources/
└── oxmysql/
ก็ได้
⑪ ระวัง Folder ซ้อน
ตัวอย่างผิด
resources/
└── oxmysql/
└── oxmysql/
├── fxmanifest.lua
└── ...
ควรเป็น
resources/
└── oxmysql/
├── fxmanifest.lua
└── ...
fxmanifest.lua ต้องอยู่ Root ของ Resource
⑫ อย่า Rename oxmysql
ชื่อ Resource ควรเป็น
oxmysql
อย่าเปลี่ยนเป็น
ox_mysql
mysql_new
oxmysql2
database
เพราะ Scripts จำนวนมาก Import
@oxmysql/lib/MySQL.lua
หรือเรียก
exports.oxmysql
โดยอิงชื่อ Resource ตรง ๆ
⑬ ตั้ง mysql_connection_string
เปิด
server.cfg
แล้วกำหนด Connection String ก่อน Start oxmysql
ตัวอย่าง URI
set mysql_connection_string "mysql://fivem_server:StrongPassword@127.0.0.1:3306/fivem"
จากนั้น
ensure oxmysql
ลำดับจึงเป็น
Connection String
↓
oxmysql
↓
Framework
⑭ Connection String แบบ URI
รูปแบบคือ
mysql://USERNAME:PASSWORD@HOST:PORT/DATABASE
ตัวอย่าง
set mysql_connection_string "mysql://fivem_server:MyPassword@127.0.0.1:3306/fivem"
แยกได้เป็น
Username = fivem_server
Password = MyPassword
Host = 127.0.0.1
Port = 3306
Database = fivem
⑮ Connection String แบบ Semicolon
Current Documentation รองรับอีกแบบ
set mysql_connection_string "user=fivem_server;password=MyPassword;host=127.0.0.1;port=3306;database=fivem"
ทั้งสองรูปแบบใช้ได้
ถ้ารูปแบบหนึ่งมีปัญหากับ Password หรือ Special Characters สามารถลองอีกรูปแบบได้
⑯ ต้องใช้ set หรือ setr
Current Documentation เน้นว่า mysql_connection_string ควรใช้
set
ตัวอย่าง
set mysql_connection_string "..."
ไม่ใช่
setr mysql_connection_string "..."
เพราะ Credential ของ Database ไม่ควรถูก Replicate ไป Client
นี่เป็น Detail สำคัญมาก
⑰ Connection String ต้องอยู่ก่อน Resources
ตัวอย่างที่ถูก
set mysql_connection_string "mysql://fivem_server:password@127.0.0.1:3306/fivem"
ensure oxmysql
ensure ox_lib
ensure qbx_core
ไม่ควรเป็น
ensure oxmysql
set mysql_connection_string "..."
เพราะ Resource อาจ Start ก่อนอ่าน Connection Configuration ที่ต้องใช้
⑱ ระวัง Special Characters ใน Password
Current Documentation เตือนว่า Connection String บาง Format มี Reserved/Unsupported Characters
เช่น
;
,
/
?
:
@
&
=
+
$
#
ถ้า Password มีตัวอักษรเหล่านี้แล้ว Connection String Parse ผิด ให้ลอง
เปลี่ยน Password
หรือ
เปลี่ยน Connection String Format
ตาม Documentation
⑲ ตัวอย่าง Password ที่เสี่ยงกับ URI
สมมติ Password
MyPass@2026
URI อาจมอง
@
เป็นส่วนของ Connection Syntax
จึงอาจ Parse ไม่เหมือนที่ต้องการ
สำหรับ Setup ง่าย ๆ ควรใช้ Strong Password ที่หลีกเลี่ยง Characters ที่ Current Documentation เตือน
⑳ อย่าเปิดเผย Connection String
Connection String มี
Database User
Password
Host
Database
จึงไม่ควรโพสต์แบบเต็มลง
Discord
Forum
GitHub
Facebook
Screenshot
ถ้าต้องขอความช่วยเหลือให้ปิด Password เช่น
set mysql_connection_string "mysql://fivem_server:********@127.0.0.1:3306/fivem"
㉑ Start oxmysql ให้เร็วใน Resource List
Current Documentation แนะนำให้
start oxmysql
อยู่ใกล้ด้านบนของ Resource List
เพราะ Framework และ Scripts จำนวนมากต้องใช้ Database
ตัวอย่าง
ensure oxmysql
ensure ox_lib
ensure qbx_core
ensure ox_target
ensure ox_inventory
㉒ Qbox Start Order
ตัวอย่าง
set mysql_connection_string "mysql://fivem_server:password@127.0.0.1:3306/qbox"
ensure oxmysql
ensure ox_lib
ensure qbx_core
ensure ox_target
ensure ox_inventory
ต้องให้ Database Layer พร้อมก่อน Framework
㉓ ESX Start Order
ตัวอย่าง
set mysql_connection_string "mysql://fivem_server:password@127.0.0.1:3306/esx"
ensure oxmysql
ensure es_extended
Current ESX Legacy ใช้ oxmysql ใน Core อยู่แล้ว
ถ้า oxmysql เชื่อม Database ไม่ได้ ESX Player Data ก็อาจโหลดไม่ได้
㉔ QBCore Start Order
ตัวอย่าง
set mysql_connection_string "mysql://fivem_server:password@127.0.0.1:3306/qbcore"
ensure oxmysql
ensure qb-core
Current qb-core มี oxmysql เป็น Dependency
จึงต้อง Start Database Resource ก่อน Core
㉕ ox_core Start Order
ตัวอย่าง
set mysql_connection_string "mysql://fivem_server:password@127.0.0.1:3306/overextended"
ensure oxmysql
ensure ox_lib
ensure ox_core
จากนั้นค่อยเพิ่ม
ox_target
ox_inventory
Gameplay Resources
㉖ เปิด Server และดู Console
เมื่อ oxmysql Start ให้ตรวจ Console
สิ่งที่ต้องการคือ Database Connection สำเร็จและไม่มี
Access denied
Connection refused
Unknown database
Authentication error
Invalid connection string
ถ้ามี Error Database อย่าเริ่ม Debug Framework ก่อน
㉗ Access denied แก้อย่างไร
ถ้าเจอ
Access denied for user
ตรวจ
Username
Password
Host ของ Database User
Privileges
ตัวอย่าง
'fivem_server'@'localhost'
กับ
'fivem_server'@'%'
ไม่ได้หมายถึง Database Account แบบเดียวกันเสมอไป
ต้องตั้ง User/Host ให้ตรงกับ Server Architecture
㉘ Connection refused แก้อย่างไร
ตรวจ
MariaDB Service เปิดหรือไม่
Host ถูกหรือไม่
Port ถูกหรือไม่
Firewall
Bind Address
Network Route
ถ้า Database อยู่เครื่องเดียวกับ FXServer มักใช้
127.0.0.1
ได้
แต่ถ้า Database อยู่คนละเครื่องต้องใช้ IP/Hostname ที่เข้าถึงได้จริง
㉙ Unknown database แก้อย่างไร
ถ้าขึ้น
Unknown database 'fivem'
หมายความว่า Connection ไปถึง Database Server แล้ว แต่ Database ชื่อที่ระบุไม่มี
ตรวจ
Database Name
และสร้าง Database หากยังไม่มี
อย่า Reinstall oxmysql เพื่อแก้ Error นี้
㉚ Invalid connection string แก้อย่างไร
Current Source สามารถ Parse ทั้ง
URI
และ
semicolon-separated values
ถ้า Structure ผิดจะเกิด Error
ตรวจ
Username
Password
@
:
/
;
=
และ Special Characters
ลองเปลี่ยน Connection String Format หากจำเป็น
㉛ วิธีทดสอบว่า Database เชื่อมได้จริง
อย่าทดสอบเพียงว่า
oxmysql Started
ให้สร้าง Resource Test เล็ก ๆ แล้ว Query Database
ตัวอย่าง Structure
db_test/
├── fxmanifest.lua
└── server.lua
㉜ fxmanifest.lua สำหรับทดสอบ
fx_version 'cerulean'
game 'gta5'
server_script '@oxmysql/lib/MySQL.lua'
server_script 'server.lua'
dependency 'oxmysql'
จุดสำคัญคือ
server_script '@oxmysql/lib/MySQL.lua'
ต้องมาก่อน Server Script ที่ใช้ MySQL
㉝ server.lua สำหรับทดสอบ
RegisterCommand('dbtest', function(source)
local result = MySQL.single.await(
'SELECT 1 AS success'
)
if result and result.success == 1 then
print('oxmysql database test: OK')
else
print('oxmysql database test: FAILED')
end
end, true)
จาก Console ใช้
dbtest
ถ้า Query สำเร็จแปลว่า
Resource
↓
oxmysql
↓
Database
ทำงานถึงกันแล้ว
㉞ ทำไมต้อง Import MySQL.lua
บรรทัด
server_script '@oxmysql/lib/MySQL.lua'
ทำให้ Resource ใช้งาน Interface
MySQL
ได้
เช่น
MySQL.query.await(...)
MySQL.single.await(...)
MySQL.scalar.await(...)
MySQL.insert.await(...)
MySQL.update.await(...)
MySQL.prepare.await(...)
MySQL.transaction.await(...)
㉟ MySQL is nil แก้อย่างไร
ถ้า Error
attempt to index a nil value (global 'MySQL')
ตรวจว่า fxmanifest.lua มี
server_script '@oxmysql/lib/MySQL.lua'
หรือไม่
และต้องอยู่ก่อน
server_script 'server.lua'
ที่เรียก MySQL
㊱ ตัวอย่าง Query หลังติดตั้ง
local players = MySQL.query.await(
'SELECT `identifier`, `name` FROM `users`'
)
print(#players)
ใช้สำหรับ Query หลาย Rows
แต่ใน Production ไม่ควร
SELECT *
ทั้ง Table โดยไม่มีเหตุผล
㊲ ตัวอย่าง Query ด้วย Parameter
local player = MySQL.single.await(
'SELECT `firstname`, `lastname` FROM `users` WHERE `identifier` = ? LIMIT 1',
{ identifier }
)
ใช้
?
เป็น Placeholder
แล้วส่งค่าผ่าน Parameters แยก
นี่ควรเป็น Pattern หลักสำหรับ Input Values
㊳ อย่าต่อ SQL String จาก User Input
ไม่ควรทำ
local query = "SELECT * FROM users WHERE name = '" .. name .. "'"
ควรใช้
local row = MySQL.single.await(
'SELECT * FROM users WHERE name = ?',
{ name }
)
ช่วยลดความเสี่ยง SQL Injection และจัดการ Parameters ได้ดีกว่า
㊴ ทดสอบ Insert
สร้าง Table ทดสอบก่อน เช่น
CREATE TABLE db_test (
id INT NOT NULL AUTO_INCREMENT,
message VARCHAR(100) NOT NULL,
PRIMARY KEY (id)
);
จากนั้น
local id = MySQL.insert.await(
'INSERT INTO db_test (message) VALUES (?)',
{ 'FiveM oxmysql test' }
)
print(id)
หากได้ Insert ID แสดงว่า Write Query ทำงาน
㊵ ทดสอบ Update
local affectedRows = MySQL.update.await(
'UPDATE db_test SET message = ? WHERE id = ?',
{
'Updated',
1
}
)
print(affectedRows)
ถ้าได้
1
หมายถึงมีหนึ่ง Row ถูกแก้
㊶ ทดสอบ Scalar
local count = MySQL.scalar.await(
'SELECT COUNT(*) FROM db_test'
)
print(count)
เหมาะกับการทดสอบ Read Query แบบค่าเดียว
㊷ ทดสอบ Transaction
ตัวอย่างง่าย
local success = MySQL.transaction.await({
{
'UPDATE accounts SET balance = balance - ? WHERE id = ?',
{ 100, 1 }
},
{
'UPDATE accounts SET balance = balance + ? WHERE id = ?',
{ 100, 2 }
}
})
print(success)
Transaction เหมาะกับ Operations ที่หลาย Query ต้องสำเร็จร่วมกัน
เช่น
Bank Transfer
ซื้อรถ
ซื้อบ้าน
Trade
Marketplace
㊸ Slow Query Warning คืออะไร
Current oxmysql สามารถแจ้งเตือน Query ที่ใช้เวลานาน
กำหนดผ่าน
set mysql_slow_query_warning 150
ตัวเลขเป็น Threshold ตาม Configuration ปัจจุบัน
ช่วยหา Query ที่อาจช้า
แต่ Slow Query Warning ไม่ได้แปลว่า Database เสียเสมอไป
㊹ Query ตอน Startup ช้าอาจไม่ใช่ปัญหา
Current Documentation เตือนว่า
Slow Query
อาจได้รับผลจาก
Server Hitch
Startup Load
Resource Initialization
และ Query ช้าตอน Server Start ไม่จำเป็นต้องเป็น Critical Problem เสมอ
ต้องดู Pattern และบริบทด้วย
㊺ เปิด mysql_debug
หากต้องการดู Queries
set mysql_debug true
จะช่วย Debug SQL ที่ Resources ส่ง
เหมาะเมื่อหา
Query ผิด
Parameter ผิด
Resource ไหนยิง SQL
แต่ไม่ควรเปิด Logging ปริมาณสูงตลอด Production โดยไม่จำเป็น
㊻ Debug เฉพาะ Resource ได้
Current Documentation รองรับการระบุ Resource List เช่น
set mysql_debug [
"ox_core",
"ox_inventory"
]
ช่วยลด Noise
เหมาะกว่าเปิดทุก Query ของ Server ใหญ่
㊼ เปลี่ยน Debug List ระหว่าง Server ทำงานได้
Current Commands มีแนวคิด
oxmysql_debug add ox_core
หรือ
oxmysql_debug remove ox_core
เพื่อปรับ Resources ที่กำลัง Debug ชั่วคราว
มีประโยชน์เวลาวิเคราะห์ Production Issue โดยไม่ต้องเปลี่ยน Config ทุกครั้ง
㊽ Transaction Isolation Level
Current oxmysql มี Convar
mysql_transaction_isolation_level
Source ปัจจุบันใช้ Default
2
ซึ่ง Mapping ไปยัง
READ COMMITTED
ไม่ควรเปลี่ยนค่านี้เพียงเพราะเห็นใน Config
ต้องเข้าใจ Database Concurrency/Isolation ก่อน
㊾ mysql-async ต้องติดตั้งด้วยไหม
โดยทั่วไปไม่จำเป็นถ้า Stack ใช้ oxmysql
Current Documentation ระบุว่า oxmysql สามารถ Provide Compatibility ให้
mysql-async
ghmattimysql
ได้
ดังนั้นไม่จำเป็นต้องเปิด Database Wrappers หลายตัวพร้อมกันโดยไม่มีเหตุผล
㊿ ใช้ Resources เก่าที่เรียก mysql-async ได้ไหม
หลายกรณีใช้ Compatibility ของ oxmysql ได้
แต่
Compatibility
≠ Script เก่าทุกตัวทำงาน 100%
Resource เก่าอาจยังมี
SQL Syntax เก่า
Deprecated API
Framework Assumptions
ที่ต้องแก้
ควรทดสอบแต่ละ Resource
51. อย่ารัน Database Wrapper หลายตัวมั่ว
ไม่ควรมี
mysql-async
ghmattimysql
oxmysql
ทำหน้าที่ Database Layer พร้อมกันโดยไม่มี Architecture ชัดเจน
อาจทำให้ Debug ยากและเพิ่ม Dependencies โดยไม่จำเป็น
เลือก Database Resource หลักให้ชัด
52. ไม่ควร Query Database ทุก Frame
ห้ามทำ Pattern เช่น
while true do
Wait(0)
local data = MySQL.query.await(...)
end
Database ไม่ได้ออกแบบมาเป็น Runtime State Storage สำหรับทุก Frame
ควรใช้
Framework State
Cache
Memory
สำหรับข้อมูลที่ใช้งานบ่อย
แล้ว Save ลง Database ตามจังหวะที่เหมาะสม
53. Framework API สำคัญกว่า Direct SQL ในหลายกรณี
สมมติต้องเปลี่ยน Job Player
ไม่ควรทำทันทีว่า
UPDATE players
SET job = 'police'
ขณะ Player Online
เพราะ Database อาจเปลี่ยนแต่ Framework Runtime State ยังไม่เปลี่ยน
ควรใช้
Framework Public API
เพื่อเปลี่ยน Player State
แล้วให้ Framework จัดการ Persistence
54. Direct SQL เหมาะกับอะไร
เหมาะกับงาน เช่น
Persistent Custom Tables
Offline Data
Reports
History
Logs
Custom Business Data
หรือ Operations ที่ Architecture ออกแบบให้ Database เป็น Source of Truth โดยตรง
แต่ Core Player State ขณะ Online ควรเคารพ Framework API
55. Database Index สำคัญมาก
ถ้ามี Query
SELECT *
FROM vehicles
WHERE owner = ?
บ่อยมาก
Column
owner
ควรมี Index ที่เหมาะกับ Workload
ไม่เช่นนั้น Database อาจต้อง Scan ข้อมูลจำนวนมาก
Performance Issue จึงไม่ได้เกิดจาก oxmysql ทุกครั้ง
56. อย่า SELECT * ถ้าไม่จำเป็น
ถ้าต้องการเพียง
firstname
lastname
ควร
SELECT firstname, lastname
FROM users
WHERE identifier = ?
แทน
SELECT *
FROM users
WHERE identifier = ?
โดยเฉพาะ Tables ใหญ่ที่มี JSON Data จำนวนมาก
57. Backup Database ก่อน Framework Update
ก่อน Update
ESX
QBCore
Qbox
ox_core
ox_inventory
Phone
Housing
ที่มี Migration SQL
ต้อง Backup Database
เพราะ oxmysql ทำ Query ได้อย่างถูกต้องไม่ได้หมายความว่า Migration SQL ที่คุณกำลังรันถูกต้องเสมอ
58. Production Database ควร Backup อัตโนมัติ
ควรมี
Daily Backup
Multiple Retention Points
Off-machine Copy
Restore Test
ตามความสำคัญของ Server
เพราะ Database เก็บ
Characters
Money
Vehicles
Inventory
Businesses
Housing
ซึ่งเป็นทรัพย์สินทาง Gameplay ของผู้เล่น
59. Checklist ติดตั้ง oxmysql
MariaDB
Service ทำงาน
Port ถูก
Database ถูกสร้าง
Database User ถูก
Permissions ถูก
oxmysql
ใช้ Current Release
Folder ชื่อ
oxmysqlไม่มี Folder ซ้อน
Resource Start สำเร็จ
server.cfg
Connection String อยู่ก่อน Resource
set mysql_connection_string "..."
ensure oxmysql
Resource ที่ใช้ Database
มี
server_script '@oxmysql/lib/MySQL.lua'
ก่อน Server Scripts ที่เรียก MySQL
Testing
SELECT สำเร็จ
INSERT สำเร็จ
UPDATE สำเร็จ
Framework โหลด Player Data ได้
ไม่มี SQL Error
60. วิธีติดตั้ง oxmysql แบบสั้นที่สุด
① ติดตั้ง MariaDB
② สร้าง Database
③ สร้าง Dedicated Database User
④ ตั้ง Password
⑤ Grant Permissions
⑥ Download Current oxmysql Release
⑦ แตก Folder เป็น oxmysql
⑧ วางใน resources
⑨ เปิด server.cfg
⑩ ใส่ mysql_connection_string
⑪ ใช้ set ไม่ใช่ setr
⑫ ใส่ ensure oxmysql
⑬ ให้ oxmysql Start ก่อน Framework
⑭ เปิด Server
⑮ ตรวจ Connection Error
⑯ สร้าง Resource Test
⑰ Import @oxmysql/lib/MySQL.lua
⑱ Test SELECT
⑲ Test INSERT/UPDATE ถ้าจำเป็น
⑳ Import Framework SQL
㉑ Start Framework
㉒ ตรวจ Character/Player Persistence
㉓ Backup Working Database
ตัวอย่าง server.cfg สำหรับ Qbox
set mysql_connection_string "mysql://fivem_server:StrongPassword@127.0.0.1:3306/qbox"
ensure oxmysql
ensure ox_lib
ensure qbx_core
ensure ox_target
ensure ox_inventory
ต้องเปลี่ยน
Username
Password
Database
ให้ตรง Server จริง
ตัวอย่าง server.cfg สำหรับ ESX
set mysql_connection_string "mysql://fivem_server:StrongPassword@127.0.0.1:3306/esx"
ensure oxmysql
ensure es_extended
แล้วจึง Start ESX Resources ตาม Dependency Order
ตัวอย่าง server.cfg สำหรับ ox_core
set mysql_connection_string "mysql://fivem_server:StrongPassword@127.0.0.1:3306/overextended"
ensure oxmysql
ensure ox_lib
ensure ox_core
ensure ox_target
ensure ox_inventory
นี่คือ Database-first Stack ที่เหมาะกับ OX ecosystem
ปัญหาที่พบบ่อยหลังติดตั้ง
Access denied for user
ตรวจ Username/Password/Privileges
Connection refused
ตรวจ MariaDB/Host/Port/Firewall
Unknown database
สร้าง Database หรือแก้ชื่อ
Unknown table
Import Framework/Resource SQL
Unknown column
ตรวจ Schema Version
MySQL is nil
Import
@oxmysql/lib/MySQL.lua
ให้ถูก
Framework เข้าไม่ได้
ตรวจ SQL Error และ Start Order
Inventory ไม่ Save
ตรวจ Database ก่อน Inventory UI
Query ช้า
ตรวจ SQL/Indexes/Database Load ไม่ใช่ Reinstall oxmysql ทันที
อย่าทำสิ่งเหล่านี้
ไม่ควร
ใช้ root แบบไม่มี Password
เปิด Database Port สู่ Internet โดยไม่จำเป็น
โพสต์ Connection String สาธารณะ
ใช้ setr กับ DB Credential
Query Database ทุก Frame
ต่อ User Input เข้า SQL String
เปิด mysql_debug ทุกอย่างตลอดเวลา
แก้ Framework DB ตรง ๆ โดยไม่รู้ Runtime State
สิ่งเหล่านี้สร้างทั้งปัญหา Security และ Performance
สรุปวิธีติดตั้ง oxmysql FiveM
การติดตั้ง oxmysql ที่ถูกต้องควรเริ่มจาก
MariaDB
↓
Database + User
↓
mysql_connection_string
↓
oxmysql
↓
Framework
↓
Gameplay Resources
Current Documentation แนะนำ MariaDB และไม่แนะนำ XAMPP เพียงเพื่อใช้เป็น Database Server
Configuration สำคัญที่สุดคือ
set mysql_connection_string "mysql://USER:PASSWORD@HOST:3306/DATABASE"
และต้องกำหนด ก่อน Start oxmysql
จากนั้น
ensure oxmysql
ควรอยู่ด้านบนของ Resource List ก่อน Framework และ Resources ที่ใช้ Database
สำหรับ Custom Lua Resource ให้เพิ่ม
server_script '@oxmysql/lib/MySQL.lua'
ก่อน Server Scripts อื่นที่ต้องใช้ MySQL
จากนั้นสามารถเรียก
MySQL.query
MySQL.single
MySQL.scalar
MySQL.insert
MySQL.update
MySQL.prepare
MySQL.transaction
ได้
Current Release ที่ตรวจสอบคือ oxmysql 2.14.1 ซึ่งเผยแพร่วันที่ 4 พฤษภาคม 2026 แต่ไม่ควรล็อก Server ไว้กับตัวเลขนี้โดยไม่ตรวจ Compatibility ของ Framework ที่ใช้อยู่
แนวทางของ comsiam คือหลังติดตั้งอย่ารีบเปิด Framework ทั้งชุดทันที ให้ทดสอบ SELECT 1 ผ่าน Resource เล็ก ๆ ก่อน หาก Query ผ่านจึงค่อย Start ESX, QBCore, Qbox หรือ ox_core วิธีนี้ช่วยแยกปัญหา Database Infrastructure ออกจาก Framework ได้ชัดเจน
สำหรับ Production comsiam แนะนำให้ใช้ Dedicated Database User, Backup อัตโนมัติ และทดสอบ Restore จริง รวมถึงใช้ Parameterized Queries และ Framework APIs ให้ถูกต้อง เพราะ Server ที่ oxmysql เชื่อมต่อสำเร็จแต่ SQL Architecture ไม่ดี ก็ยังสามารถเกิดข้อมูลหาย, Economy เพี้ยน และ Performance Problems ได้
Comments
Post a Comment