asd
(1) cmd到目錄安裝外掛
npm i --save egg-mysql
(2) 到config/plugin.js 新增下面code,代表啟動該plugin
exports.mysql = {
enable: true,
package: 'egg-mysql',
};
(3) config/plugin.js 如有 module.exports 需註解掉
(4) config/config.${env}.js 配置連線資訊
//代表用config.mysql 或 exports.mysql都可以
const config = exports = {};
//連線資訊
exports.mysql = {
client: {
host: 'mysql.com',
port: '3306',
user: 'test_user',
password: 'test_password',
database: 'test',
},
app: true,
agent: false,
};
(5)可以去controler下語法輸出
let a = await this.app.mysql.query('SELECT * FROM admins ', '');
this-----app-----mysql-----query
ctx-----|
----------------其他方式----------------
//官方建議把mysql放在Service層
(1)建立一個service資料夾
(2)建立一個跟controller一樣名稱的service
例如:App----Controller----Device.js
|
|---service-------Device.js
檔名需一樣呼叫才會成功
語法
ctx.service.device.xxxx()
語法說明
const user = await this.app.mysql.get('users', { id: 11 });
????????待研究????????
----------------
#ubuntu解鎖目錄使用權
chmod 777 -R /目錄