Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

677 lines (463 sloc) 30.055 kB
= Name =
drizzle-nginx-module - Upstream module for talking to MySQL and Drizzle directly
''This module is not distributed with the Nginx source.'' See [[#Installation|the installation instructions]].
= Status =
This module is already production ready and is powering [ the Taobao LineZing site].
= Version =
This document describes ngx_drizzle [ v0.1.4] released on 17 August 2012.
= Synopsis =
<geshi lang="nginx">
http {
upstream cluster {
# simple round-robin
drizzle_server dbname=test
password=some_pass user=monty protocol=mysql;
drizzle_server dbname=test2
password=pass user=bob protocol=drizzle;
upstream backend {
drizzle_server dbname=test
password=some_pass user=monty protocol=mysql;
server {
location /mysql {
set $my_sql 'select * from cats';
drizzle_query $my_sql;
drizzle_pass backend;
drizzle_connect_timeout 500ms; # default 60s
drizzle_send_query_timeout 2s; # default 60s
drizzle_recv_cols_timeout 1s; # default 60s
drizzle_recv_rows_timeout 1s; # default 60s
# for connection pool monitoring
location /mysql-pool-status {
deny all;
= Description =
This is an nginx upstream module integrating [ libdrizzle] into Nginx in a non-blocking and streamming way.
Essentially it provides a very efficient and flexible way for nginx internals to access MySQL, Drizzle, as well as other RDBMS's that support the Drizzle or MySQL wired protocol. Also it can serve as a direct REST interface to those RDBMS backends.
This module does not generate human-readable outputs, rather, in a binary format called Resty-DBD-Stream (RDS) designed by ourselves. You usually need other components, like [[HttpRdsJsonModule]], [[HttpRdsCsvModule]], or [[LuaRdsParser]], to work with this module. See [[#Output Format|Output Format]] for details.
== Keepalive connection pool ==
This module also provides a builtin per-worker connection pool mechanism for MySQL or Drizzle TCP connections.
Here's a sample configuration:
<geshi lang="nginx">
upstream backend {
drizzle_server dbname=test
password=some_pass user=monty protocol=mysql;
drizzle_keepalive max=100 mode=single overflow=reject;
For now, the connection pool uses a simple LIFO algorithm to assign idle connections in the pool. That is, most recently (successfully) used connections will be reused first the next time. And new idle connections will always replace the oldest idle connections in the pool even if the pool is already full.
See the [[#drizzle_keepalive|drizzle_keepalive]] directive for more details.
== Last Insert ID ==
If you want to get LAST_INSERT_ID, then ngx_drizzle already returns that automatically for you when you're doing a SQL insert query. Consider the following sample <code>nginx.conf</code> snippet:
<geshi lang="nginx">
location /test {
echo_location /mysql "drop table if exists foo";
echo_location /mysql "create table foo (id serial not null, primary key (id), val real);";
echo_location /mysql "insert into foo (val) values (3.1415926);";
echo_location /mysql "select * from foo;";
location /mysql {
drizzle_pass backend;
drizzle_module_header off;
drizzle_query $query_string;
rds_json on;
Then request <code>GET /test</code> gives the following outputs:
<geshi lang="javascript">
You can see the <code>insert_id</code> field (as well as the <code>affected_rows</code> field in the 3rd JSON response.
= Directives =
== drizzle_server ==
'''syntax:''' ''drizzle_server <host> user=<user> password=<pass> dbname=<database>''
'''syntax:''' ''drizzle_server <host>:<port> user=<user> password=<pass> dbname=<database> protocol=<protocol> charset=<charset>''
'''default:''' ''no''
'''context:''' ''upstream''
Directive assigns the name and the parameters of server. For the name it is possible to use a domain name, an address, with an optional port (default: 3306). If domain name resolves to several addresses, then all are used.
The following options are supported:
: MySQL/Drizzle user name <code><user></code> for login.
: Specify mysql password <code><pass></code>for login. If you have special characters like <code>#</code> or spaces in your password text, then you'll have to quote the whole key-value pair with either single-quotes or double-quotes, as in
<geshi lang="nginx">
drizzle_server user=monty "password=a b#1"
dbname=test protocol=mysql;
: Specify default MySQL database <code><database></code> for the connection. Note that MySQL does allow referencing tables belonging to different databases by qualifying table names with database names in SQL queries.
: Specify which wire protocol to use, <code>drizzle</code> or <code>mysql</code>. Default to <code>drizzle</code>.
: Explicitly specify the character set for the MySQL connections. Setting this option to a non-empty value will make this module send out a <code>set names '<charset>'</code> query right after the mysql connection is established.
: If the default character encoding of the MySQL connection is already what you want, you needn't set this option because it has extra runtime cost.
: Here is a small example:
<geshi lang="nginx">
drizzle_server user=monty password=some_pass
dbname=test protocol=mysql
Please note that for the mysql server, "utf-8" is not a valid encoding name while <code>utf8</code> is.
== drizzle_keepalive ==
'''syntax:''' ''drizzle_keepalive max=<size> mode=<mode>''
'''default:''' ''drizzle_keepalive max=0 mode=single''
'''context:''' ''upstream''
Configures the keep-alive connection pool for MySQL/Drizzle connections.
The following options are supported:
: Specify the capacity of the connection pool for the current upstream block. The <num> value ''must'' be non-zero. If set to <code>0</code>, it effectively disables the connection pool. This option is default to <code>0</code>.
: This supports two values, <code>single</code> and <code>multi</code>. The <code>single</code> mode means the pool does not distinguish various drizzle servers in the current upstream block while <code>multi</code> means the pool will merely reuse connections which have identical server host names and ports. Note that even under <code>multi</code>, differences between <code>dbname</code> or <code>user</code> parameters will be silently ignored. Default to <code>single</code>.
: This option specifies what to do when the connection pool is already full while new database connection is required. Either <code>reject</code> or <code>ignore</code> can be specified. In case of <code>reject</code>, it will reject the current request, and returns the <code>503 Service Unavailable</code> error page. For <code>ignore</code>, this module will go on creating a new database connection.
== drizzle_query ==
'''syntax:''' ''drizzle_query <sql>''
'''default:''' ''no''
'''context:''' ''http, server, location, location if''
Specify the SQL queries sent to the Drizzle/MySQL backend.
Nginx variable interpolation is supported, but you must be careful with SQL injection attacks. You can use the [[HttpSetMiscModule#set_quote_sql_str|set_quote_sql_str]] directive, for example, to quote values for SQL interpolation:
<geshi lang="nginx">
location /cat {
set_unescape_uri $name $arg_name;
set_quote_sql_str $quoted_name $name;
drizzle_query "select * from cats where name = $quoted_name";
drizzle_pass my_backend;
== drizzle_pass ==
'''syntax:''' ''drizzle_pass <remote>''
'''default:''' ''no''
'''context:''' ''location, location if''
'''phase:''' ''content''
This directive specifies the Drizzle or MySQL upstream name to be queried in the current location. The <code><remote></code> argument can be any upstream name defined with the [[#drizzle_server|drizzle_server]] directive.
Nginx variables can also be interpolated into the <code><remote></code> argument, so as to do dynamic backend routing, for example:
<geshi lang="nginx">
upstream moon { drizzle_server ...; }
server {
location /cat {
set $backend 'moon';
drizzle_query ...;
drizzle_pass $backend;
== drizzle_connect_timeout ==
'''syntax:''' ''drizzle_connect_time <time>''
'''default:''' ''drizzle_connect_time 60s''
'''context:''' ''http, server, location, location if''
Specify the (total) timeout for connecting to a remote Drizzle or MySQL server.
The <code><time></code> argument can be an integer, with an optional time unit, like <code>s</code> (second), <code>ms</code> (millisecond), <code>m</code> (minute). The default time unit is <code>s</code>, i.e., "second". The default setting is <code>60s</code>.
== drizzle_send_query_timeout ==
'''syntax:''' ''drizzle_send_query_timeout <time>''
'''default:''' ''drizzle_send_query_timeout 60s''
'''context:''' ''http, server, location, location if''
Specify the (total) timeout for sending a SQL query to a remote Drizzle or MySQL server.
The <code><time></code> argument can be an integer, with an optional time unit, like <code>s</code> (second), <code>ms</code> (millisecond), <code>m</code> (minute). The default time unit is <code>s</code>, ie, "second". The default setting is <code>60s</code>.
== drizzle_recv_cols_timeout ==
'''syntax:''' ''drizzle_recv_cols_timeout <time>''
'''default:''' ''drizzle_recv_cols_timeout 60s''
'''context:''' ''http, server, location, location if''
Specify the (total) timeout for receiving the columns metadata of the result-set to a remote Drizzle or MySQL server.
The <code><time></code> argument can be an integer, with an optional time unit, like <code>s</code> (second), <code>ms</code> (millisecond), <code>m</code> (minute). The default time unit is <code>s</code>, ie, "second". The default setting is <code>60s</code>.
== drizzle_recv_rows_timeout ==
'''syntax:''' ''drizzle_recv_rows_timeout <time>''
'''default:''' ''drizzle_recv_rows_timeout 60s''
'''context:''' ''http, server, location, location if''
Specify the (total) timeout for receiving the rows data of the result-set (if any) to a remote Drizzle or MySQL server.
The <code><time></code> argument can be an integer, with an optional time unit, like <code>s</code> (second), <code>ms</code> (millisecond), <code>m</code> (minute). The default time unit is <code>s</code>, ie, "second". The default setting is <code>60s</code>.
== drizzle_buffer_size ==
'''syntax:''' ''drizzle_buffer_size <size>''
'''default:''' ''drizzle_buffer_size 4k/8k''
'''context:''' ''http, server, location, location if''
Specify the buffer size for drizzle outputs. Default to the page size (4k/8k). The larger the buffer, the less streammy the outputing process will be.
== drizzle_module_header ==
'''syntax:''' ''drizzle_module_header on|off''
'''default:''' ''drizzle_module_header on''
'''context:''' ''http, server, location, location if''
Controls whether to output the drizzle header in the response. Default on.
The drizzle module header looks like this:
<geshi lang="text">
X-Resty-DBD-Module: ngx_drizzle 0.1.0
== drizzle_status ==
'''syntax:''' ''drizzle_status''
'''default:''' ''no''
'''context:''' ''location, location if''
'''phase:''' ''content''
When specified, the current Nginx location will output a status report for all the drizzle upstream servers in the virtual server of the current Nginx worker process.
The output looks like this:
<geshi lang="text">
worker process: 15231
upstream backend
active connections: 0
connection pool capacity: 10
overflow: reject
cached connection queue: 0
free'd connection queue: 10
cached connection successfully used count:
free'd connection successfully used count: 3 0 0 0 0 0 0 0 0 0
servers: 1
peers: 1
upstream backend2
active connections: 0
connection pool capacity: 0
servers: 1
peers: 1
Note that, this is ''not'' the global statistics if you do have multiple Nginx worker processes configured in your <code>nginx.conf</code>.
= Variables =
This module creates the following Nginx variables:
== $drizzle_thread_id ==
This variable will be assigned a textual number of the underlying MySQL or Drizzle query thread ID when the current SQL query times out. This thread ID can be further used in a SQL kill command to cancel the timed-out query.
Here's an example:
<geshi lang="nginx">
drizzle_connect_timeout 1s;
drizzle_send_query_timeout 2s;
drizzle_recv_cols_timeout 1s;
drizzle_recv_rows_timeout 1s;
location /query {
drizzle_query 'select sleep(10)';
drizzle_pass my_backend;
rds_json on;
more_set_headers -s 504 'X-Mysql-Tid: $drizzle_thread_id';
location /kill {
drizzle_query "kill query $arg_tid";
drizzle_pass my_backend;
rds_json on;
location /main {
content_by_lua '
local res = ngx.location.catpure("/query")
if res.status ~= ngx.HTTP_OK then
local tid = res.header["X-Mysql-Tid"]
if tid and tid ~= "" then
ngx.location.capture("/kill", { args = {tid = tid} })
where we make use of [[HttpHeadersMoreModule]], [[HttpLuaModule]], and [[HttpRdsJsonModule]] too. When the SQL query timed out, we'll explicitly cancel it immediately. One pitfall here is that you have to add these modules in this order while building Nginx:
* [[HttpLuaModule]]
* [[HttpHeadersMoreModule]]
* [[HttpRdsJsonModule]]
Such that, their output filters will work in the ''reversed'' order, i.e., first convert RDS to JSON, and then add our <code>X-Mysql-Tid</code> custom header, and finally capture the whole (subrequest) response with the Lua module. You're recommended to use the [ OpenResty bundle] though, it ensures the module building order automatically for you.
= Output Format =
This module generates binary query results in a format that is shared among the various Nginx database driver modules like [ ngx_postgres]. This data format is named <code>Resty DBD Stream</code> (RDS).
If you're a web app developer, you may be more interested in
* using [[HttpRdsJsonModule]] to obtain JSON output,
* using [[HttpRdsCsvModule]] to obain Comma-Separated-Value (CSV) output,
* or using [[LuaRdsParser]] to parse the RDS data into Lua data structures.
For the HTTP response header part, the <code>200 OK</code> status code should always be returned. The <code>Content-Type</code> header ''must'' be set to <code>application/x-resty-dbd-stream</code>. And the driver generating this response also sets a <code>X-Resty-DBD</code> header. For instance, this module adds the following output header:
<geshi lang="text">
X-Resty-DBD-Module: drizzle 0.1.0
where <code>0.1.0</code> is this module's own version number. This <code>X-Resty-DBD-Module</code> header is optional though.
Below is the HTTP response body format (version 0.0.3):
== RDS Header Part ==
The RDS Header Part consists of the following fields:
: endian type (1 means big-endian and little endian otherwise)
: format version (v1.2.3 is represented as 1002003 in decimal)
: result type (0 means normal SQL result type, fixed for now)
: standard error code
: driver-specific error code
: driver-specific error string length
'''u_char *'''
: driver-specific error string data
: database rows affected
: insert id (if none, 0)
: column count
== RDS Body Part ==
When the <code>column count</code> field in the [[#RDS Header Part|RDS Header Part]] is zero, then the whole RDS Body Part is omitted.
The RDS Body Part consists of two sections, [[#Columns|Columns]] and [[#Rows|Rows]].
=== Columns ===
The columns part consists of zero or more column data. The number of columns is determined by <code>column count</code> field in [[#RDS Header Part|RDS Header Part]].
Each column consists of the following fields
: non-zero value for standard column type code and for the column list terminator and zero otherwise.
: driver-specific column type code
: column name length
'''u_char *'''
: column name data
=== Rows ===
The rows part consists of zero or more row data, terminated by a 8-bit zero.
Each row data consists of a [[#Row Flag|Row Flag]] and an optional [[#Fields Data|Fields Data]] part.
==== Row Flag ====
: valid row (1 means valid, and 0 means the row list terminator)
==== Fields Data ====
The Fields Data consists zero or more fields of data. The field count is predetermined by the <code>column number</code) specified in [[#RDS Header Part|RDS Header Part]].
: field length ((uint32_t) -1 represents NULL)
'''u_char *'''
: field data in textual representation), is empty (0) if field length == (uint32_t) -1
== RDS buffer Limitations ==
On the nginx output chain link level, the following components should be put into a single <code>ngx_buf_t</code> struct:
* the header
* each column and the column list terminator
* each row's valid flag byte and row list terminator
* each field in each row (if any) but the field data can span multiple bufs.
= Status Code =
If the MySQL error code in MySQL's query result is not OK, then a 500 error page is returned by this module, except for the table non-existent error, which results in the <code>410 Gone</code> error page.
= Caveats =
* Other usptream modules like <code>upstream_hash</code> and [[HttpUpstreamKeepaliveModule]] ''must not'' be used with this module in a single upstream block.
* Directives like [[HttpUpstreamModule#server|server]] ''must not'' be mixed with [[#drizzle_server|drizzle_server]] either.
* Upstream backends that don't use [[#drizzle_server|drizzle_server]] to define server entries ''must not'' be used in the [[#drizzle_pass|drizzle_pass]] directive.
= Trouble Shooting =
* When you see the following error message in <code>error.log</code>:
<geshi lang="text">
failed to connect: 15: drizzle_state_handshake_result_read:
old insecure authentication mechanism not supported in upstream, ...
: then you may checkout if your MySQL is too old (at least 5.x is required) or your mysql config file explicitly forces the use of old authentication method (you should remove the <code>old-passwords</code> line from your <code>my.cnf</code> and add the line <code>secure_auth 1</code>).
* When you see the following error message in <code>error.log</code>:
<geshi lang="text">
failed to connect: 23: Access denied for user 'root'@'ubuntu'
(using password: YES) while connecting to drizzle upstream, ...
: You should check if your MySQL account does have got TCP login access on your MySQL server side. A quick check is to use MySQL's official client to connect to your server:
<geshi lang="bash">
mysql --protocol=tcp -u user --password=password -h dbname
: Note that the <code>--protocol=tcp</code> option is required here, or your MySQL client may use Unix Domain Socket to connect to your MySQL server.
= Known Issues =
* Calling mysql procedures are currently not supported because the underlying libdrizzle library does not support the <code>CLIENT_MULTI_RESULTS</code> flag yet :( But we'll surely work on it.
* Multiple SQL statements in a single query are not supported due to the lack of <code>CLIENT_MULTI_STATEMENTS</code> support in the underlying libdrizzle library.
* This module does not (yet) work with the <code>RTSIG</code> event model.
= Installation =
You're recommended to install this module as well as [[HttpRdsJsonModule]] via the ngx_openresty bundle:
The installation steps are usually as simple as <code>./configure --with-http_drizzle_module && make && make install</code> (But you still need to install the libdrizzle library manually, see [] for detailed instructions.
Alternatively, you can compile this module with Nginx core's source by hand:
* You should first install libdrizzle 1.0 which is now distributed with the drizzle project and can be obtained from []. The latest drizzle7 release does not support building libdrizzle 1.0 separately and requires a lot of external dependencies like Boost and Protobuf which are painful to install. The last version supporting building libdrizzle 1.0 separately is <code>2011.07.21</code>. You can download it from . Which this version of drizzle7, installation of libdrizzle 1.0 is usually as simple as
<geshi lang="nginx">
tar xzvf drizzle7-2011.07.21.tar.gz
cd drizzle7-2011.07.21/
./configure --without-server
make libdrizzle-1.0
make install-libdrizzle-1.0
: Ensure that you have the <code>python</code> command point to a <code>python2</code> interpreter. It's known that on recent : Arch Linux distribution, <code>python</code> is linked to <code>python3</code> by default, and while running <code>make libdrizzle-1.0</code> will yield the error
<geshi lang="bash">
File "config/pandora-plugin", line 185
print "Dependency loop detected with %s" % plugin['name']
SyntaxError: invalid syntax
make: *** [.plugin.scan] Error 1
: You can fix this by pointing <code>python</code> to <code>python2</code>.
* Download the latest version of the release tarball of this module from drizzle-nginx-module [ file list].
* Grab the nginx source code from [], for example, the version 1.2.3 (see [[#Compatibility|nginx compatibility]]), and then build the source with this module:
<geshi lang="bash">
wget ''
tar -xzvf nginx-1.2.3.tar.gz
cd nginx-1.2.3/
# if you have installed libdrizzle to the prefix /opt/drizzle, then
# specify the following environments:
# export LIBDRIZZLE_INC=/opt/drizzle/include/libdrizzle-1.0
# export LIBDRIZZLE_LIB=/opt/drizzle/lib
# Here we assume you would install you nginx under /opt/nginx/.
./configure --prefix=/opt/nginx \
make -j2
make install
You usually also need [[HttpRdsJsonModule]] to obtain JSON output from the binary RDS output generated by this upstream module.
= Compatibility =
If you're using MySQL, then MySQL <code>5.0 ~ 5.5</code> is required. We're not sure if MySQL <code>5.6+</code> work; reports welcome!
This module has been tested on Linux and Mac OS X. Reports on other POSIX-compliant systems will be highly appreciated.
The following versions of Nginx should work with this module:
* 1.3.x (last tested: 1.3.4)
* 1.2.x (last tested: 1.2.3)
* 1.1.x (last tested: 1.1.5)
* 1.0.x (last tested: 1.0.8)
* 0.8.x (last tested: 0.8.55)
* 0.7.x >= 0.7.44 (last tested version is 0.7.67)
Earlier versions of Nginx like <code>0.6.x</code> and <code>0.5.x</code> will ''not'' work.
If you find that any particular version of Nginx above <code>0.7.44</code> does not work with this module, please consider reporting a bug.
= Community =
== English Mailing List ==
The [ openresty-en] mailing list is for English speakers.
== Chinese Mailing List ==
The [ openresty] mailing list is for Chinese speakers.
= Report Bugs =
Please submit bug reports, wishlists, or patches by
# creating a ticket on the [ issue tracking interface] provided by GitHub,
# or sending an email to the [[#Community|OpenResty community]].
= Source Repository =
Available on github at [ chaoslawful/drizzle-nginx-module].
= Test Suite =
This module comes with a Perl-driven test suite. The [ test cases] are
[ declarative] too. Thanks to the [ Test::Nginx] module in the Perl world.
To run it on your side:
<geshi lang="bash">
$ PATH=/path/to/your/nginx-with-echo-module:$PATH prove -r t
Because a single nginx server (by default, <code>localhost:1984</code>) is used across all the test scripts (<code>.t</code> files), it's meaningless to run the test suite in parallel by specifying <code>-jN</code> when invoking the <code>prove</code> utility.
= TODO =
* add the MySQL transaction support.
* add multi-statement MySQL query support.
* implement the "drizzle_max_output_size" directive. When the RDS data is larger then the size specified, the module will try to terminate the output as quickly as possible but will still ensure the resulting response body is still in valid RDS format.
* implement the <code>drizzle_upstream_next</code> mechanism for failover support.
* add support for multiple "drizzle_query" directives in a single location.
* implement ''weighted'' round-robin algorithm for the upstream server clusters.
* add the <code>max_idle_time</code> option to the [[#drizzle_server|drizzle_server]] directive, so that the connection pool will automatically release idle connections for the timeout you specify.
* add the <code>min</code> option to the "drizzle_server" directive so that the connection pool will automatically create that number of connections and put them into the pool.
* add Unix domain socket support in the <code>drizzle_server</code> directive.
* make the [[#drizzle_query|drizzle_query]] directive reject variables that have not been processed by a [[#drizzle_process|drizzle_process]] directive. This will pretect us from SQL injections. There will also be an option ("strict=no") to disable such checks.
= Changes =
The changes of every release of this module can be obtained from the ngx_openresty bundle's change logs:
= Authors =
* chaoslawful (王晓哲) <chaoslawful at gmail dot com>
* Yichun "agentzh" Zhang (章亦春) <agentzh at gmail dot com>
* Piotr Sikora <piotr.sikora at frickle dot com>
= Copyright & License =
This module is licenced under the BSD license.
Copyright (C) 2009-2012, by Xiaozhe Wang (chaoslawful) <>.
Copyright (C) 2009-2012, by Yichun "agentzh" Zhang (章亦春) <>.
Copyright (C) 2010-2012, by FRiCKLE Piotr Sikora <>.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
= See Also =
* [[HttpRdsJsonModule]]
* [[HttpRdsCsvModule]]
* [[LuaRdsParser]]
* [ The ngx_openresty bundle]
* [ DrizzleNginxModule bundled by ngx_openresty]
* [ postgres-nginx-module]
* [[HttpLuaModule]]
* The [ lua-resty-mysql] library based on the [[HttpLuaModule]] cosocket API.
Jump to Line
Something went wrong with that request. Please try again.