一. 环境版本
1. node v13.5.0
2. npm v6.13.4
3. mysql v8.0.19
3. egg.js v2.15.1
4. egg-mysql v3.0.0
二. 安装mysql
1. 安装mysql ,系统可自动选择您需要下载的版本,选择第一个 Download 进行下载
2. 进入傻瓜式安装,其中需要注意的一点是下面一步
如果选择 use Strong password Encryption 选项,为强密码模式,则会将你输入的密码进行加密,会在后面服务器与客户端连接时报错
如果选择 use Legacy Password Encryption 选项,为普通模式
为了方便,我们选择第二种普通模式,如已经安装选择了第一种,也没有关系,可通过终端进行修改为普通模式,(提示:在下面👇【四-7,8,9】题中提到)
三,打开 【系统偏好设置】,将mysql 开启
四,打开终端,
1. 查看MySQL是否存 执行语句为: ps aux | grep mysql
2. 执行命令切换到这个目录底下,执行语句为:cd /usr/local/mysql/
3. 切换到bin目录下,执行语句为:cd bin/
4. 登录MySQL,输入指令后根据提示输入初始密码 执行语句为:./mysql -u root -p
5. 如密码错误,或密码复杂需修改初始密码,执行语句为:SET PASSWORD FOR ‘root’@‘localhost’ = PASSWORD(‘新密码’);
6. 刷新权限 ,执行语句为:FLUSH PRIVILEGES;
7. 如需要将加密模式改为普通模式,执行语句为:ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;
8. 更新用户密码,执行语句为:ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
9. 刷新权限 ,执行语句为:FLUSH PRIVILEGES;
10. 项目中输入刚刚修改的密码,再次测试连接,搞定
五,安装 Navicat for Mysql,下载
下载后,打开安装包,如提示文件已损坏或无法打开,请查看以下👇文档或 损坏请查看此链接,打不开请查看此链接
五,本人使用next.js 框架来 使用egg.js
1. 全局安装egg.js的脚手架工具egg-init:
npm i egg-init -g
2. 在next.js 项目的根目录下新建 service,并cd service,用脚手架自动生成项目的基本结构
egg-init --type=simple
3. 安装插件
npm install
4. 启动
npm run dev
然后在浏览器中打开网址:http://127.0.0.1:7001/
如果在页面中显示hi.egg说明我们环境搭建完成。
5. 安装 egg-mysql
npm i egg-mysql --save 或 yarn add egg-mysql
安装完成可以到package.json中看一下
6. 插件配置,文件/server/config/plugin.js,下面配置
'use strict';
//配置插件
exports.mysql = {
enable: true,
package: 'egg-mysql'
}
7. 数据库连接,打开/config/config.default.js文件,作下面的配置
'use strict';
/**
* @param {Egg.EggAppInfo} appInfo app info
*/
module.exports = appInfo => {
/**
* built-in config
* @type {Egg.EggAppConfig}
**/
const config = exports = {};
// use for cookie sign key, should change to your own and keep security
config.keys = appInfo.name + '_1584348767004_9973';
// add your middleware config here
config.middleware = [];
// add your user config here
const userConfig = {
// myAppName: 'egg',
};
config.mysql = {
client: {
// host
host: 'localhost',
// 端口号
port: '3306',
// 用户名
user: 'root',
// 密码
password: '321',
// 数据库名
database: 'test'
},
// 是否加载到 app 上,默认开启
app: true,
// 是否加载到 agent 上,默认关闭
agent: false,
};
return {
...config,
...userConfig,
};
};
五,遇到问题
以下问题有两种情况会提示错误
1. 密码输入错误,请输入正确密码
2. 因安装mysql默认为密码加密模式,需修改 为普通模式 (可查看 👆【四-7,8,9】题)
1. ERROR 25667 [-/127.0.0.1/-/0ms GET /] nodejs.ER_NOT_SUPPORTED_AUTH_MODEError: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
2. 2020-03-17 10:22:20,089 ERROR 26558 [-/127.0.0.1/-/0ms GET /] nodejs.ER_ACCESS_DENIED_ERRORError: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'localhost' (using password: YES)
at Handshake.Sequence._packetToError (/Users/gaoqiaoxia/workSpace/blog/service/node_modules/_mysql@2.18.1@mysql/lib/protocol/sequences/Sequence.js:47:14)
at Handshake.ErrorPacket (/Users/gaoqiaoxia/workSpace/blog/service/node_modules/_mysql@2.18.1@mysql/lib/protocol/sequences/Handshake.js:123:18)
at Protocol._parsePacket (/Users/gaoqiaoxia/workSpace/blog/service/node_modules/_mysql@2.18.1@mysql/lib/protocol/Protocol.js:291:23)
at Parser._parsePacket (/Users/gaoqiaoxia/workSpace/blog/service/node_modules/_mysql@2.18.1@mysql/lib/protocol/Parser.js:433:10)
at Parser.write (/Users/gaoqiaoxia/workSpace/blog/service/node_modules/_mysql@2.18.1@mysql/lib/protocol/Parser.js:43:10)
at Protocol.write (/Users/gaoqiaoxia/workSpace/blog/service/node_modules/_mysql@2.18.1@mysql/lib/protocol/Protocol.js:38:16)
at Socket.<anonymous> (/Users/gaoqiaoxia/workSpace/blog/service/node_modules/_mysql@2.18.1@mysql/lib/Connection.js:88:28)
at Socket.<anonymous> (/Users/gaoqiaoxia/workSpace/blog/service/node_modules/_mysql@2.18.1@mysql/lib/Connection.js:526:10)
at Socket.emit (events.js:305:20)
at addChunk (_stream_readable.js:341:12)
--------------------
at Protocol._enqueue (/Users/gaoqiaoxia/workSpace/blog/service/node_modules/_mysql@2.18.1@mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/Users/gaoqiaoxia/workSpace/blog/service/node_modules/_mysql@2.18.1@mysql/lib/protocol/Protocol.js:51:23)
at PoolConnection.connect (/Users/gaoqiaoxia/workSpace/blog/service/node_modules/_mysql@2.18.1@mysql/lib/Connection.js:116:18)
at Pool.getConnection (/Users/gaoqiaoxia/workSpace/blog/service/node_modules/_mysql@2.18.1@mysql/lib/Pool.js:48:16)
at /Users/gaoqiaoxia/workSpace/blog/service/node_modules/_pify@2.3.0@pify/index.js:29:7
at new Promise (<anonymous>)
at Pool.<anonymous> (/Users/gaoqiaoxia/workSpace/blog/service/node_modules/_pify@2.3.0@pify/index.js:12:10)
at Pool.ret [as getConnection] (/Users/gaoqiaoxia/workSpace/blog/service/node_modules/_pify@2.3.0@pify/index.js:56:34)
at Pool.query (/Users/gaoqiaoxia/workSpace/blog/service/node_modules/_mysql@2.18.1@mysql/lib/Pool.js:202:8)
at /Users/gaoqiaoxia/workSpace/blog/service/node_modules/_pify@2.3.0@pify/index.js:29:7
sql: select now() as currentTime;
code: "ER_ACCESS_DENIED_ERROR"
errno: 1045
sqlMessage: "Access denied for user 'root'@'localhost' (using password: YES)"
sqlState: "28000"
fatal: true
name: "ER_ACCESS_DENIED_ERRORError"
pid: 26558
hostname: MacBook-Pro.local
3. 2020-03-17 10:22:20,115 ERROR 26529 nodejs.AppWorkerDiedError: [master] app_worker#3:26558 died (code: 0, signal: null, suicide: false, state: dead), current workers: []
at Master.onAppExit (/Users/gaoqiaoxia/workSpace/blog/service/node_modules/_egg-cluster@1.26.0@egg-cluster/lib/master.js:510:21)
at Master.emit (events.js:305:20)
at Messenger.sendToMaster (/Users/gaoqiaoxia/workSpace/blog/service/node_modules/_egg-cluster@1.26.0@egg-cluster/lib/utils/messenger.js:137:17)
at Messenger.send (/Users/gaoqiaoxia/workSpace/blog/service/node_modules/_egg-cluster@1.26.0@egg-cluster/lib/utils/messenger.js:102:12)
at EventEmitter.<anonymous> (/Users/gaoqiaoxia/workSpace/blog/service/node_modules/_egg-cluster@1.26.0@egg-cluster/lib/master.js:353:22)
at EventEmitter.emit (events.js:317:22)
at ChildProcess.<anonymous> (internal/cluster/master.js:192:13)
at Object.onceWrapper (events.js:412:26)
at ChildProcess.emit (events.js:305:20)
at Process.ChildProcess._handle.onexit (internal/child_process.js:275:12)
name: "AppWorkerDiedError"
pid: 26529
hostname: MacBook-Pro.local