FiveM oxmysql Connection Pool คืออะไร? วิธีตั้ง mysql_connection_string เชื่อม MySQL/MariaDB ให้เสถียรและแก้ Connection Error

 FiveM oxmysql Connection Pool คือกลไกการจัดการ Connections ระหว่าง FXServer กับ MySQL/MariaDB โดย oxmysql ใช้ node-mysql2 เป็น Database Driver และฝั่ง mysql2 รองรับ Connection Pool ซึ่งจะสร้าง Connections ตามความต้องการจนถึงขีดจำกัดของ Pool แทนการสร้าง Connection ใหม่สำหรับ SQL ทุกคำสั่ง.

สำหรับผู้ดูแล FiveM สิ่งสำคัญกว่าการพยายามปรับจำนวน Connections แบบสุ่ม คือการตั้ง mysql_connection_string ให้ถูกต้อง, ให้ Database ทำงานอยู่จริง, Start oxmysql ก่อน Resources ที่ต้องใช้ Database และลด Query ที่ไม่จำเป็น เพราะปัญหา Connection จำนวนมากไม่ได้เกิดจาก Pool เล็กเกินไป แต่เกิดจาก Configuration, Network หรือ Query Architecture ที่ผิด.

① FiveM เชื่อม Database อย่างไร

โครงสร้างทั่วไปคือ:

FiveM Resource
↓
oxmysql
↓
node-mysql2
↓
Connection Pool
↓
MySQL / MariaDB

oxmysql ระบุอย่างเป็นทางการว่าเป็น MySQL Resource สำหรับ FXServer ที่ใช้ node-mysql2 ในการสื่อสารกับ Database.

ดังนั้นเวลาที่ Resource เรียก:

MySQL.query.await(...)

หรือ:

MySQL.prepare.await(...)

Resource ไม่ได้เปิด TCP Connection ใหม่ด้วยตัวเองทุกครั้ง แต่ Database Layer เป็นผู้จัดการ Connection ให้

② Connection คืออะไร

Connection คือช่องทางการติดต่อหนึ่งช่องระหว่าง:

FXServer
↔
Database Server

ผ่านข้อมูล เช่น:

Host
Port
Username
Password
Database

โดย oxmysql กำหนดข้อมูลหลักเหล่านี้ผ่าน:

mysql_connection_string

ก่อน Start Resources ที่ต้องใช้งาน Database.

③ Connection Pool คืออะไร

แทนที่จะทำ:

Query 1
↓
เปิด Connection
↓
Query
↓
ปิด

Query 2
↓
เปิด Connection ใหม่
↓
Query
↓
ปิด

Pool ใช้แนวคิด:

Connection Pool
├── Connection A
├── Connection B
├── Connection C
└── ...

Queries สามารถใช้งาน Connections จาก Pool และ Driver จัดการคืน Connection เมื่อ Query เสร็จ

mysql2 Documentation ระบุว่า Pool ไม่สร้าง Connections ทั้งหมดขึ้นมาตั้งแต่เริ่ม แต่จะสร้างตามความต้องการจนถึง Connection Limit.

④ Connection Pool ช่วยอะไร

ประโยชน์หลักคือไม่ต้องสร้าง Connection ใหม่จากศูนย์สำหรับทุก Query

Flow จึงเป็น:

Resource Query
↓
Pool จัด Connection
↓
Database Execute
↓
Result
↓
Connection กลับสู่ Pool

mysql2 ระบุว่าเมื่อใช้ pool.query() Connection จะถูก Release กลับสู่ Pool เมื่อ Query ทำงานเสร็จ.

⑤ oxmysql ใช้ Pool หรือไม่

oxmysql ใช้ node-mysql2 เป็น Database Driver และ mysql2 มี Connection Pool API เป็นส่วนหลักของระบบ Connection Management.

อย่างไรก็ตาม Server Owner ทั่วไปไม่จำเป็นต้องสร้าง mysql.createPool() เองใน Lua Resource เพราะ oxmysql ทำหน้าที่เป็น Database Layer ให้ FXServer อยู่แล้ว

⑥ อย่าสร้าง MySQL Connection เองในทุก Resource

หาก Server ใช้ oxmysql อยู่แล้ว ควรใช้:

MySQL.query.await(...)
MySQL.single.await(...)
MySQL.prepare.await(...)

ผ่าน oxmysql

แทนการให้ Resource แต่ละตัวสร้าง Database Client หรือ Connection Pool ของตัวเองโดยไม่มีเหตุผล เพราะจะเพิ่มความซับซ้อนในการจัดการ Connections และ Configuration

⑦ mysql_connection_string คืออะไร

เป็นค่าที่ oxmysql ใช้สำหรับกำหนดว่าจะเชื่อม Database ใด

ตัวอย่างรูปแบบ URL:

set mysql_connection_string "mysql://USER:PASSWORD@127.0.0.1:3306/DATABASE"

oxmysql Documentation ระบุให้ตั้ง Connection String ก่อน Start Resources ที่ต้องใช้ Database.

⑧ Connection String แบบ URL

ตัวอย่าง:

set mysql_connection_string "mysql://fivem_user:CHANGE_ME@127.0.0.1:3306/fivem"

โครงสร้าง:

mysql://
USERNAME
:
PASSWORD
@
HOST
:
PORT
/
DATABASE

อย่านำ Password ตัวอย่างไปใช้จริง

⑨ Connection String แบบ Key/Value

oxmysql รองรับอีกรูปแบบ เช่น:

set mysql_connection_string "user=fivem_user;password=CHANGE_ME;host=127.0.0.1;port=3306;database=fivem"

เอกสารปัจจุบันแสดงทั้ง URL Format และ Semicolon-separated Format.

⑩ ควรใช้ Format ไหน

ใช้ Format ที่อ่านง่ายและทำงานกับ Credential ของ Server

oxmysql เตือนว่า Special Characters บางตัวอาจถูก Reserved หรือไม่รองรับใน Connection String บางรูปแบบ และแนะนำให้ลองเปลี่ยน Connection String Format เมื่อพบปัญหา.

⑪ Password มี @ หรือ # แล้วต่อไม่ได้เพราะอะไร

Connection String แบบ URL ใช้อักขระบางตัวเป็นส่วนของ URL Syntax

oxmysql Documentation ระบุ Characters ที่อาจสร้างปัญหา เช่น:

;
,
/
?
:
@
&
=
+
$
#

และแนะนำให้ลองใช้ Connection String Format อื่นหาก Credential มี Characters เหล่านี้.

⑫ อย่าแก้ปัญหาด้วยการแชร์ Password จริง

ถ้าต้อง Debug:

mysql://user:password@host/database

ให้ปิดบัง:

user
password
public IP

ก่อนโพสต์ Log หรือส่ง Screenshot

Database Credentials ถือเป็นข้อมูลสำคัญของ Server

⑬ oxmysql ควร Start ตอนไหน

ควร Start ก่อน Framework และ Resources ที่เรียก Database

ตัวอย่าง:

ensure oxmysql
ensure my_framework
ensure my_resource

oxmysql Documentation ระบุให้เพิ่ม oxmysql ไว้ด้านบนของ Resource List และเอกสาร ox_inventory ก็ย้ำว่า Resource Start Order ต้องเรียง Dependencies ให้ถูก.

⑭ ถ้า Resource Start ก่อน oxmysql จะเกิดอะไร

อาจพบ Error เช่น:

No such export in resource oxmysql

Official Common Issues ของ oxmysql ระบุว่าสาเหตุหนึ่งคือ Resource เรียก Export ก่อน oxmysql Start และแนะนำให้จัด oxmysql ไว้ต้น Resource Start Order.

⑮ Unable to establish a connection คืออะไร

เป็น Error กว้างๆ ที่หมายถึง oxmysql ยังเชื่อม Database ไม่สำเร็จ

Official Common Issues แนะนำตรวจอย่างน้อย:

mysql_connection_string
Database มีอยู่จริง
Database Server กำลังทำงาน
Connection String Format
Special Characters

⑯ Checklist เมื่อ oxmysql ต่อไม่ได้

ตรวจตามนี้:

① MySQL/MariaDB Running?
② Database Name ถูก?
③ Username ถูก?
④ Password ถูก?
⑤ Host ถูก?
⑥ Port ถูก?
⑦ Connection String ถูก?
⑧ Firewall อนุญาต?
⑨ oxmysql Start ก่อน Resource?
⑩ Database Server Reachable?

อย่าแก้ SQL Query ก่อนถ้า Connection Layer ยังไม่ผ่าน

⑰ ECONNREFUSED คืออะไร

แนวคิดของ:

ECONNREFUSED

คือ Applicationพยายามเชื่อมไปยัง Host/Port แต่ Connection ถูกปฏิเสธ

ควรตรวจ:

Database Service
Port
Bind Address
Firewall
Host

ก่อนตรวจ Schema หรือ SQL

⑱ ETIMEDOUT คืออะไร

แนวคิดคือ Connection Attempt ไม่ได้รับ Response ภายในเวลาที่กำหนด

ถ้า Database อยู่ Remote Host ให้ตรวจ:

Network Route
Firewall
Security Group
Database Listen Address
Host IP
Database Availability

ปัญหานี้ต่างจาก SQL Syntax Error เพราะยังไม่ถึงขั้น Execute SQL

⑲ Access denied for user คืออะไร

โดยทั่วไปหมายถึง Database Server ตอบกลับแล้ว แต่ Credential หรือ Permission ไม่ผ่าน

ตรวจ:

Username
Password
Allowed Host
Database Privileges

อย่าแก้ด้วยการให้ Permission ทุกอย่างแก่ User ทันที

Production ควรใช้ Database User ที่มี Permission เท่าที่ Application ต้องใช้

⑳ Unknown database คืออะไร

หมายถึง Connection กำลังระบุ Database Name ที่ไม่มีอยู่ตาม Server ที่เชื่อมไป

ตรวจ:

database=fivem

กับชื่อ Database จริง

Official oxmysql Common Issues ระบุให้แน่ใจว่า Database มีอยู่และกำลังทำงาน.

㉑ Host localhost กับ 127.0.0.1 ต่างกันไหม

ทั้งสองสามารถหมายถึงเครื่อง Local ได้ในหลาย Environment แต่ Behavior ของ Database Client/OS สามารถต่างกัน

หาก Connection มีปัญหาให้ทดสอบตาม Configuration ที่ Database Server ใช้จริง เช่น:

127.0.0.1

แทนการเดาว่า Hostname Resolution ทำงานถูกเสมอ

㉒ Database อยู่เครื่องเดียวกับ FXServer

Architecture:

FXServer
│
├── oxmysql
└── MariaDB

สามารถใช้ Local Host/Loopback Connection ได้ตาม Database Configuration

ข้อดีคือ Network Path สั้น

แต่ CPU, RAM และ Disk ของ Database ก็แชร์เครื่องกับ FXServer

㉓ Database อยู่คนละเครื่อง

Architecture:

FXServer
↓
Network
↓
Database Server

ช่วยแยก Workload ได้ แต่เพิ่ม Network Layer

ดังนั้นต้องตรวจ:

Latency
Packet Loss
Firewall
Database Access Control
Availability

เพิ่ม

㉔ Remote Database ช้าทำให้ Query ช้าได้ไหม

ได้ เพราะเวลาที่ Resourceเห็นอาจประกอบด้วยทั้ง:

Network
+
Database Execution
+
Driver Processing

ดังนั้น Query ช้าเมื่อ Database อยู่ Remote ไม่ควรตรวจ Index อย่างเดียว

ต้องดู Network ระหว่าง FXServer กับ Database ด้วย

㉕ Connection Pool สร้าง Connections ทั้งหมดทันทีไหม

mysql2 Documentation ระบุว่า ไม่

Pool จะสร้าง Connections ตามความต้องการจนถึง Connection Limit แทนการเปิดทุก Connection ตั้งแต่ Pool ถูกสร้าง.

จึงไม่ควรตีความว่า:

Pool limit = 10

แล้วจะมี 10 Active Database Connections ตลอดเวลาเสมอ

㉖ Query เสร็จแล้ว Connection หายไหม

เมื่อใช้ Pool แนวคิดคือ Connection ถูกคืนกลับ Pool เพื่อใช้กับงานอื่น แทนการสร้างใหม่ทุกครั้ง

mysql2 ระบุว่า pool.query() จะ Release Connection อัตโนมัติเมื่อ Query Resolve.

㉗ ต้อง connection.release() ใน Lua oxmysql ไหม

โดยทั่วไปไม่

Resource ที่ใช้:

MySQL.query.await(...)

ไม่ได้รับ Raw mysql2 Connection Object มาให้จัดการเอง

oxmysql ทำหน้าที่ Database Wrapper ให้แล้ว

ดังนั้นอย่านำตัวอย่าง Node.js:

connection.release()

มาวางใน FiveM Lua Resource แบบตรงๆ

㉘ connectionLimit คืออะไร

ใน mysql2 Connection Pool มีแนวคิด Connection Limit ซึ่งกำหนดเพดานจำนวน Connections ที่ Pool สามารถสร้างได้ และ Pool จะสร้าง Connections ตามความต้องการจนถึงขีดจำกัดนั้น.

แต่ Server Owner ที่ใช้ oxmysql ควรระวังว่า เอกสาร oxmysql สาธารณะเน้นการตั้ง mysql_connection_string มากกว่าให้ผู้ใช้ปรับ Pool Internals ด้วยตัวเอง

จึงไม่ควร Copy ค่า connectionLimit จาก Tutorial Node.js มาใส่ FiveM Config โดยไม่ตรวจว่า oxmysql Version ที่ใช้อยู่รองรับ Configuration นั้นอย่างไร

㉙ เพิ่ม Connection Limit แล้ว Server เร็วขึ้นไหม

ไม่แน่นอน

ถ้า Bottleneck คือ:

Query ไม่มี Index
SQL ช้า
Database CPU 100%
Disk ช้า
Query เยอะเกินไป

การเพิ่มจำนวน Connections อาจไม่ได้แก้ปัญหา

บางกรณีอาจเพิ่ม Concurrent Load ให้ Database มากขึ้นด้วย

㉚ Connection Pool ไม่ใช่การแก้ Slow Query

จำ:

Pool
= จัดการ Connections

Index
= ช่วยค้น Rows

Prepare
= Query ที่เรียกซ้ำ

Cache
= ลด Query

Transaction
= Consistency

เป็นคนละ Layer

ถ้า Query ใช้ 2 วินาทีเพราะ Scan Table ใหญ่ ให้แก้ Query/Index ไม่ใช่เพิ่ม Connections ก่อน

㉛ Too many connections คืออะไร

เป็น Error ที่เกี่ยวข้องกับจำนวน Database Connections เกินข้อจำกัดของ Database Server หรือเกิด Connection Management ที่ผิดปกติ

อย่าแก้ทันทีด้วย:

เพิ่ม max_connections สูงๆ

โดยไม่ตรวจว่า Connections มาจากไหน

ควรดู:

จำนวน Applications
Connection Pools
Long Queries
Database Monitoring
Resource Architecture

ก่อน

㉜ FiveM Resource ควรเปิด Connection เองหรือไม่

ถ้าใช้ oxmysql อยู่แล้ว Resource ส่วนใหญ่ควรใช้ oxmysql API แทน

ตัวอย่าง:

local rows =
    MySQL.query.await(
        'SELECT `id` FROM `users` WHERE `identifier` = ?',
        {
            identifier
        }
    )

ให้ Database Wrapper จัด Connection Lifecycle

㉝ Query พร้อมกันจำนวนมากเกิดอะไรขึ้น

สมมติ:

100 Players
↓
แต่ละคน Query 20 ครั้งพร้อมกัน
↓
2,000 Database Operations

Pool และ Database ต้องจัดการ Workload จำนวนมาก

ถึง Connection Management จะดี SQL Architecture ก็ยังสามารถสร้าง Load Spike ได้

จึงควรลด Query Count ก่อนเพิ่ม Connection Capacity แบบสุ่ม

㉞ Login Query Burst คืออะไร

เมื่อ Server Restart หรือมีผู้เล่นเข้าใกล้กันจำนวนมาก Resource อาจทำ:

Account Query
Character Query
Settings Query
Vehicle Query
Metadata Query

หลายชุดพร้อมกัน

ให้ตรวจ:

Query Count ต่อ Login

ด้วย oxmysql Debug Tools

ไม่ใช่ดูแค่จำนวน Connections

㉟ Autosave Burst คืออะไร

ตัวอย่าง:

ทุก 5 นาที
↓
Save Player ทุกคนพร้อมกัน

ถ้ามี 200 Players และแต่ละ Player Save หลาย Tables Database จะได้รับ Write Burst

แนวทางที่ควรพิจารณา:

Dirty Flag
Stagger Save
Save เฉพาะข้อมูลเปลี่ยน
Transaction เมื่อจำเป็น
Upsert ตาม Use Case

แทนการเพิ่ม Pool Size อย่างเดียว

㊱ Cache ช่วย Connection Pool อย่างไร

Cache ช่วยลด Query ที่ต้องเข้าสู่ Database ตั้งแต่ต้น

เช่น:

Login
↓
Query Settings
↓
Cache

Gameplay
↓
อ่าน Cache

แทน:

Gameplay Action ทุกครั้ง
↓
Database Query

เมื่อ Query ลดลง Pressure ต่อ Pool และ Database ก็ลดลงด้วย

㊲ Prepared Query ช่วย Pool ไหม

MySQL.prepare ไม่ได้เพิ่มจำนวน Connections แต่ช่วยจัดการ Frequently-called Query ให้เหมาะสมใน Layer ของ Query Execution

oxmysql ระบุว่า Prepare เหมาะกับ Queries ที่ถูกเรียกบ่อย.

แต่ถ้า Query Frequency สูงเกินความจำเป็น ควรลด Frequency ด้วย

㊳ Transaction ใช้ Connection อย่างไรในแนวคิด

Transaction หลาย Operations ต้องทำภายใต้ Context ที่รักษา Transaction State จน Commit/Rollback

ดังนั้น Transaction ที่ยาวหรือมี Slow Queries จำนวนมากสามารถยึด Database Work ไว้นานขึ้น

จึงควรรวมเฉพาะ Queries ที่ต้อง Atomic จริง ไม่ใช่ใส่ Query ทุกอย่างใน Transaction เดียว

㊴ Slow Query ทำให้ Pool ตันได้ไหม

ในเชิง Architecture เป็นไปได้

ถ้า Connections หลายตัวกำลังติดอยู่กับ Queries ที่ใช้เวลานาน:

Connection A → Slow Query
Connection B → Slow Query
Connection C → Slow Query

Queries ใหม่อาจต้องรอ Capacity

ดังนั้นการ Optimize Slow Query สามารถสำคัญกว่าการเพิ่มจำนวน Connections

㊵ mysql_debug ช่วยวิเคราะห์ Connection Problem ได้ไหม

mysql_debug เหมาะกับการดู Query Execution มากกว่าการตรวจ TCP Connection โดยตรง แต่ช่วยให้รู้ว่า:

Resource ไหน Query
Query บ่อยแค่ไหน
Query ไหนใช้เวลานาน

เมื่อ Connection สำเร็จแล้ว

ถ้ายังขึ้น Unable to establish a connection ให้แก้ Connection Configuration ก่อน

㊶ วิธีแยก Connection Error กับ Query Error

Connection Error

มักเกิดก่อน SQL Execute

เช่น:

Unable to establish a connection
ECONNREFUSED
ETIMEDOUT
Access denied
Unknown database

Query Error

Connection ผ่านแล้ว แต่ SQL มีปัญหา เช่น:

Unknown column
Syntax error
Duplicate entry
Table doesn't exist

การแยกสองกลุ่มนี้จะช่วยแก้ได้เร็วขึ้น

㊷ Database Connection ผ่านแต่ Resource ยัง Error

ตรวจ:

Schema Import ครบ?
Table มีจริง?
Columns ตรง Version?
Resource Migration ครบ?
oxmysql API ถูก?

Connection สำเร็จไม่ได้หมายความว่า Database Schema ถูกต้อง

㊸ oxmysql Import ใน fxmanifest.lua

สำหรับ Lua Resources ที่ใช้ oxmysql สามารถ Import Library ก่อน Server Scripts ตาม Documentation ของ oxmysql

แนวคิด:

fx_version 'cerulean'
game 'gta5'

server_script '@oxmysql/lib/MySQL.lua'
server_script 'server.lua'

ทำให้ server.lua เรียก:

MySQL.query.await(...)

ได้ตาม Library Integration ของ Resource

㊹ อย่าใส่ Connection String ใน client.lua

Database Credentials ต้องอยู่ฝั่ง Server Configuration

ไม่ควรมี:

local password = 'database-password'

ใน Client Resource

เพราะ Client Code ไม่ใช่ที่เก็บ Secret

㊺ Connection String ควรอยู่ตรงไหน

วางใน Server Configuration ก่อน oxmysql Start

ตัวอย่าง:

set mysql_connection_string "mysql://USER:PASSWORD@127.0.0.1:3306/DATABASE"

ensure oxmysql
ensure framework
ensure my_resource

oxmysql ระบุให้ Configure Connection String ก่อน Start Resources ที่ต้องใช้ Database.

㊻ ใช้ set หรือ setr

เอกสาร oxmysql เน้นว่า Connection String ให้ใช้:

set

และระบุว่าให้ใช้ set เท่านั้นสำหรับค่านี้.

ดังนั้น:

set mysql_connection_string ...

เป็นรูปแบบที่ควรใช้

㊼ อย่า Print Connection String ตอน Debug

หลีกเลี่ยง:

print(GetConvar(
    'mysql_connection_string',
    ''
))

เพราะอาจทำให้ Database Credentials ปรากฏใน Console/Logs

Debug ด้วย Error Type และ Config Fields ที่ปิดบัง Secret แทน

㊽ Dedicated Database User สำคัญไหม

Production ควรใช้ Database User เฉพาะสำหรับ FiveM Application แทน Account ระดับ Admin หากทำได้

แนวคิด:

fivem_app
↓
เฉพาะ Database ที่ FiveM ใช้

แทนให้ Application มีสิทธิ์กว้างเกิน Requirement

㊾ Database Firewall ควรเปิด Port ให้ใคร

ถ้า Database อยู่คนละเครื่อง ควรจำกัดให้ Connection เข้ามาจาก FXServer หรือ Management Hosts ที่ต้องใช้จริง

ไม่ควรเปิด Database Port สู่ Internet ทั้งหมดโดยไม่มีเหตุผล

㊿ Local Database ยังต้องใช้ Password ไหม

Production Database ควรมี Authentication และ Permission ที่เหมาะสม แม้ Application กับ Database จะอยู่เครื่องเดียวกัน

อย่าคิดว่า:

127.0.0.1
=
ไม่ต้องรักษาความปลอดภัย

เพราะ Resource หรือ Process อื่นบน Server อาจกลายเป็น Attack Surface ได้

51. FiveM Connection Pool กับ Player Count

จำนวน Players ไม่ได้แปลตรงๆ ว่า:

1 Player
=
1 Database Connection

Player หนึ่งคนสามารถสร้าง Queries หลายรายการ และหลาย Players สามารถใช้ Pool ร่วมกัน

ดังนั้น Capacity Planning ควรดู:

Queries per second
Query latency
Concurrent operations
Database CPU

ไม่ใช่จำนวน Players อย่างเดียว

52. 100 Players ต้องมี 100 Connections ไหม

ไม่จำเป็น

Connection Pool ถูกออกแบบให้ Connections ถูกนำกลับมาใช้ใหม่กับ Queries ต่างๆ และ mysql2 สร้าง Connections ตามความต้องการจนถึง Limit.

ดังนั้นไม่ควรตั้งสูตร:

Players = Connections

แบบ 1 ต่อ 1

53. 500 Players ต้องเพิ่ม Pool ก่อนหรือไม่

ไม่ควรเริ่มจาก Pool ก่อน

ตรวจ:

Queries/player
Slow Query
Cache
Indexes
Autosave
Login Burst
Database CPU
Disk Latency

เพราะ Resource ที่ Query เกินจำเป็นสามารถทำให้ Database หนักแม้ Pool ใหญ่

54. Connection Pool กับ Database CPU

Pool ช่วยจัด Connections แต่ไม่ได้เพิ่ม Processing Power ของ Database

ถ้า MariaDB CPU:

100%

อยู่แล้ว

เพิ่ม Concurrent Connections อาจทำให้ Database ต้องรับงานพร้อมกันมากกว่าเดิม

จึงต้องดู Bottleneck ก่อนปรับ

55. Connection Pool กับ Disk

Database Queries บางประเภทต้องอ่าน/เขียน Storage

ถ้า Disk I/O ช้า:

Connection เยอะ

ไม่ได้แก้ Storage Bottleneck

ต้องตรวจ:

Index
Query
Buffering
Storage Performance
Database Configuration

ต่อ

56. Connection Pool กับ Network Latency

หาก Database อยู่ Remote:

FXServer
↓
30 ms Network
↓
Database

ทุก Database Operation มี Network Path เพิ่ม

Pool ช่วยลด Connection Setup บางส่วน แต่ไม่ได้ทำให้ Physical Network Latency หายไป

57. Connection Pool กับ Query Count

สมมติ Resource เดิม:

Action เดียว
→ 20 Queries

Optimize เหลือ:

Action เดียว
→ 4 Queries

การลด Query Count สามารถลด Pressure ทั้งต่อ:

Pool
Database CPU
Network
Resource

พร้อมกัน

58. N+1 Query มีผลกับ Pool อย่างไร

ตัวอย่าง:

for i = 1, #players do
    MySQL.single.await(...)
end

ถ้ามี 200 Players:

200 Queries

สำหรับหนึ่ง Operation

ควรตรวจว่าสามารถเปลี่ยนเป็น Query ที่รวมข้อมูลหรือ Cache ได้หรือไม่

Connection Pool ไม่ได้แก้ N+1 Architecture ให้เอง

59. Connection Pool กับ Upsert

Upsert สามารถลด:

SELECT
+
INSERT/UPDATE

เหลือ SQL Statement เดียวใน Use Case ที่เหมาะสม

จึงช่วยลดจำนวน Database Operations ที่ต้องผ่าน Pool แต่ Upsert ต้องมี Primary/Unique Key ที่ออกแบบถูกต้อง

60. Connection Pool กับ rawExecute/prepare

การเลือก:

query
prepare
rawExecute

ควรดู Query Characteristics และ Return Data

ไม่ควรเลือก API จากความเชื่อว่า API ใด “ใช้ Connection น้อยกว่า” โดยไม่มีข้อมูล

oxmysql เป็น Layer เดียวกันที่สื่อสารผ่าน Database Driver

61. ควร Restart oxmysql เมื่อ Connection หลุดไหม

อย่าใช้ Restart เป็นวิธีแก้ประจำโดยไม่หาสาเหตุ

ถ้า Database Connection หลุด ให้ตรวจ:

Database Restart?
Network Drop?
Credential Changed?
Firewall?
Database Crash?
Resource Version?

ก่อน

การ Restart Resource อาจช่วยชั่วคราว แต่ไม่แก้ Infrastructure Problem

62. Database Restart ระหว่าง FiveM เปิดอยู่

ถ้า MariaDB/MySQL Restart:

Existing Connections

สามารถถูกตัด

Behavior หลัง Database กลับมาขึ้นอยู่กับ Driver/Wrapper และ Error Handling ของ Version ที่ใช้งาน

จึงควร Test Database Restart Scenario บน Staging หาก Server ต้องการ High Availability จริง

63. Connection Monitoring ควรดูอะไร

อย่างน้อย:

Active Connections
Queries/sec
Slow Queries
Database CPU
Memory
Disk
Network
FiveM Hitch
oxmysql Errors

จะช่วยแยกได้ว่า Bottleneck มาจาก:

Connections
หรือ
Queries
หรือ
Database

64. Connection Error เกิดเฉพาะช่วง Player เยอะ

ตรวจว่า:

Database Max Connections
Query Burst
Long-running Queries
Autosave
Connection Timeouts
Database CPU

ก่อนเพิ่ม Capacity

ถ้าปัญหาเกิดเฉพาะทุก 5 นาที อาจเป็น Scheduled Save มากกว่าจำนวน Players ตรงๆ

65. Connection Error เกิดตอน Server Start

ตรวจ Startup Order:

Database Service
↓
oxmysql
↓
Framework
↓
Resources

หาก FXServer Start ก่อน Database พร้อม อาจเกิด Connection Failure ตอน Initialisation

ควรให้ Infrastructure Startup Dependency ทำงานเป็นระบบ

66. Checklist oxmysql Connection

ก่อน Production ตรวจ:

  1. Database Server Running

  2. Database Name ถูก

  3. Username ถูก

  4. Password ถูก

  5. Host ถูก

  6. Port ถูก

  7. Connection String ถูก

  8. ใช้ set mysql_connection_string

  9. Connection String อยู่ก่อน oxmysql

  10. oxmysql Start ก่อน Framework

  11. Framework Start ก่อน Resources ที่พึ่ง Framework

  12. Database User Permission ถูก

  13. Firewall ถูก

  14. Remote Database Reachable

  15. ไม่เปิด Database สู่ Internet โดยไม่จำเป็น

  16. ไม่ใส่ Password ใน Client

  17. ไม่ Print Password ลง Logs

  18. ระวัง Special Characters

  19. ลอง Alternate Connection String Format เมื่อจำเป็น

  20. ตรวจ Unable to establish connection

  21. แยก Connection Error กับ SQL Error

  22. ตรวจ Query Count

  23. ตรวจ Slow Queries

  24. ตรวจ N+1 Queries

  25. ใช้ Cache เมื่อเหมาะสม

  26. ใช้ Index

  27. ใช้ Upsert เมื่อเหมาะสม

  28. ใช้ Transaction เฉพาะที่ต้อง Atomic

  29. ไม่ Query ทุก Frame

  30. ไม่เพิ่ม Connections แบบเดา

  31. ตรวจ Database CPU

  32. ตรวจ Disk

  33. ตรวจ Network Latency

  34. ตรวจ Player Login Burst

  35. ตรวจ Autosave Burst

  36. Backup Database

  37. Test Restore

  38. Test Resource Restart

  39. Test Database Restart ใน Staging

  40. Monitor Errors ต่อเนื่อง

ตารางปัญหา oxmysql Connection ที่พบบ่อย

อาการจุดที่ควรตรวจ
Unable to establish connectionConnection String / Database Running
Connection refusedService / Host / Port / Firewall
TimeoutNetwork / Firewall / Remote DB
Access deniedUser / Password / Host Permission
Unknown databaseDatabase Name
No such export oxmysqlStart Order / oxmysql Running
Query ช้าSQL / Index / Workload
Connections เต็มQuery Duration / Pool / DB Limits / Architecture

Official oxmysql Common Issues แนะนำให้เริ่มจาก Connection String, Database State และ Start Order เมื่อเชื่อมต่อไม่ได้.

FiveM oxmysql Connection Pool คืออะไร

คือระบบจัดการ Database Connections ที่อยู่เบื้องหลัง Database Driver โดย oxmysql ใช้ node-mysql2 และ mysql2 รองรับ Pool ที่สร้าง Connections ตามความต้องการจนถึงขีดจำกัด.

ผู้พัฒนา Lua Resource จึงใช้ oxmysql API โดยไม่ต้องเปิด/ปิด Raw Database Connection เองทุก Query

FiveM mysql_connection_string ตั้งยังไง

ตัวอย่าง:

set mysql_connection_string "mysql://USER:PASSWORD@127.0.0.1:3306/DATABASE"

หรือ:

set mysql_connection_string "user=USER;password=PASSWORD;host=127.0.0.1;port=3306;database=DATABASE"

สองรูปแบบนี้อยู่ใน Documentation ของ oxmysql.

FiveM oxmysql ต่อ Database ไม่ได้แก้อย่างไร

ตรวจ:

Connection String
↓
Database Running
↓
Database Exists
↓
Host/Port
↓
Credential
↓
Special Characters
↓
Firewall
↓
Resource Start Order

Official oxmysql Common Issues ระบุ Connection String และ Database Availability เป็นจุดตรวจหลัก.

FiveM oxmysql ต้อง Start ก่อน ESX/QBCore ไหม

หาก Framework นั้นต้องใช้ oxmysql ก็ต้องให้ oxmysql พร้อมก่อน Framework เรียก Database

หลักทั่วไปคือ:

oxmysql
↓
framework
↓
dependent resources

เอกสาร Resource Start Order ของ Overextended แสดง oxmysql ไว้ก่อน Framework อย่างชัดเจน.

FiveM Database Password มี @ แล้วต่อไม่ได้

oxmysql เตือนว่า Special Characters บางชนิดอาจมีปัญหาตาม Connection String Format และแนะนำให้เปลี่ยน Format หากจำเป็น.

อย่าโพสต์ Password จริงเพื่อขอความช่วยเหลือ

FiveM ต้องเพิ่ม Connection Limit ไหม

ไม่ควรปรับเพียงเพราะ Player เพิ่ม

ให้ตรวจ:

Query Frequency
Slow Queries
Database CPU
Connection Usage
Login Burst
Autosave

ก่อน

mysql2 มี Connection Limit ในระบบ Pool แต่ oxmysql Public Setup Documentation ไม่ได้แนะนำสูตร Connection Limit ตามจำนวนผู้เล่น.

FiveM 100 คนต้องใช้ 100 MySQL Connections ไหม

ไม่

Connection Pool นำ Connections กลับมาใช้กับ Queries อื่น และ Connections ถูกสร้างตาม Demand ไม่ใช่หนึ่ง Connection ต่อหนึ่ง Player.

FiveM Database อยู่ Remote ดีไหม

ทำได้ แต่เพิ่ม Network Dependency

ต้องตรวจ:

Latency
Firewall
Database Access
Network Stability

และควรวัด Query Time จริงก่อนตัดสินว่า Remote Database เหมาะกับ Server หรือไม่

FiveM Database อยู่เครื่องเดียวกันดีไหม

Setup ง่ายและไม่มี External Network Hop ระหว่าง FXServer กับ Database แต่ Database จะแชร์:

CPU
RAM
Disk

กับ FXServer

จึงต้องดู Server Scale และ Resource Usage จริง

FiveM Query เยอะต้องเพิ่ม Connections หรือ Optimize Query ก่อน

โดยทั่วไปควรวิเคราะห์ Query ก่อน

เพราะ:

Slow Query
N+1 Query
Query ทุก Tick
Autosave Burst
ไม่มี Cache

เป็นปัญหา Architecture ที่ Pool ใหญ่ขึ้นไม่ได้แก้ให้

FiveM Too many connections แก้อย่างไร

อย่าเริ่มด้วยการเพิ่ม Database max_connections

ตรวจ:

จำนวน Pool/Applications
Long Queries
Active Connections
Query Frequency
Database Load

ก่อน

เป้าหมายคือหาว่า Connections ถูกใช้กับงานอะไรและทำไมถึงค้าง/พร้อมกันมาก

FAQ FiveM oxmysql Connection Pool

oxmysql Connection Pool คืออะไร

เป็นกลไกจัดการ Connections ไปยัง Database โดย oxmysql ใช้ node-mysql2 ซึ่งรองรับ Pool และสร้าง Connections ตามความต้องการจนถึง Limit.

oxmysql ใช้ Database อะไร

oxmysql เป็น MySQL Resource สำหรับ FXServer และถูกออกแบบให้สื่อสารกับ MySQL-compatible Database ผ่าน node-mysql2.

mysql_connection_string คืออะไร

คือ Configuration ที่กำหนด Host, User, Password, Port และ Database ที่ oxmysql จะเชื่อมต่อ.

Connection String ต้องใช้ set หรือ setr

oxmysql Documentation ระบุให้ใช้ set สำหรับ mysql_connection_string.

oxmysql ต้อง Start ก่อน Resource อื่นไหม

ต้อง Start ก่อน Resources ที่มี Dependency กับ oxmysql และ Official Documentation แนะนำให้อยู่ช่วงต้น Resource List.

Unable to establish connection แก้อย่างไร

ตรวจ Connection String, Database Server, Database Name และ Credential ก่อน ซึ่งเป็นรายการหลักใน Official Common Issues.

Password มี Special Character ทำอย่างไร

ลองเปลี่ยน Connection String Format ตามคำแนะนำของ oxmysql และอย่าเปิดเผย Password จริง.

Connection Pool เปิด Connections ทั้งหมดทันทีไหม

ไม่ mysql2 ระบุว่า Pool สร้าง Connections ตามความต้องการจนถึง Connection Limit.

FiveM Player หนึ่งคนใช้หนึ่ง Connection ไหม

ไม่ Connection Pool ถูกแชร์ตาม Database Operations ไม่ได้ Mapping หนึ่ง Player ต่อหนึ่ง Connection

เพิ่ม Connection Limit ช่วย Slow Query ไหม

ไม่ใช่การแก้โดยตรง Slow Query ต้องตรวจ SQL, Index, Workload และ Database Performance

Query เยอะควรทำอะไร

ลด Query ที่ไม่จำเป็น ใช้ Cache, แก้ N+1 Queries, ปรับ Index และกระจาย Autosave ก่อนเพิ่ม Database Concurrency แบบสุ่ม

Database อยู่คนละเครื่องได้ไหม

ได้ แต่ต้องคำนึงถึง Network Latency, Firewall และ Availability เพิ่มเติม

ประเด็นสำคัญ

FiveM oxmysql ทำหน้าที่เป็น Database Layer ระหว่าง Resources กับ MySQL/MariaDB และใช้ node-mysql2 ซึ่งมี Connection Pool สำหรับจัด Connections โดย Pool จะสร้าง Connections ตามความต้องการจนถึงขีดจำกัด ไม่ได้สร้าง Connection ใหม่ให้ผู้เล่นแต่ละคนหรือทุก SQL Query แบบหนึ่งต่อหนึ่ง.

สิ่งที่ Server Owner ควรให้ความสำคัญก่อนคือ:

mysql_connection_string ถูก
↓
Database Running
↓
oxmysql Start ก่อน Dependencies
↓
Queries ถูกออกแบบดี
↓
ลด Query Count
↓
แก้ Slow Query
↓
ค่อย Capacity Tune

oxmysql แนะนำให้ตั้ง mysql_connection_string ก่อน Resources ที่ต้องใช้ Database และ Official Common Issues ให้ตรวจ Connection String กับ Database Availability เป็นอันดับต้นๆ เมื่อ Connection ล้มเหลว.

อย่าคิดว่า Player เพิ่มแล้วต้องเพิ่มจำนวน Database Connections ตาม Player เพราะ Pool ถูกนำกลับมาใช้กับ Database Operations และ Bottleneck ที่แท้จริงอาจเป็น SQL ที่ช้า, N+1 Query, Autosave Burst, Index ไม่เหมาะ หรือ Database CPU/Disk มากกว่าจำนวน Connections

สำหรับผู้อ่าน comsiam ให้จำสูตร “Connection Pool จัด Connections — Query Design จัด Workload” และ comsiam แนะนำให้ Optimize Query Count และ Slow Queries ก่อนปรับ Connection Capacity เพราะ Server ที่ Query ดีมัก Scale ได้มีประสิทธิภาพกว่าการเพิ่ม Connections เพื่อรองรับ SQL ที่ถูกเรียกเกินความจำเป็น

Comments

Popular posts from this blog

FiveM ยังน่าเล่นไหม? Enhanced เปลี่ยน FiveM แค่ไหน

FiveM คืออะไร เล่นอย่างไร สำหรับมือใหม่ เริ่มต้นตั้งแต่ศูนย์

วิธีตั้ง Admin Permission ด้วย add_ace และ add_principal FiveM แบบละเอียด