egg.js部署上线,结果连不上服务器里的MySQL数据库,好难受~(更新:终于解决了)
发布于 3 个月前 作者 darrenliuwei 1760 次浏览 来自 问答

在网上找了好久,egg.js部署上线教程几乎都和egg官方文档一样。

但是都没有提到MySQL数据库这一块

我是直接 sudo apt-get install mysql-server mysql-client 进行安装MySQL的

难受,难道真的要从入门到放弃么 微信截图_20191228191225.png


更新: 几个小时后,数据库终于连上了,但是在执行 npm start 时报错了 报错信息:

ubuntu@VM-0-8-ubuntu:/var/www/html/loveapi$ npm start

> [email protected] start /var/www/html/loveapi
> egg-scripts start --daemon --title=egg-server-loveapi --ignore-stderr

[egg-scripts] Starting egg application at /var/www/html/loveapi
[egg-scripts] Run node /var/www/html/loveapi/node_modules/egg-scripts/lib/start-cluster {"title":"egg-server-loveapi","baseDir":"/var/www/html/loveapi","framework":"/var/www/html/loveapi/node_modules/egg"} --title=egg-server-loveapi
[egg-scripts] Save log file to /home/ubuntu/logs
[egg-scripts] Wait Start: 1...
[egg-scripts] Wait Start: 2...
[egg-scripts] tail -n 100 /home/ubuntu/logs/master-stderr.log
[egg-scripts] Got error when startup:
[egg-scripts] 2019-12-28 22:35:56,008 ERROR 2722 [app_worker] server got error: bind EACCES 127.0.0.1:80, code: EACCES
[egg-scripts] 2019-12-28 22:35:56,021 ERROR 2704 nodejs.AppWorkerDiedError: [master] app_worker#1:2722 died (code: 0, signal: null, suicide: false, state: dead), current workers: []
[egg-scripts]     at Master.onAppExit (/var/www/html/loveapi/node_modules/egg-cluster/lib/master.js:510:21)
[egg-scripts]     at Master.emit (events.js:210:5)
[egg-scripts]     at Messenger.sendToMaster (/var/www/html/loveapi/node_modules/egg-cluster/lib/utils/messenger.js:137:17)
[egg-scripts]     at Messenger.send (/var/www/html/loveapi/node_modules/egg-cluster/lib/utils/messenger.js:102:12)
[egg-scripts]     at EventEmitter.<anonymous> (/var/www/html/loveapi/node_modules/egg-cluster/lib/master.js:353:22)
[egg-scripts]     at EventEmitter.emit (events.js:215:7)
[egg-scripts]     at ChildProcess.<anonymous> (internal/cluster/master.js:188:13)
[egg-scripts]     at Object.onceWrapper (events.js:300:26)
[egg-scripts]     at ChildProcess.emit (events.js:210:5)
[egg-scripts]     at Process.ChildProcess._handle.onexit (internal/child_process.js:272:12)
[egg-scripts] name: "AppWorkerDiedError"
[egg-scripts] pid: 2704
[egg-scripts] hostname: VM-0-8-ubuntu
[egg-scripts]
[egg-scripts] 2019-12-28 22:35:56,021 ERROR 2704 [master] app_worker#1:2722 start fail, exiting with code:1
[egg-scripts] 2019-12-28 22:35:56,021 ERROR 2704 [master] exit with code:1
[egg-scripts] 2019-12-28 22:35:56,029 ERROR 2711 [agent_worker] receive disconnect event on child_process fork mode, exiting with code:110
[egg-scripts] 2019-12-28 22:35:56,031 ERROR 2711 [agent_worker] exit with code:110
[egg-scripts]
[egg-scripts] Start got error, see /home/ubuntu/logs/master-stderr.log
[egg-scripts] Or use `--ignore-stderr` to ignore stderr at startup.

/home/ubuntu/logs/master-stderr.log 文件里的内容:

2019-12-28 22:30:54,738 ERROR 1761 [app_worker] server got error: bind EADDRINUSE 127.0.0.1:80, code: EADDRINUSE
2019-12-28 22:30:54,753 ERROR 1739 nodejs.AppWorkerDiedError: [master] app_worker#1:1761 died (code: 0, signal: null, suicide: false, state: dead), current workers: []
    at Master.onAppExit (/var/www/html/loveapi/node_modules/egg-cluster/lib/master.js:510:21)
    at Master.emit (events.js:210:5)
    at Messenger.sendToMaster (/var/www/html/loveapi/node_modules/egg-cluster/lib/utils/messenger.js:137:17)
    at Messenger.send (/var/www/html/loveapi/node_modules/egg-cluster/lib/utils/messenger.js:102:12)
    at EventEmitter.<anonymous> (/var/www/html/loveapi/node_modules/egg-cluster/lib/master.js:353:22)
    at EventEmitter.emit (events.js:215:7)
    at ChildProcess.<anonymous> (internal/cluster/master.js:188:13)
    at Object.onceWrapper (events.js:300:26)
    at ChildProcess.emit (events.js:210:5)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:272:12)
name: "AppWorkerDiedError"
pid: 1739
hostname: VM-0-8-ubuntu

2019-12-28 22:30:54,753 ERROR 1739 [master] app_worker#1:1761 start fail, exiting with code:1
2019-12-28 22:30:54,754 ERROR 1739 [master] exit with code:1
2019-12-28 22:30:54,762 ERROR 1750 [agent_worker] receive disconnect event on child_process fork mode, exiting with code:110
2019-12-28 22:30:54,763 ERROR 1750 [agent_worker] exit with code:110

更新: 经过 @i5ting 的提醒,终于运行不报错了,但是接下来我突然不知道该怎么访问我写的接口了: 过程: 我把 config.default.js 文件改成了下面这样,主要是把端口号从 80 改成了 7001 Snipaste_2019-12-28_22-45-17.png

ubuntu@VM-0-8-ubuntu:/var/www/html/loveapi$ npm start

> [email protected] start /var/www/html/loveapi
> egg-scripts start --daemon --title=egg-server-loveapi --ignore-stderr

[egg-scripts] Starting egg application at /var/www/html/loveapi
[egg-scripts] Run node /var/www/html/loveapi/node_modules/egg-scripts/lib/start-cluster {"title":"egg-server-loveapi","baseDir":"/var/www/html/loveapi","framework":"/var/www/html/loveapi/node_modules/egg"} --title=egg-server-loveapi
[egg-scripts] Save log file to /home/ubuntu/logs
[egg-scripts] Wait Start: 1...
[egg-scripts] egg started on http://127.0.0.1:7001
ubuntu@VM-0-8-ubuntu:/var/www/html/loveapi$

更新: 把配置文件 config.default.js 改成下面这样之后,可以通过IP或者域名+端口号访问接口,但是我希望可以把端口号也省略掉,有办法吗? 捕获.PNG

更新: 最后终于解决了 把配置文件 config.default.js 改成下面这样(其实就是直接注释了): Snipaste_2019-12-28_23-20-06.png 然后再在 Nginx 里加上这些配置: Snipaste_2019-12-28_23-22-56.png Nginx 配置链接:https://eggjs.org/zh-cn/tutorials/socketio.html#部署

7 回复

你这是连接数据库失败了啊 所以先检查账号密码是否正确, 然后在服务器上直接用mysql 账户账号密码尝试连接数据库, 看看能不能连

自己先连上啊。很明显mysql权限没处理

@cWatermelon @i5ting 谢谢,在经过几小时排查,终于可以连上数据库了,但是在执行 npm start 时,又报错了,难受~

80端口,应该是访问权限问题吧

@i5ting 感谢,终于不报错了,可是我发现浏览器输入IP地址+端口号,还是访问不到我写的get接口

楼主是后端新手吗?

@JZLeung 嗯,刚接触 egg.js 不久

回到顶部