Comments (6)
Hello!
On Fri, Oct 31, 2014 at 1:03 AM, lenxeon wrote:
local ok,err,errno,sqlstate = db:connect{
host = "127.0.0.1",
port = 3306,
database = "db_blog",
user = "root",
password = "admin",
max_package_size = 1024,
charset = "utf-8"
lua-resty-mysql 库的 connect 方法尚不支持 charset 选项,使用的就是 mysqld
服务器端的默认字符集,可以参见官方文档(https://github.com/openresty/lua-resty-mysql )。
不过有一个有待合并的补丁实现了该 charset 选项,可以覆盖 mysqld 服务器的默认字符集设置:
https://github.com/openresty/lua-resty-mysql/pull/10
在此之前有两种现成的办法:
-
修改你的 MySQL 服务器端默认的连接字符集,细节见:
这或者是最简单最高效的做法。
- 或者在你的用户 Lua 代码中(即在客户端),使用 query() 方法自己发送“SET NAMES 'utf8';”这个查询。细节见
http://dev.mysql.com/doc/refman/5.0/en/charset-syntax.html
当然,对于办法 2,由于 MySQL 连接池的存在,你只需要在第一个创建连接时发送该查询就可以了。具体做法是通过
get_reused_times() 这个调用检查连接是否被复用过:
https://github.com/openresty/lua-resty-mysql#get_reused_times
这种做法不需要修改 MySQL 服务器端的配置。
Regards,
-agentzh
from lua-resty-mysql.
@lenxeon 另外,请不要在这里使用中文。这里最好只使用英文。如果你想使用中文,请加入 openresty 中文邮件列表,请见:http://openresty.org/#Community
from lua-resty-mysql.
ok, thank you.
i changed my.conf to resolve it. but i think support charset in lua-resty-mysql my be a good idea
from lua-resty-mysql.
@lenxeon Yes, sure. I meant to merge that pull request after some polishing :)
from lua-resty-mysql.
support charset:
[code]:
db:query("SET NAMES utf8")
res,err,errno,sqlstate = db:query("select * from wp_terms")
if not res then
ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
return
end
from lua-resty-mysql.
@pen886 MySQL's charset setting is a mess. Even if you have UTF-8 encoded table, the default character encoding used on the MySQL connections can still be something else. The simplest and most efficient way is to add the following lines under the [mysqld]
section in your MySQL server's my.cnf
file and reload the database:
character-set-server=utf8
collation-server=utf8_general_ci
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
- Test case failures on rhel 7.6 ppc64le platform HOT 1
- 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.