Authored by 郝肖肖

'mysql-lib'

@@ -18,6 +18,13 @@ const defaults = { @@ -18,6 +18,13 @@ const defaults = {
18 port: '6379', 18 port: '6379',
19 //password: '' 19 //password: ''
20 } 20 }
  21 + },
  22 + mysql: {
  23 + host: '192.168.102.219',
  24 + user: 'yh_test',
  25 + password: 'yh_test',
  26 + port: '3306',
  27 + database: 'seo',
21 } 28 }
22 }; 29 };
23 30
  1 +'use strict';
  2 +
  3 +const mysql = require('promise-mysql');
  4 +const config = require('../config/config');
  5 +
  6 +class MysqlPromise {
  7 + constructor() {
  8 + this.connect().then(conn => {
  9 + this.connect = conn;
  10 + });
  11 + }
  12 +
  13 + connect() {
  14 + if (this.connect) {
  15 + return Promise.resolve(this.connect);
  16 + }
  17 +
  18 + return mysql.createConnection({
  19 + host: config.mysql.host,
  20 + user: config.mysql.user,
  21 + password: config.mysql.password,
  22 + port: config.mysql.port,
  23 + database: config.mysql.database
  24 + }).then(conn => {
  25 + return conn;
  26 + }).catch(err => {
  27 + console.error(`mysql connect fail, err: ${err.message}`);
  28 + return null;
  29 + });
  30 + }
  31 +}
  32 +
  33 +module.exports = MysqlPromise;
@@ -54,6 +54,7 @@ @@ -54,6 +54,7 @@
54 "node-zookeeper-client": "^0.2.2", 54 "node-zookeeper-client": "^0.2.2",
55 "nodemailer": "^3.1.8", 55 "nodemailer": "^3.1.8",
56 "npm": "^3.10.9", 56 "npm": "^3.10.9",
  57 + "promise-mysql": "^3.0.1",
57 "qcloudapi-sdk": "^0.1.5", 58 "qcloudapi-sdk": "^0.1.5",
58 "qn": "^1.3.0", 59 "qn": "^1.3.0",
59 "qs": "^6.2.0", 60 "qs": "^6.2.0",
@@ -68,4 +69,4 @@ @@ -68,4 +69,4 @@
68 "devDependencies": { 69 "devDependencies": {
69 "nodemon": "^1.11.0" 70 "nodemon": "^1.11.0"
70 } 71 }
71 -}  
  72 +}