使用腾讯云轻量级服务器

购买于2021年11月11日.系统为Ubuntu,选这个是因为比较熟悉Ubuntu.

安装环境

1. sudo apt-get update  # 更新软件源

2. sudo apt-get install nodejs -y  

3. sudo apt-get install npm -y

4. node -v #查看nodejs版本,发现版本太久,v10的版本,而现在已经是v16.

于是开始升级nodejs的版本

node有一个模块叫n,是专门用来管理node.js的版本。

1. sudo npm install -g n

2. sudo n stable #升级node.js到最新稳定版

3. sudo npm install -g koa 安装koa

4. adduser  ubuntu推荐用这个,可以有互动对话,而Useradd需要手动指定很多初始化参数

5. 给新用户添加sudo权限 sudo vi /etc/sudoers

 "root ALL=(ALL) ALL" 在起下面添加 "xxx ALL=(ALL) ALL" (这里的 xxx 是你的用户名),然后保存退出

ssh登录到服务器,失败

问题原因:ssh链接未允许远程密码认证导致
解决办法:
vim /etc/ssh/sshd_config

PasswordAuthentication no
更改为
PasswordAuthentication yes
按【ESC】输入保存:
:wq

再输入命令重启服务
service sshd restart

改用MobaXterm,这个集成了winscp的功能和secureCRT的功能.

 安装mysql

sudo apt-get install mysql-server

        sudo apt install mysql-client

在Ubuntu中,默认情况下,只有最新版本的MySQL包含在APT软件包存储库中,要安装它,只需更新服务器上的包索引并安装默认包apt-get。

#命令1
sudo apt-get update
#命令2
sudo apt-get install mysql-server


2.配置MySQL
2.1 初始化配置
sudo mysql_secure_installation
1
配置项较多,如下所示:

#1
VALIDATE PASSWORD PLUGIN can be used to test passwords...
Press y|Y for Yes, any other key for No: N (我的选项)

#2
Please set the password for root here...
New password: (输入密码)
Re-enter new password: (重复输入)

#3
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them...
Remove anonymous users? (Press y|Y for Yes, any other key for No) : N (我的选项)

#4
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network...
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y (我的选项)

#5
By default, MySQL comes with a database named 'test' that
anyone can access...
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N (我的选项)

#6
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y (我的选项)

2.2 检查mysql服务状态
systemctl status mysql.service
1
显示如下结果说明mysql服务是正常的:

https://blog.csdn.net/weixx3/article/details/80782479

4. 重新启动mysql:

sudo service mysql restart
mysql -u root -p // 启动后输入已经修改好的密码:root

 

 https://www.runoob.com/nodejs/nodejs-mysql.html  nodejs 连接mysql

CREATE DATABASE koa;

--- 创建用户并授予权限
CREATE USER 'user'@'localhost' IDENTIFIED BY 'pass';
GRANT ALL PRIVILEGES ON koa.* TO 'user'@'localhost';

--- 处理 MySQL 8.0 版本的认证协议问题
ALTER USER 'user'@'localhost' IDENTIFIED WITH mysql_native_password BY 'pass';     这里要将密码验证方式改成  mysql_native_password ,并重设新密码,新密码必须与旧密码不同才行.
flush privileges;

USE mysql; #查看密码验证方式
SELECT User, Host, plugin FROM mysql.user;

The user specified as a definer ('mysql.infoschema'@'localhost') does not exist

  1. Delete the problematic user
DROP USER 'mysql.infoschema'@'localhost';

The rest of the solution is like previous answers.

  1. Create the user again
  2. Grant it permissions
mysql> CREATE USER 'mysql.infoschema'@'localhost' IDENTIFIED BY 'password';

mysql> GRANT SELECT ON *.* TO `mysql.infoschema`@`localhost`;
 

最近一个项目微信小程序,需要写个小型的后端程序处理聊天通讯记录保存,主要是功能是组建群聊天室,所以用node写了个websocket服务...

但是终端连接到服务器,运行 node server.js, 退出终端之后,服务就停止运行了。
原以为 node server.js & 或者 nohup node server.js >/dev/null 2>&1 & 能轻松的解决,后来发现完全不是那么回事..........
谷歌了一番资料,了解到

nodejs一般是当成一条用户命令执行的,当用户断开客户连接,运用也就停了,很烦人。如何让nodejs应用当成服务,在后台执行呢?

最后使用 forever 包搞定,解决方案如下:

sudo npm install -g forever --registry=http://registry.cnpmjs.org
forever start 你的脚本文件(如server.js)
forever list 查看所有 forever 运行的进程
forever stop uid 停止运行指定 uid 的进程

 它能做更多的事情,比如分别记录输出和错误日志,比如可以在js中作为api使用。通过以下(我的是ubutun系统)也可以正常安装

$ sudo npm install forever -g   #安装
$ forever start app.js          #启动
$ forever stop app.js           #关闭
$ forever start -l forever.log -o out.log -e err.log app.js   #输出日志和错误

命令语法及使用 https://github.com/nodejitsu/forever

 sudo forever start -v -c ts-node src/server.ts

https://stackoverflow.com/questions/57997191/how-to-run-typescript-files-on-server-in-background
原文地址:https://www.cnblogs.com/laiqun/p/15542482.html