-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
37 lines (35 loc) · 1.18 KB
/
database.js
File metadata and controls
37 lines (35 loc) · 1.18 KB
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
/**
* Created by Administrator on 2016/2/29.
*/
var mysql = require('mysql'),
settings = require('../settings');
module.exports.getConnection = function () {
if ((module.exports.connection) && (module.exports.connection.socket) && (module.exports.connection._socket.readable) && (module.exports.connection._socket.writable)) {
return module.exports.connection;
}
console.log(((module.exports.connection) ? "UNHEALTHY SQL CONNECTION; RE" : "") + "CONNECT TO SQL.");
var connection = mysql.createConnection({
host: localhost,
port: 3306,
database: imooc,
user: root,
password: chen321,
charset: "utf8"
});
connection.connect(function (err) {
if (err) {
console.log("SQL CONNECT ERROR: ", err);
} else {
console.log("SQL CONNECT SUCCESSFUL.");
}
});
connection.on("close", function(err) {
console.log("SQL CONNECTION CLOSED.");
});
connection.on("error", function(err) {
console.log("SQL CONNECTION ERROR: ." + err);
});
module.exports.connection = connection;
return module.exports.connection;
};
module.exports.getConnection();