local cjson = require "cjson"
local mysql = require "resty.mysql"

local db, err = mysql:new()  
if not db then  
    ngx.say("new mysql error : ", err)  
    return  
end  
 
db:set_timeout(1000)  
  
local props = {  
    host = "127.0.0.1",
    port = 3306,
    database = "mysql",
    user = "root",
    password = "123456"
}

local res, err, errno, sqlstate = db:connect(props)  
  
if not res then  
   ngx.say("connect to mysql error : ", err, " , errno : ", errno, " , sqlstate : ", sqlstate)  
end

连接这一步就报错了。  failed to receive the result packet: failed to receive packet header: closed

网上找了半天,没找到原因,麻烦帮忙看一下。
    Hello

    具体你可以通过这些方式来分析:

    1. 看 nginx error_log
    2. 打印更多日志
    3. tcpdump 抓包

    2017-03-16 14:33 GMT+08:00 <congyua...@gmail.com>:
    local cjson = require "cjson"
    local mysql = require "resty.mysql"

    local db, err = mysql:new()  
    if not db then  
        ngx.say("new mysql error : ", err)  
        return  
    end  
     
    db:set_timeout(1000)  
      
    local props = {  
        host = "127.0.0.1",
        port = 3306,
        database = "mysql",
        user = "root",
        password = "123456"
    }

    local res, err, errno, sqlstate = db:connect(props)  
      
    if not res then  
       ngx.say("connect to mysql error : ", err, " , errno : ", errno, " , sqlstate : ", sqlstate)  
    end

    连接这一步就报错了。  failed to receive the result packet: failed to receive packet header: closed

    网上找了半天,没找到原因,麻烦帮忙看一下。

    --

      4 years later

      我们也在测试环境中遇到 "failed to receive packet header: timeoutnilnil, context: ngx.timer"
      很奇怪在公司环境ok,用手机热点环境ok,但是家里的网络不行,怀疑是数据库的ip策略,结果到最后发现是timeout
      的问题,把timeout设置大一点就OK

        Write a Reply...