eggjs 连接数据库配置

eggjs yekong 1261℃

添加配置

config/plugin.js


'use strict';

/** @type Egg.EggPlugin */
// module.exports = {
//   // had enabled by egg
//   mysql: {
//     enable: true,
//     package: 'egg-mysql',
//   },
//   static: {
//     enable: true,
//   },
// };
exports.mysql = {
  enable: true,
  package: 'egg-mysql',
};
// eslint-disable-next-line eggache/no-override-exports

数据库配置信息

config/config.default.js

/* eslint valid-jsdoc: "off" */

'use strict';

/**
 * @param {Egg.EggAppInfo} appInfo app info
 */
module.exports = appInfo => {
  /**
   * built-in config
   * @type {Egg.EggAppConfig}
   **/
  const config = exports = {};
  config.mysql = {

    // 单数据库信息配置
    client: {
      // host
      host: '127.0.0.1',
      // 端口号
      port: '8889',
      // 用户名
      user: 'root',
      // 密码
      password: 'root',
      // 数据库名
      database: 'wanjunshijie',
    },
    // 是否加载到 app 上,默认开启
    app: true,
    // 是否加载到 agent 上,默认关闭
    agent: false,
  };
  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1642632691032_5581';

  // add your middleware config here
  config.middleware = [];

  // csrf
  config.security = {
    csrf: {
      enable: false,
    },
  };
  // add your user config here
  const userConfig = {
    // myAppName: 'egg',
  };

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

连接到数据库后,就可以读取数据库了

喜欢 (1)