首先,安装与配置
npm install egg-mysql
config/plugin.js
然后在config/config.default.js里面按照下图添加一下,password是连接mysql的密码(注意数据库的版本,版本过低可能报错==>报这个” Client does not support authentication protocol requested by server; consider upgrading MySQL client(客户端不支持服务器请求的身份验证协议;请考虑升级MySQL客户端) “====>升级mysql(即重装哈哈哈哈哈))
config/config.default.js
然后简单的操作一下
在app/controller下新建一个suibian.js,查询test1表中的所有数据
‘use strict’;
const Controller = require(‘egg’).Controller;
class HomeController extends Controller { async index() { const result=await this.app.mysql.query(“select * from test1”); this.ctx.body=result; }}
module.exports = HomeController;
如果你没出错,就完成了,其他的操作语句,去官方文档看一下,报错一般是mysql版本问题
#注意mysql配置那里是否正确,语句是否出现错误,流程没错,注意router。
github地址:https://github.com/thesecondlight/egg_mysql 注册登录