Git Product home page Git Product logo

Comments (6)

agentzh avatar agentzh commented on July 4, 2024

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

在此之前有两种现成的办法:

  1. 修改你的 MySQL 服务器端默认的连接字符集,细节见:

    http://dev.mysql.com/doc/refman/5.0/en/charset-server.html

这或者是最简单最高效的做法。

  1. 或者在你的用户 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.

agentzh avatar agentzh commented on July 4, 2024

@lenxeon 另外,请不要在这里使用中文。这里最好只使用英文。如果你想使用中文,请加入 openresty 中文邮件列表,请见:http://openresty.org/#Community

from lua-resty-mysql.

lenxeon avatar lenxeon commented on July 4, 2024

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.

agentzh avatar agentzh commented on July 4, 2024

@lenxeon Yes, sure. I meant to merge that pull request after some polishing :)

from lua-resty-mysql.

pen886 avatar pen886 commented on July 4, 2024

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.

agentzh avatar agentzh commented on July 4, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.