使用騰訊雲輕量級服務器


購買於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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM