MySQL Setup

Database Updated: Dec 10, 2025

MySQL Setup

cLib provides MySQL integration using MySQLOO.

Requirements

1. MySQLOO 9.8 Beta - Download 2. MySQL Server - Running and accessible

Installing MySQLOO

Windows

1. Download gmsv_mysqloo_win64.dll
2. Place in garrysmod/lua/bin/

Linux

1. Download gmsv_mysqloo_linux64.dll
2. Place in garrysmod/lua/bin/
3. May need libmysqlclient.so

Connection Setup

-- sv_database.lua
local dbConfig = {
    db_host = "localhost",      -- MySQL host
    db_port = 3306,             -- MySQL port
    db_user = "gmod_user",      -- Username
    db_password = "password",   -- Password
    db_name = "gmod_server"     -- Database name
}

local db = nil

hook.Add("cLib.OnLoaded", "MyAddon.Database", function()
    db = cLib.MYSQL.f.ConnectDatabase(dbConfig,
        function(database)
            -- Connected successfully
            print("[MyAddon] Database connected!")
            MyAddon.DB = database
            MyAddon.CreateTables()
        end,
        function(database, err)
            -- Disconnected
            print("[MyAddon] Database disconnected:", err)
        end
    )
end)

Create Tables

function MyAddon.CreateTables()
    local query = [[
        CREATE TABLE IF NOT EXISTS myaddon_players (
            id INT AUTO_INCREMENT PRIMARY KEY,
            steamid VARCHAR(32) NOT NULL UNIQUE,
            name VARCHAR(64),
            money INT DEFAULT 0,
            playtime INT DEFAULT 0,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            INDEX idx_steamid (steamid)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
    ]]
    
    cLib.MYSQL.f.Query(MyAddon.DB, query, {},
        function()
            print("[MyAddon] Tables created!")
        end,
        function(err)
            print("[MyAddon] Table creation failed:", err)
        end
    )
end

Configuration Options

-- Max reconnection attempts (default: 15)
cLib.MYSQL.MaxAttempts = 15

-- Seconds between attempts (default: 5)
cLib.MYSQL.AttemptCooldown = 5

-- Health monitor interval (default: 15)
cLib.MYSQL.InstancesHealthMonitorCooldown = 15