forked from arut/nginx-mysql-module
-
Notifications
You must be signed in to change notification settings - Fork 2
/
README
194 lines (124 loc) · 4.15 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
nginx-mysql-module
(c) 2012 Arutyunyan Roman ([email protected])
(c) 2012 DenoFiend zhao ([email protected]) (add transaction support in mysql)
== MySQL support in NGINX ==
* this module is asynchronous (non-blocking)
* this module support transaction in mysql
* this module response is generated in rds format, so it's compatible with ngx_rds_json.
* requires nginx-mtask-module (https://github.com/arut/nginx-mtask-module)
* requires standard MySQL client shared library libmysqlclient.so
* requires ngx_rds_json module(https://github.com/denofiend/rds-json-nginx-module)
NGINX configure:
./configure --add-module=/path/to/nginx-mysql-module/ --add-module=/path/to/nginx-mtask-module/ --add-module=/path/to/rds-json-nginx-module/
Usage examples:
MySQL:
USE test;
CREATE TABLE `users` (
`name` varchar(128) DEFAULT NULL,
`id` int(11) NOT NULL auto_increment, PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `a` (
`id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `b` (
`id` bigint(20) NOT NULL,
`name` varchar(128) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
nginx.conf:
http {
...
server {
...
# TCP/IP access
#mysql_host 127.0.0.1;
# unix socket access (default)
#mysql_host localhost;
#mysql_user theuser;
#mysql_password thepass;
#mysql_port theport;
mysql_database test;
mysql_connections 32;
mysql_charset utf8;
# this turns on multiple statements support
# and is REQUIRED when calling stored procedures
mysql_multi on;
mtask_stack 65536;
#transaction location conf
location /test/select {
mysql_query "SELECT a.`id`, b.`name` from a, b where a.`id` = b.`id` limit 3";
rds_json on;
rds_json_root ids;
}
location /test/insert {
mysql_transaction "BEGIN" "INSERT INTO `a` values('$arg_id')" "INSERT INTO `b` values('$arg_id', '$arg_val')" "COMMIT";
rds_json on;
}
location ~ /all {
mysql_query "SELECT name FROM users WHERE id='$arg_id'";
}
location /multi {
mysql_query "SELECT id, name FROM users ORDER BY id DESC LIMIT 10; SELECT id, name FROM users ORDER BY id ASC LIMIT 10";
}
location ~ /select.* {
if ( $arg_id ~* ^\d+$ ) {
mysql_query "SELECT name FROM users WHERE id='$arg_id'";
}
}
location ~ /insert.* {
mysql_escape $escaped_name $arg_name;
mysql_query "INSERT INTO users(name) VALUES('$escaped_name')";
}
location ~ /update.* {
mysql_query "UPDATE users SET name='$arg_name' WHERE id=$arg_id";
}
location ~ /delete.* {
mysql_query "DELETE FROM users WHERE id=$arg_id";
}
# store number of page shows per user
location ~ /register-by-cookie-show.* {
# register show in database
# need a location with this query:
# mysql_query "INSERT INTO shows(id) VALUES('$id') ON DUPLICATE KEY UPDATE count=count+1";
mysql_subrequest /update_shows_in_mysql?id=$cookie_abcdef;
# output content
rewrite ^(.*)$ /content last;
}
# spammers use big ids in picture refs to see who opened the message
location ~/spammer-style-image-show-register.* {
# need a location with this query:
# mysql_query "INSERT INTO shows(id) VALUES('$arg_id')";
mysql_subrequest /insert_show?id=$arg_id;
rewrite ^(.*)$ /content last;
}
location ~ /session.* {
# get user session
# need a location with this query:
# mysql_query "SELECT username, lastip FROM sessions WHERE id='$id'";
mysql_subrequest /select_from_mysql?id=$cookie_abcdef $username $lastip;
# go to content generator
rewrite ^(.*)$ /content?username=$username&lastip=$lastip last;
}
...
}
...
}
access:
curl 'http://localhost:8080/test/insert?id=1&val=test_case_1'
{"errcode":0}
curl 'http://localhost:8080/test/insert?id=2&val=test_case_2'
{"errcode":0}
curl 'http://localhost:8080/test/select'
{"ids":[{"id":1,"name":"test_case_1"},{"id":2,"name":"test_case_2"}]}
curl 'localhost:8080/insert?name=foo'
11
curl 'localhost:8080/select?id=11'
foo
curl 'localhost:8080/update?id=11&name=bar'
<nothing>
curl 'localhost:8080/select?id=11'
bar
curl 'localhost:8080/delete?id=11'
<nothing>
curl 'localhost:8080/select?id=11'
<nothing>