动态创建实例已经OK,但引起了其他方面的报错,报错信息如下:
> egg-bin dev
2017-03-10 17:23:45,381 INFO 88108 [master] =================== egg start =====================
2017-03-10 17:23:45,383 INFO 88108 [master] egg version 0.5.0
2017-03-10 17:23:45,384 INFO 88108 [master] start with options: {"framework":"/Users/johnnychq/workbench/cxinscn_cathay_portal/public/node_modules/egg","baseDir":"/Users/johnnychq/workbench/cxinscn_cathay_portal/public","port":7001,"workers":1,"plugins":null,"https":false,"key":"","cert":"","customEgg":"/Users/johnnychq/workbench/cxinscn_cathay_portal/public/node_modules/egg"}
2017-03-10 17:23:45,384 INFO 88108 [master] start with env: isProduction: false, isDebug: false, EGG_SERVER_ENV: undefined, NODE_ENV: development
2017-03-10 17:23:45,404 INFO 88108 [master] Agent Worker:88109 start with ["{\"framework\":\"/Users/johnnychq/workbench/cxinscn_cathay_portal/public/node_modules/egg\",\"baseDir\":\"/Users/johnnychq/workbench/cxinscn_cathay_portal/public\",\"port\":7001,\"workers\":1,\"plugins\":null,\"https\":false,\"key\":\"\",\"cert\":\"\",\"customEgg\":\"/Users/johnnychq/workbench/cxinscn_cathay_portal/public/node_modules/egg\",\"clusterPort\":56747}"]
2017-03-10 17:23:45,836 INFO 88109 [egg:watcher:agent] watcher start success
2017-03-10 17:23:45,841 INFO 88108 [master] Agent Worker started (441ms)
2017-03-10 17:23:45,851 INFO 88108 [master] App Worker#1:88110 start, state: none, current workers: ["1"]
2017-03-10 17:23:46,793 WARN 88108 [master] App Worker#1:88110 started at 7001, remain 0 (952ms)
2017-03-10 17:23:46,793 INFO 88108 [master] egg started on http://127.0.0.1:7001 (1409ms)
2017-03-10 17:23:46,810 ERROR 88110 nodejs.ER_ACCESS_DENIED_ERRORError: ER_ACCESS_DENIED_ERROR: Access denied for user 'DBSA1'@'10.167.11.82' (using password: YES)
at Handshake.Sequence._packetToError (/Users/johnnychq/workbench/cxinscn_cathay_portal/public/node_modules/.2.13.0@mysql/lib/protocol/sequences/Sequence.js:52:14)
at Handshake.ErrorPacket (/Users/johnnychq/workbench/cxinscn_cathay_portal/public/node_modules/.2.13.0@mysql/lib/protocol/sequences/Handshake.js:103:18)
at Protocol._parsePacket (/Users/johnnychq/workbench/cxinscn_cathay_portal/public/node_modules/.2.13.0@mysql/lib/protocol/Protocol.js:280:23)
at Parser.write (/Users/johnnychq/workbench/cxinscn_cathay_portal/public/node_modules/.2.13.0@mysql/lib/protocol/Parser.js:75:12)
at Protocol.write (/Users/johnnychq/workbench/cxinscn_cathay_portal/public/node_modules/.2.13.0@mysql/lib/protocol/Protocol.js:39:16)
at Socket. (/Users/johnnychq/workbench/cxinscn_cathay_portal/public/node_modules/.2.13.0@mysql/lib/Connection.js:103:28)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
--------------------
at Protocol._enqueue (/Users/johnnychq/workbench/cxinscn_cathay_portal/public/node_modules/.2.13.0@mysql/lib/protocol/Protocol.js:141:48)
at Protocol.handshake (/Users/johnnychq/workbench/cxinscn_cathay_portal/public/node_modules/.2.13.0@mysql/lib/protocol/Protocol.js:52:41)
at PoolConnection.connect (/Users/johnnychq/workbench/cxinscn_cathay_portal/public/node_modules/.2.13.0@mysql/lib/Connection.js:130:18)
at Pool.getConnection (/Users/johnnychq/workbench/cxinscn_cathay_portal/public/node_modules/.2.13.0@mysql/lib/Pool.js:48:16)
at Pool.query (/Users/johnnychq/workbench/cxinscn_cathay_portal/public/node_modules/.2.13.0@mysql/lib/Pool.js:202:8)
at /Users/johnnychq/workbench/cxinscn_cathay_portal/public/node_modules/.2.6.1@ali-rds/lib/client.js:28:10
at /Users/johnnychq/workbench/cxinscn_cathay_portal/public/node_modules/.4.6.0@co/index.js:136:8
at thunkToPromise (/Users/johnnychq/workbench/cxinscn_cathay_portal/public/node_modules/.4.6.0@co/index.js:135:10)
at toPromise (/Users/johnnychq/workbench/cxinscn_cathay_portal/public/node_modules/.4.6.0@co/index.js:119:55)
at next (/Users/johnnychq/workbench/cxinscn_cathay_portal/public/node_modules/.4.6.0@co/index.js:99:29)
sql: select now() as currentTime;
code: 'ER_ACCESS_DENIED_ERROR'
errno: 1045
sqlState: '28000'
fatal: true
name: 'ER_ACCESS_DENIED_ERRORError'
pid: 88110
hostname: johnnychq-MacBook-Pro.local
2017-03-10 17:23:46,852 INFO 88110 [egg-mysql] select now() as currentTime; 68ms
报错信息比较简单,说连接mysql错误。
动态创建mysql的app.js的代码为:
// app.js
module.exports = app => {
app.beforeStart(function* () {
let mysqlConfig = app.config.mysql.client;
if(app.config.env == 'prod'){
mysqlConfig.password = decipher(mysqlConfig.password)
}
// app.mysql = app.mysql
app.database = app.mysql.createInstance(mysqlConfig);
});
};
生产环境config.prod.js的配置为
exports.mysql = {
// 单数据库信息配置
client: {
// host
host: '10.164.50.22',
// 端口号
port: '3300',
// 用户名
user: 'DBSA1',
// 密码
password: '36a880b148',
// 数据库名
database: 'cathay_portal',
},
// 是否加载到 app 上,默认开启
app: true,
// 是否加载到 agent 上,默认关闭
agent: false,
};
上述config中的password已经进行加密,在app.js中根据env=prod判断,会进行动态解密密码,然后手动创建app.database的mysql实例。
上述过程代码以及逻辑我觉的没什么问题,但为什么会报错呢?
我怀疑是,即使通过app.js动态创建mysql实例,egg框架仍然会创建一个默认的mysql实例,并且该实例会以config中的账号、密码进行连接,由于config中的密码被加密,所以连接失败。即创建了两个mysql实例,自己动态创建的没问题,但egg内部默认创建的报错。