mysqld.lua
1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
local skynet = require "skynet"
require "skynet.manager"
local mysql = require "skynet.db.mysql"
local db
local idx = ...
local command = {}
function command.open(conf)
local function on_connect(db)
local servId = skynet.getenv("servId")
db:query("set names utf8mb4");
db:query(string.format("use server_%s", servId))
end
local servId = skynet.getenv("servId")
db=mysql.connect({
host=conf.host,
port=conf.port,
database= "mysql",
user=conf.user,
password=conf.pwd,
max_packet_size = 5 * 1024 * 1024,
on_connect = on_connect
})
if not db then
print("failed to connect")
end
end
skynet.start(function()
skynet.dispatch("lua", function(session, address, cmd, ...)
if cmd == "open" then
local f = command[string.lower(cmd)]
skynet.ret(skynet.pack(f(...)))
elseif string.lower(cmd) == "quote_sql_str" then
skynet.ret(skynet.pack(db[string.lower(cmd)](...)))
else
skynet.ret(skynet.pack(db[string.lower(cmd)](db, ...)))
end
end)
skynet.info_func(function()
return skynet.stat("mqlen")
end)
skynet.register(".mysql" .. idx)
end)