You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In contrast to node-mysql, `zeroFill` flag is ignored in type conversion.
30
-
You need to check corresponding field zeroFill flag and convert to string manually if this is of importance to you.
31
-
32
-
DECIMAL and NEWDECIMAL types always returned as string unless you pass this config option:
33
-
```javascript
34
-
{
35
-
decimalNumbers:true
36
-
}
37
-
```
38
-
**_Warning this option could lose precision on the number as Javascript Number is a Float!_**
39
-
40
-
## Examples
41
-
42
-
Simple select:
43
-
44
-
```js
45
-
var mysql =require('mysql2');
46
-
var connection =mysql.createConnection({ user:'test', database:'test'});
47
-
48
-
connection.query('SELECT 1+1 as test1', function(err, rows) {
49
-
//
50
-
});
51
-
```
52
-
53
-
Prepared statement and parameters:
54
-
55
-
```js
56
-
var mysql =require('mysql2');
57
-
var connection =mysql.createConnection({ user:'test', database:'test'});
58
-
59
-
connection.execute('SELECT 1+? as test1', [10], function(err, rows) {
60
-
//
61
-
});
62
-
```
63
-
64
-
Connecting over encrypted connection:
65
-
66
-
```js
67
-
var fs =require('fs');
68
-
var mysql =require('mysql2');
69
-
var connection =mysql.createConnection({
70
-
user:'test',
71
-
database:'test',
72
-
ssl: {
73
-
key:fs.readFileSync('./certs/client-key.pem'),
74
-
cert:fs.readFileSync('./certs/client-cert.pem')
75
-
}
76
-
});
77
-
connection.query('SELECT 1+1 as test1', console.log);
78
-
```
79
-
80
-
You can use 'Amazon RDS' string as value to ssl property to connect to Amazon RDS mysql over ssl (in that case http://s3.amazonaws.com/rds-downloads/mysql-ssl-ca-cert.pem CA cert is used)
81
-
82
-
```js
83
-
var mysql =require('mysql2');
84
-
var connection =mysql.createConnection({
85
-
user:'foo',
86
-
password:'bar',
87
-
host:'db.id.ap-southeast-2.rds.amazonaws.com',
88
-
ssl:'Amazon RDS'
89
-
});
90
-
91
-
conn.query('show status like \'Ssl_cipher\'', function(err, res) {
92
-
console.log(err, res);
93
-
conn.end();
94
-
});
95
-
```
96
-
97
-
98
-
Simple mysql proxy server:
99
-
100
-
```js
101
-
var mysql =require('mysql2');
102
-
103
-
var server =mysql.createServer();
104
-
server.listen(3307);
105
-
server.on('connection', function(conn) {
106
-
console.log('connection');
18
+
- Prepared Statements
19
+
- Promise Wrapper
20
+
- Authentication Switch
21
+
- MySQL Server (API and Mocks)
22
+
- Pooling
23
+
- SSL
24
+
- MySQL Compression
25
+
- Binary Log Protocol Client
107
26
108
-
conn.serverHandshake({
109
-
protocolVersion:10,
110
-
serverVersion:'node.js rocks',
111
-
connectionId:1234,
112
-
statusFlags:2,
113
-
characterSet:8,
114
-
capabilityFlags:0xffffff
115
-
});
116
-
117
-
conn.on('field_list', function(table, fields) {
118
-
console.log('field list:', table, fields);
119
-
conn.writeEof();
120
-
});
121
-
122
-
var remote =mysql.createConnection({user:'root', database:'dbname', host:'server.example.com', password:'secret'});
123
-
124
-
conn.on('query', function(sql) {
125
-
console.log('proxying query:'+ sql);
126
-
remote.query(sql, function(err) { // overloaded args, either (err, result :object)
127
-
// or (err, rows :array, columns :array)
128
-
if (Array.isArray(arguments[1])) {
129
-
// response to a 'select', 'show' or similar
130
-
var rows =arguments[1], columns =arguments[2];
131
-
console.log('rows', rows);
132
-
console.log('columns', columns);
133
-
conn.writeTextResult(rows, columns);
134
-
} else {
135
-
// response to an 'insert', 'update' or 'delete'
136
-
var result =arguments[1];
137
-
console.log('result', result);
138
-
conn.writeOk(result);
139
-
}
140
-
});
141
-
});
142
-
143
-
conn.on('end', remote.end.bind(remote));
144
-
});
145
-
```
146
-
## MySQL Server API
147
-
148
-
### Server
149
-
150
-
***createServer()** - creates server instance
151
-
***Server.listen** - listen port / unix socket (same arguments as [net.Server.listen](http://nodejs.org/api/net.html#net_server_listen_port_host_backlog_callback))
152
-
153
-
events:
154
-
155
-
***connect** - new incoming connection.
156
-
157
-
### Connection
158
-
159
-
***serverHandshake({serverVersion, protocolVersion, connectionId, statusFlags, characterSet, capabilityFlags})** - send server handshake initialisation packet, wait handshake response and start listening for commands
160
-
***writeOk({affectedRows: num, insertId: num})** - send [OK packet](http://dev.mysql.com/doc/internals/en/overview.html#packet-OK_Packet) to client
***writeTextRow(row)** - write array (not hash!) of values as result row
165
-
* TODO: binary protocol
166
-
167
-
events:
168
-
169
-
Every command packet received by the server will be emitted as a **packet** event with the parameters:
170
-
171
-
* packet: Packet - The packet itself
172
-
* knownCommand: boolean - is this command known to the server
173
-
* commandCode: number - the parsed command code (first byte)
174
-
175
-
In addition special events are emitted for [commands](https://dev.mysql.com/doc/internals/en/text-protocol.html) received from the client. If no listener is present a fallback behavior will be invoked.
176
-
177
-
***quit**() - Default: close the connection
178
-
***init_db**(schemaName: string) - Default: return OK
179
-
***query**(sql: string) - Please attach a listener to this. Default: return HA_ERR_INTERNAL_ERROR
Please check [documentation](https://github.com/sidorares/node-mysql2/tree/master/documentation) to get started.
186
28
187
29
## Acknowledgements
188
30
189
-
- Internal protocol is written from scratch using my experience with [mysql-native](https://github.com/sidorares/nodejs-mysql-native)
190
-
-constants, sql parameters interpolation, pool, connection config class taken from [node-mysql](https://github.com/felixge/node-mysql) (I tried to preserve git history)
31
+
- Internal protocol is written by @sidorares[MySQL-native](https://github.com/sidorares/nodejs-mysql-native)
32
+
-Constants, SQL parameters interpolation, Pooling, `ConnectionConfig`class taken from [node-mysql](https://github.com/felixge/node-mysql)
191
33
- SSL upgrade code based on @TooTallNate[code](https://gist.github.com/TooTallNate/848444)
192
-
- Secure connection / compressed connection api flags compatible to [mariasql](https://github.com/mscdex/node-mariasql/) client.
var connection =mysql.createConnection({ user:'test', database:'test'});
8
+
9
+
connection.query('SELECT 1+1 as test1', function(err, rows) {
10
+
//
11
+
});
12
+
```
13
+
14
+
## Prepared statement and parameters
15
+
16
+
```js
17
+
var mysql =require('mysql2');
18
+
var connection =mysql.createConnection({ user:'test', database:'test'});
19
+
20
+
connection.execute('SELECT 1+? as test1', [10], function(err, rows) {
21
+
//
22
+
});
23
+
```
24
+
25
+
## Connecting over encrypted connection
26
+
27
+
```js
28
+
var fs =require('fs');
29
+
var mysql =require('mysql2');
30
+
var connection =mysql.createConnection({
31
+
user:'test',
32
+
database:'test',
33
+
ssl: {
34
+
key:fs.readFileSync('./certs/client-key.pem'),
35
+
cert:fs.readFileSync('./certs/client-cert.pem')
36
+
}
37
+
});
38
+
connection.query('SELECT 1+1 as test1', console.log);
39
+
```
40
+
41
+
You can use 'Amazon RDS' string as value to ssl property to connect to Amazon RDS mysql over ssl (in that case http://s3.amazonaws.com/rds-downloads/mysql-ssl-ca-cert.pem CA cert is used)
42
+
43
+
```js
44
+
var mysql =require('mysql2');
45
+
var connection =mysql.createConnection({
46
+
user:'foo',
47
+
password:'bar',
48
+
host:'db.id.ap-southeast-2.rds.amazonaws.com',
49
+
ssl:'Amazon RDS'
50
+
});
51
+
52
+
conn.query('show status like \'Ssl_cipher\'', function(err, res) {
53
+
console.log(err, res);
54
+
conn.end();
55
+
});
56
+
```
57
+
58
+
59
+
## Simple MySQL proxy server
60
+
61
+
```js
62
+
var mysql =require('mysql2');
63
+
64
+
var server =mysql.createServer();
65
+
server.listen(3307);
66
+
server.on('connection', function(conn) {
67
+
console.log('connection');
68
+
69
+
conn.serverHandshake({
70
+
protocolVersion:10,
71
+
serverVersion:'node.js rocks',
72
+
connectionId:1234,
73
+
statusFlags:2,
74
+
characterSet:8,
75
+
capabilityFlags:0xffffff
76
+
});
77
+
78
+
conn.on('field_list', function(table, fields) {
79
+
console.log('field list:', table, fields);
80
+
conn.writeEof();
81
+
});
82
+
83
+
var remote =mysql.createConnection({user:'root', database:'dbname', host:'server.example.com', password:'secret'});
84
+
85
+
conn.on('query', function(sql) {
86
+
console.log('proxying query:'+ sql);
87
+
remote.query(sql, function(err) { // overloaded args, either (err, result :object)
88
+
// or (err, rows :array, columns :array)
89
+
if (Array.isArray(arguments[1])) {
90
+
// response to a 'select', 'show' or similar
91
+
var rows =arguments[1], columns =arguments[2];
92
+
console.log('rows', rows);
93
+
console.log('columns', columns);
94
+
conn.writeTextResult(rows, columns);
95
+
} else {
96
+
// response to an 'insert', 'update' or 'delete'
97
+
var result =arguments[1];
98
+
console.log('result', result);
99
+
conn.writeOk(result);
100
+
}
101
+
});
102
+
});
103
+
104
+
conn.on('end', remote.end.bind(remote));
105
+
});
106
+
```
107
+
108
+
## Examples using MySQL server API
109
+
110
+
-[MySQL-pg-proxy](https://github.com/sidorares/mysql-pg-proxy) - MySQL to Postgres proxy server.
111
+
-[MySQLite.js](https://github.com/sidorares/mysqlite.js) - MySQL server with JS-only (emscripten compiled) sqlite backend.
112
+
-[SQL-engine](https://github.com/eugeneware/sql-engine) - MySQL server with LevelDB backend.
113
+
-[MySQL-osquery-proxy](https://github.com/sidorares/mysql-osquery-proxy) - Connect to [facebook osquery](https://osquery.io/) using MySQL client
114
+
-[PlyQL](https://github.com/implydata/plyql) - Connect to [Druid](http://druid.io/) using MySQL client
***writeTextRow(row)** - write array (not hash!) of values as result row
20
+
* TODO: binary protocol
21
+
22
+
#### Events
23
+
24
+
Every command packet received by the server will be emitted as a **packet** event with the parameters:
25
+
26
+
* packet: Packet - The packet itself
27
+
* knownCommand: boolean - is this command known to the server
28
+
* commandCode: number - the parsed command code (first byte)
29
+
30
+
In addition special events are emitted for [commands](https://dev.mysql.com/doc/internals/en/text-protocol.html) received from the client. If no listener is present a fallback behavior will be invoked.
31
+
32
+
***quit**() - Default: close the connection
33
+
***init_db**(schemaName: string) - Default: return OK
34
+
***query**(sql: string) - Please attach a listener to this. Default: return HA_ERR_INTERNAL_ERROR
0 commit comments