【发布时间】:2021-08-08 06:27:59
【问题描述】:
我在 openresty 项目中使用 redis 和 mysql,我正在尝试使用 init_worker_by_lua 初始化数据库,但我收到以下错误。
我的 db.lua 代码:
local mysql = require("resty.mysql") -- Introduce mysql
local redis = require("resty.redis") -- Introduce Redis
local db, err = mysql:new()
if not db then
ngx.say("failed to instantiate mysql: ", err)
return
end
local ok, err, errcode, sqlstate = db:connect{
host = "127.0.0.1",
port = 3306,
database = "employee",
user = "root",
password = "password",
charset = "utf8",
max_packet_size = 1024 * 1024,
}
下面是 nginx.conf 文件:
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
init_worker_by_lua_file /Users/rahulgarg/work/conf/db.lua;
server {
listen 8082;
location ~ ^/api(.*)$ {
default_type 'text/html';
add_header 'Content-Type' 'application/json';
access_by_lua_file /Users/rahulgarg/work/conf/auth.lua;
content_by_lua_file /Users/rahulgarg/work/conf/dbapi.lua;
}
}
}
运行 openresty -p pwd/ -c conf/nginx.conf 时收到错误:
[error] 72436#1634105:
init_worker_by_lua_file error:
...ebrew/Cellar/openresty/1.19.3.1_1/lualib/resty/mysql.lua:1094:
API disabled in the context of init_worker_by_lua* stack traceback:
[C]:
in function 'tcp'
...ebrew/Cellar/openresty/1.19.3.1_1/lualib/resty/mysql.lua:1094:
in function 'new'
/Users/rahulgarg/work/conf/db.lua:4: in main chunk
我们如何解决此问题的任何解决方案。谢谢
【问题讨论】: