location = /lua-mysql-test {
content_by_lua '
local cjson = require "cjson"
local mysql = require "mysql"
local db, err = mysql:new()
if not db then
ngx.log(ngx.ERR,"failed to instantiate mysql: ", err)
return ngx.exit(500)
end
db:set_timeout(1000)
local ok, err, errno, sqlstate = db:connect{
path = "/var/run/mysqld/mysqld.sock",
database = "db",
user = "user",
password = "password" }
local data = {}
local ok, err = db:close()
--local ok, err = db:set_keepalive(10000, 100)
data.ok = ok
data.err = err
ngx.say(cjson.encode(data))
';
}
{"ok":1}
$ lsof -n -i -U | sed -e '1p' -e '/mysql/!d'
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 10142 http 35u IPv4 3157278 0t0 TCP 127.0.0.1:54103->127.0.0.1:mysql (ESTABLISHED)
mysqld 14909 mysql 23u IPv4 2703699 0t0 TCP 127.0.0.1:mysql (LISTEN)
mysqld 14909 mysql 24u unix 0xffff880038bd8380 0t0 2703700 /run/mysqld/mysqld.sock type=STREAM
mysqld 14909 mysql 54u IPv4 3157279 0t0 TCP 127.0.0.1:mysql->127.0.0.1:54103 (ESTABLISHED)
function _M.close(self)
local sock = self.sock
if not sock then
return nil, "not initialized"
end
self.state = nil
_send_packet(self,0x01,4)
return sock:close()
end
Hmm, I don't think the COM_QUIT packet is mandatory for closing the
MySQL connection. I've never seen any issues from MySQL 5.1 to 5.5.
What exact version of MySQL server are you using?
Also, I still think your Lua code has bugs, because nothing can stop
the client from closing the connection (even when the server is not
ready for that).
$ echo "SHOW STATUS;" | mysql | grep Aborted_clients
Aborted_clients 214
$ curl -s http://localhost/lua-mysql-test > /dev/null
$ echo "SHOW STATUS;" | mysql | grep Aborted_clients
Aborted_clients 215