0
votes

I have been using openresty lua and trying to connect to a mysql db running on my local host. But, I am not able to connect to it and I couldn't debug it as the lua mysql client is not giving any information about the error.

    --
-- Created by IntelliJ IDEA.
-- User: AGK
-- Date: 6/5/18
-- Time: 2:14 PM
-- To change this template use File | Settings | File Templates.
--

local mysql = require "resty.mysql"

local _M={}
function _M.connect()


local db, err = mysql:new()
if not db then
    ngx.say("failed to instantiate mysql: ", err)
    return
end

db:set_timeout(1000) -- 1 sec

-- or connect to a unix domain socket file listened
-- by a mysql server:
--     local ok, err, errcode, sqlstate =
--           db:connect{
--              path = "/path/to/mysql.sock",
--              database = "ngx_test",
--              user = "ngx_test",
--              password = "ngx_test" }

local ok, err, errcode, sqlstate = db:connect{
    host = "127.0.0.1",
    port = 3306,
    database = "mydb",
    user = "root",
    password = "abcd1234",
    charset = "utf8",
    max_packet_size = 1024 * 1024,
    ssl = true,
    sslverify = true,
}

if not ok then
    ngx.say("failed to connect: ", err, ": ", errcode, " ", sqlstate)
    return
end

ngx.say("connected to mysql.")
end

return _M

this outputs failed to connect: failed to connect: connection refused: nil nil

Any idea what might be wrong with this simple program. Note: I am running mysql 5.7.x on my mac.

1
Are you running Openresty and MySQL server on the same box? - Alexander Altshuler
@AlexanderAltshuler yes I am running both of them on the same box. - GAK
And you can see MySQL listening on 3306 port? sudo netstat -tlpn please provide output - Alexander Altshuler
@Alexander Altshuler yes I do. I even connect to the DB using Java code. The frustrating part is lua my sql lib is not giving me any error instead it just gives connection refused and nil for the error code - GAK
May be you run Openresty in container? - Alexander Altshuler

1 Answers

1
votes

If you run within docker container - don't connect to 127.0.0.1.

Depends how do you run MySQL we may need different solutions. I suppose you also run MySQL into container.

Configure Docker networking for both containers in such way so MySQL container would be visible by some name.

Usually I create named user defined network for this purpose.