Skip to content

egg-mysql配置 #3

@davidkorea

Description

@davidkorea

egg-mysql plugin

1 配置egg-mysql插件

  1. cnpm install --save egg-mysql
  2. config/plugin.js
'use strict';

exports.mysql = {
  enable: true,
  package: 'egg-mysql'
}
  1. config/config.fefault.js
module.exports = appInfo => {
  const config = exports = {};
  config.keys = appInfo.name + '_1586922076183_5926';
  config.middleware = [];
  const userConfig = {
    // myAppName: 'egg',
  };

  // database configuration
  config.mysql = {
    client: {
      host: 'localhost',
      port: '3306',
      user: 'root',
      password: 'root',
      database: 'egg-db',    
    },
    app: true,
    agent: false,
  };

  return {
    ...config,
    ...userConfig,
  };
};

2 测试数据库查询

  • app/controller/home.js
class HomeController extends Controller {
  async index() {
    let result = await this.app.mysql.get("blog_type",{})
    this.ctx.body = 'hi, egg ' + JSON.stringify(result);
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions