Comments (4)
@njentit Have you ever put your connections into the connection pool via the set_keepalive
method? If not, then your connection pool is always empty and you won't have any reused connections. In addition, you need to ensure your set_keepalive
method is not ever failing by always checking its return values in your Lua code.
from lua-resty-mysql.
I'm running 1 worker_processes openresty ver. 1.7.10.1 on osx Lion with mysql 5.6. Here my nginx config, it just your sample config with addition printing get_reused_times after db:connect
location /test {
content_by_lua '
local mysql = require "resty.mysql"
local db, err = mysql:new()
if not db then
ngx.say("failed to instantiate mysql: ", err)
return
end
db:set_timeout(1000) -- 1 sec
local ok, err, errno, sqlstate = db:connect{
host = "127.0.0.1",
port = 3306,
database = "testdb",
user = "test",
password = "test123",
max_packet_size = 1024 * 1024 }
if not ok then
ngx.say("failed to connect: ", err, ": ", errno, " ", sqlstate)
return
end
ngx.say("connected to mysql.")
local times, err = db:get_reused_times()
if not times then
ngx.say("failed to get get_reused_times: ", err)
return
end
ngx.say("connection reused ", times," times.")
local res, err, errno, sqlstate =
db:query("drop table if exists cats")
if not res then
ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
return
end
res, err, errno, sqlstate =
db:query("create table cats "
.. "(id serial primary key, "
.. "name varchar(5))")
if not res then
ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
return
end
ngx.say("table cats created.")
res, err, errno, sqlstate =
db:query("insert into cats (name) "
.. "values (\'Bob\'),(\'\'),(null)")
if not res then
ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
return
end
ngx.say(res.affected_rows, " rows inserted into table cats ",
"(last insert id: ", res.insert_id, ")")
-- run a select query, expected about 10 rows in
-- the result set:
res, err, errno, sqlstate =
db:query("select * from cats order by id asc", 10)
if not res then
ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
return
end
local cjson = require "cjson"
ngx.say("result: ", cjson.encode(res))
-- put it into the connection pool of size 100,
-- with 10 seconds max idle timeout
local ok, err = db:set_keepalive(10000, 100)
if not ok then
ngx.say("failed to set keepalive: ", err)
return
end
ngx.say("set_keepalive success")
';
}
}
set_keepalive return success, next code executed print zero result. Thanks.
from lua-resty-mysql.
@njentit I've tried your example on my side and it works perfectly as expected:
agentzh@w530 ~ $ curl localhost:8080/test
connected to mysql.
connection reused 0 times.
table cats created.
3 rows inserted into table cats (last insert id: 1)
result: [{"name":"Bob","id":"1"},{"name":"","id":"2"},{"name":null,"id":"3"}]
set_keepalive success
agentzh@w530 ~ $ curl localhost:8080/test
connected to mysql.
connection reused 1 times.
table cats created.
3 rows inserted into table cats (last insert id: 1)
result: [{"name":"Bob","id":"1"},{"name":"","id":"2"},{"name":null,"id":"3"}]
set_keepalive success
agentzh@w530 ~ $ curl localhost:8080/test
connected to mysql.
connection reused 2 times.
table cats created.
3 rows inserted into table cats (last insert id: 1)
result: [{"name":"Bob","id":"1"},{"name":"","id":"2"},{"name":null,"id":"3"}]
set_keepalive success
You can see that the reused times are increasing monotonically.
Maybe you have turned off the Lua code cache by configuring lua_code_cache off
in your nginx.conf
file? The cosocket connection pools require the Lua code cache to work across requests.
from lua-resty-mysql.
it working greats, sorry i missed that. Sorry for using issue as this actually not an issue, i'll use group for next question if any. I'm currently porting one of php project into lua+openresty.
Thanks for great work.
from lua-resty-mysql.
Related Issues (20)
- TODO: support specify pool_size and backlog in connect method. HOT 1
- create_db, create_user, grant and switch_user
- _recv_packet 函数 遇到错误返回,read_result并没有设置mysql. state=1 而是直接返回nil,导致下一次query失败 HOT 5
- lua entry thread aborted: runtime error: /usr/local/openresty/lualib/resty/mysql.lua:571: bad request
- 腾讯云服务器上mysql,使用lua不能访问数据库 HOT 2
- 为什么查询结果不支持返回number类型,而是将number类型也按string类型返回?
- Hardcoded query works but API disabled in the current context when using a variable HOT 3
- world.sql.gz page was not found, resulting in a CI error HOT 5
- schedule for SQL prepare statement support
- /usr/local/openresty/lualib/resty/mysql.lua:373: attempt to perform arithmetic on field 'packet_no' (a nil value)
- mysql查询是否串行 HOT 1
- bad result: Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='
- install by luarocks error HOT 3
- can not use "not res" to judge if the 'res' is empty table
- The connect function is thrown incorrectly
- using lua-resty-mysql query table contents, returned body incorrect
- connect() to unix:/var/lib/mysql/mysql.sock failed (11: Resource temporarily unavailable)
- Client does not support authentication protocol requested by server; consider upgrading MySQL client: 1251 08004 HOT 1
- Why not release the latest version on opm ?
- Connection pooling for php-fpm with nginx unix sockets and resty mysql HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lua-resty-mysql.