云服务器 ECS Nodejs 通 过forever 设置后台运行及 Nodejs 链接 MySQL RDS

  • A+
所属分类:Linux
高性能企业级服务器首台5折

Nodejs 应用通过 forever 设置后台运行


默认可以通过 node app.js 启动 nodejs 应用,如果退出 ssh 连接,应用也会退出。

可以使用 Forever 来管理启动 nodejs 应用,Forever 可以看做是一个 nodejs 的守护进程,能够启动,停止,重启我们的 app 应用

如同官方的描述

  1. 1
    <span class="pln">A simple CLI tool </span><span class="kwd"><span class="hljs-keyword">for</span></span><span class="pln"> ensuring that a given script runs continuously </span><span class="pun">(</span><span class="pln">i</span><span class="pun">.</span><span class="pln">e</span><span class="pun">.</span><span class="pln"> forever</span><span class="pun">).//</span> <span class="pun">一个用来持续(或者说永远)运行一个给定脚本的简单的命令行工具</span>

安装

  1. 1
    <span class="pln">sudo npm install forever </span><span class="pun">-</span><span class="pln">g </span><span class="com"><span class="hljs-comment">#记得加-g,forever要求安装到全局环境下</span></span>

启动

  1. 1
    <span class="pln">forever start app</span><span class="pun">.</span><span class="pln">js</span>
  2. 1
    <span class="pln">forever start </span><span class="pun">-</span><span class="pln">o out</span><span class="pun">.</span><span class="pln">log </span><span class="pun">-</span><span class="pln">e err</span><span class="pun">.</span><span class="pln">log app</span><span class="pun">.</span><span class="pln">js     </span><span class="com"><span class="hljs-comment">#-o 是 console 输出的日志,-e 是 console.error 日志</span></span>
  3. 1
    <span class="pln">forever list </span><span class="com"><span class="hljs-comment">##显示所有运行的服务</span></span>

开机自启动可以添加到 /etc/rc.local 里

 

Nodejs 连接RDS MySQL数据库


Nodejs 连接 RDS MySQL 数据库,Nodejs 连接连接数据库需要通过 Nodejs  的 api 来进行,以连接 MySQL 为例可以参考如下

1. 安装连接模块 npm install mysql

  1. 1
    <span class="pln">npm install mysql</span>

 

2. 运行命令截图:

1.png

 

3. 连接代码参考

首先通过 require 获取 MySQL 的接口,定义连接字符串

  1. 1
    <span class="pln">var mysql </span><span class="pun">=</span><span class="pln"> require</span><span class="pun">(</span><span class="str"><span class="hljs-string">'mysql'</span></span><span class="pun">);</span>
  2. 1
    <span class="pln">var conn </span><span class="pun">=</span><span class="pln"> mysql</span><span class="pun">.</span><span class="pln">createConnection</span><span class="pun">({</span>
  3. 1
    <span class="pln">    host</span><span class="pun">:</span> <span class="str"><span class="hljs-string">'localhost'</span></span><span class="pun">,</span>
  4. 1
    <span class="pln">    user</span><span class="pun">:</span> <span class="str"><span class="hljs-string">'nodejs'</span></span><span class="pun">,</span>
  5. 1
    <span class="pln">    password</span><span class="pun">:</span> <span class="str"><span class="hljs-string">'nodejs'</span></span><span class="pun">,</span>
  6. 1
    <span class="pln">    database</span><span class="pun">:</span> <span class="str"><span class="hljs-string">'nodejs'</span></span><span class="pun">,</span>
  7. 1
    <span class="pln">    port</span><span class="pun">:</span> <span class="lit">3306</span>
  8. 1
    <span class="pun">});</span>
  9. 1
    <span class="pln">conn</span><span class="pun">.</span><span class="pln">connect</span><span class="pun">();</span>

 

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: