安装mysql数据库插件
执行安装命令
cnpm/npm install egg-mysql --save yarn add egg-mysql --save
在egg项目的config/plugin.js中添加
module.exports = { mysql: { enable: true, package: 'egg-mysql' } }
在egg项目的config/config.default.js中添加
module.exports = { config.mysql = { client: { host: 'localhost', // 自己的数据库host port: '3306', // 自己的连接端口 user: 'root', // 可以使用root password: 'root', // 数据库密码 database: 'database', // 要连接的数据库名称 }, app: true, agent: false, } }
接下来可以通过写service操作数据库数据