云服务器部署(CentOS8,node.js16,MongoDB4)

129 阅读10分钟

Linux基本命令

  1. 文档型(touch,cat,echo,rm,vi,cd)
命令解释
mkdir test创建一个test目录
touch a.txt创建一个a.txt文件
ls查看目录
vi a.txt读取某个文件
i输入
ESC退出输入
:wq,:q, :q!保存并退出,不保存退出,不保存强制退出
cat a.txt查看文件的内容
echo "123" >> a.txt往a.txt中加入内容
rm a.txt删除a.txt文件
rm -r test删除test目录
rm -rf a.txt强制删除某个文件或文件夹
clear清空命令行
  1. 下载\解压
wget命令语法格式:wget [options] [url]

下载:wget https://nodejs.org/dist/v16.13.0/node-v16.13.0-linux-x64.tar.xz

//使用 -O 选项以其他名称保存下载的文件: wget -O node https://nodejs.org/dist/v16.13.0/node-v16.13.0-linux-x64.tar.xz

//使用 -P 选项将文件下载到指定目录-P: wget -P /usr/software https://nodejs.org/dist/v16.13.0/node-v16.13.0-linux-x64.tar.xz
============gzip命令:[选项][文件或者目录]==============

-a或——ascii:使用ASCII文字模式;
-d或--decompress或----uncompress:解开压缩文件;
-f或——force:强行压缩文件。不理会文件名称或硬连接是否存在以及该文件是否为符号连接;
-h或——help:在线帮助;
-l或——list:列出压缩文件的相关信息;
-L或——license:显示版本与版权信息;
-n或--no-name:压缩文件时,不保存原来的文件名称及时间戳记;
-N或——name:压缩文件时,保存原来的文件名称及时间戳记;
-q或——quiet:不显示警告信息;
-r或——recursive:递归处理,将指定目录下的所有文件及子目录一并处理;
-S或<压缩字尾字符串>或----suffix<压缩字尾字符串>:更改压缩字尾字符串;
-t或——test:测试压缩文件是否正确无误;
-v或——verbose:显示指令执行过程;
-V或——version:显示版本信息;
-<压缩效率>:压缩效率是一个介于1~9的数值,预设值为“6”,指定愈大的数值,压缩效率就会愈高;
--best:此参数的效果和指定“-9”参数相同;
--fast:此参数的效果和指定“-1”参数相同。
-num 用指定的数字num调整压缩的速度,-1或--fast表示最快压缩方法(低压缩比),-9或--best表示最慢压缩方法(高压缩比)。系统缺省值为6。
-c或--stdout或--to-stdout:保留原始文件,生成标准输出流(结合重定向使用)。

把test6目录下的每个文件压缩成.gz文件:gzip *

把上例中每个压缩的文件解压,并列出详细的信息:gzip -dv *
 
详细显示例1中每个压缩的文件的信息,并不解压:gzip -l *
 
压缩一个tar备份文件,此时压缩文件的扩展名为.tar.gz:gzip -r log.tar
 
递归的压缩目录:gzip -rv test6
这样,所有test下面的文件都变成了*.gz,目录依然存在只是目录里面的文件相应变成了*.gz.这就是压缩,和打包不同。因为是对目录操作,所以需要加上-r选项,这样也可以对子目录进行递归了。
 
递归地解压目录:gzip -dr test6
 
保留原始文件,把压缩/解压流重定向到新文件:
gzip -c aa > aa.gz
gzip -dc bb.gz > bb



==================tar命令: tar [选项...] [FILE]...=============

tar的主要功能是打包、压缩和解压文件。tar本身不具有压缩功能。他是调用压缩功能实现的 。

-c  :创建一个新的打包文件,可搭配-v来查看过程中要被打包的文件名。
-t  :列出打包文件中的内容。
-x  :解压被打包文件,可以搭配-C(大写)解压到特定目录下
   ====注意,-c, -t, -x不可同时出现在一串指令列中====
-z  :使用gzip的支持来压缩/解压,打包文件名最好为:*.tar.gz
-j  :通过bzip2的支持来压缩/解压,此时文件名最好为:*.tar.bz2
-J  :通过xz的支持来压缩/解压缩,此时文件名最好为:*.tar.xz
-v  :将解压/打包过程显示出来。
-f filename  : -f后面要立刻接要处理的文件名!建议-f单独写一个选项
-C 目录  :指定定要解压到的目录

// tar格式(该格式仅仅打包,不压缩)
-   打包:**tar -cvf [目标文件名].tar [原文件名/目录名]**
-   解包:**tar -xvf [原文件名].tar**
-   注:c参数代表create(创建),x参数代表extract(解包),v参数代表verbose(详细信息),f参数代表filename(文件名),所以f后必须接文件名。

//  tar.gz格式
方式一:利用前面已经打包好的tar文件,直接用压缩命令。
压缩:gzip [原文件名].tar
解压:gunzip [原文件名].tar.gz
方式二:一次性打包并压缩、解压并解包
打包并压缩: tar -zcvf [目标文件名].tar.gz [原文件名/目录名]
解压并解包: tar -zxvf [原文件名].tar.gz
注:z代表用gzip算法来压缩/解压。

// tar.bz2格式
方式一:利用已经打包好的tar文件,直接执行压缩命令:
压缩:bzip2 [原文件名].tar
解压:bunzip2 [原文件名].tar.bz2
方式二:一次性打包并压缩、解压并解包
打包并压缩: tar -jcvf [目标文件名].tar.bz2 [原文件名/目录名]
解压并解包: tar -jxvf [原文件名].tar.bz2
注:小写j代表用bzip2算法来压缩/解压。

// tar.xz格式
方式一:利用已经打包好的tar文件,直接用压缩命令:
压缩:xz [原文件名].tar
解压:unxz [原文件名].tar.xz
方式二:一次性打包并压缩、解压并解包
打包并压缩: tar -Jcvf [目标文件名].tar.xz [原文件名/目录名]
解压并解包: tar -Jxvf [原文件名].tar.xz
注:大写J代表用xz算法来压缩/解压。

删除二进制包: rm -rfnode-v16.13.0-linux-x64.tar.xz

3. 进程

ps -ef //查看进程
ps -ef | grep docker

4. 查看服务的状态

service sshd status //查看ssh服务的状态 
service sshd restart //重启ssh服务 
systemctl status docker  

云服务器(本次centos8)

  1. 选购云服务器

  2. 重置实例密码和远程连接密码,之后重启服务器

  3. 用MotaXterm或finalshell连接web服务器。(SSH:填写公网ip,用户名,密码)

lsb_release -a //查看服务器的版本
uname -a //查看Linux内核的版本
df -Th //查看磁盘空间的使用情况
cd /  //进入目录
ls -la  //查看Linux的目录结构

image.png

node.js 安装

  1. 进入opt/software(tips: 如果没有software要自己创建)

  2. 下载node.js安装包 :wget https://nodejs.org/dist/v16.5.0/node-v16.5.0-linux-x64.tar.xz

  3. 解压(ls 查看文件后缀,解压后修改文件名为node): tar xvJf node-v16.5.0-linux-x64.tar.xz

  4. 配置环境变量

读取环境变量文件命令:vi /etc/profile  

并插入export PATH=$PATH:/opt/software/node/bin/

保存命令:source /etc/profile

5. 配置安全组策略开放端口

  1. 将服务端代码拖拽进/home文件夹内,并cd /home,安装依赖npm i,启动服务npm run dev

npm,cnpm,yarn(自行选择)

  1. npm
npm config set registry https://registry.npm.taobao.org --global

npm config set disturl https://npm.taobao.org/dist --global

2. cnpm

npm install -g cnpm --registry=https://registry.npm.taobao.org

3. yarn

cnpm i yarn -g
yarn config set registry https://registry.npm.taobao.org
yarn add koa
yarn remove koa

PM2

  1. 安装pm2 npm i pm2 -g //查看是否安装成功

  2. 启动项目 pm2 start index.js --name hello 停止hello项目pm2 stop hello

  3. pm2命令

命令解释
pm2 start index.js --name my-server启动并命名进程
pm2 list显示所有进行
pm2 stop my-server停止my-server这个进程
pm2 restart all启动所有进程
pm2 delete my-server删除某个进程
pm2 show my-server查看某个进程的详情信息
pm2 logs查看日志信息

安装MongoDB / MySQL

MongoDB(4)

  1. cd /usr/local

  2. 下载: wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel80-4.4.18.tgz

  3. 解压后文件重命名为mongodb: tar -zxvf mongodb-linux-x86_64-rhel80-4.4.18.tgz

  4. 创建文件夹:

mkdir -p /var/mongodb/data
mkdir -p /var/mongodb/logs/
touch /var/mongodb/logs/log.log
touch /var/mongodb/mongodb.conf

5. 打开rc.loacl文件,添加CentOS开机启动项

chmod +x /etc/rc.d/rc.local

vi /etc/rc.d/rc.local

/usr/local/mongodb/bin/mongod --dbpath=/var/mongodb/data --logpath /var/mongodb/logs/log.log - fork

  1. 配置mongodb.conf文件

vim /var/mongodb/mongodb.conf

dbpath=/var/mongodb/data
logpath=/var/mongodb/logs/log.log
logappend = true 
port = 27017 
fork = true 
auth = true
bind_ip = 0.0.0.0

7. 配置path

vim ~/.bashrc
export PATH=$PATH:/usr/local/mongodb/bin
source ~/.bashrc

8. 启动mongodb服务: mongod --config /var/mongodb/mongodb.conf

  1. 远程加密,添加账号密码:
    mongo
    use admin
    db.createUser({user:"test",pwd:"123",roles:["root"]})

  2. 关闭mongodb服务: mongod -shutdown -dbpath=/var/mongodb/data

  3. 启动mongodb服务:mongod --config /var/mongodb/mongodb.conf

  4. 连接数据库(配置安全策略开放端口):

mongo
use admin
db.auth("test","123")  //输出1表示成功
use mybase
db.sm.insert({name:"hhhhhhhaaa"})

13. 开放端口27017

  1. 客户端连接(用户名和密码:test:123,公网IP:45.123.156.23):mongodb://test:123@123.249.122.1/mybase?authSource=admin

15.将本地数据库迁移到阿里云服务器

  1. 进入本地数据库data目录打开cmd:mongodump -h 127.0.0.1 -o ./yourpath

  2. 阿里云授权导入数据:mongorestore ./yourpath -u "test" -p "123" --authenticationDatabase "admin"

MySQL(8)

# 开机启动服务
systemctl enable mysqld
systemctl daemon-reload
 
#启动服务
systemctl start mysqld
 
# 重新启动服务
systemctl restart mysqld
 
# 查看服务当前状态
systemctl status mysqld
 
#停止服务
systemctl stop mysqld
 
# 永久性停止服务
systemctl disable mysqld

1. 下载 MySQL 的 Yum 源

下载MySQL的 Yum Repository。 一般需要根据 CentOS 版本选择 MySQL
下载命令:

wget https://dev.mysql.com/get/mysql80-community-release-el7-2.noarch.rpm

2. 用 yum命令安装下载好的rpm包。

yum -y install mysql80-community-release-el7-2.noarch.rpm

3. 安装 MySQL Server

yum -y install mysql-community-server

安装过程中可能遇到如下问题

not found 问题 在这里插入图片描述 解决办法


yum module disable mysql

后续继续执行


yum -y install mysql-community-server

** Error: GPG check FAILED 问题**


yum -y install mysql-community-server  --nogpgcheck

安装完成之后如下: 在这里插入图片描述

查看是否安装成功

启动 MySQL 命令


systemctl start  mysqld.service

查看 MySQL. 运行状态


systemctl status mysqld.service

在这里插入图片描述

其中Active后面代表状态启功服务后为active (running),停止后为inactive (dead)

4. 登陆 MySQL

初识时会给个固定密码,MySQL已经开始正常运行,要进入MySQL还得先找出此时root用户的密码,使用如下命令可以找出密码:


grep "password" /var/log/mysqld.log

登陆命令


mysql -u root -p

修改密码


mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';

新密码设置的时候如果设置的过于简单会报错, 如果想改个简单秘密,需要进行如下操作:

简单说明:


 show VARIABLES LIKE 'validate_password%';

在这里插入图片描述

密码的长度是由 validate_password_length决定的 ·validate_password_length的 计算公式如下:


validate_password_length = 
validate_password_number_count + validate_password_special_char_count + 
(2 * validate_password_mixed_case_count)

validate_password_policy 代表密码策略:

  • 默认是1:符合长度,且必须含有数字,小写或大写字母,特殊字符。
  • 设置为0判断密码的标准就基于密码的长度了。

要想密码简单,操作如下:


mysql> set global validate_password.policy=0;

validate_password_length代表密码长度,最小值为4


mysql> set global validate_password.length=4; 

操作完成后,结果如下: 在这里插入图片描述 此时可以设置一个很简单的密码,例如1234 abcd之类的。

还有一个问题,就是因为安装了 Yum Repository,以后每次yum操作都会自动更新,需要把这个卸载掉:


[root@localhost ~]# yum -y remove mysql80-community-release-el7-2.noarch

执行 SQL 时候,还可能遇到一个问题:this is incompatible with sql_mode=only_full_group_by


### Error querying database. Cause: java.sql.SQLSyntaxErrorException: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'yshopb2c.yx_store_order.create_time' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by ### The error may exist in co/yixiang/modules/order/service/mapper/StoreOrderMapper.java (best guess) ### The error may involve co.yixiang.modules.order.service.mapper.StoreOrderMapper.chartList-Inline ### The error occurred while setting parameters ### SQL: SELECT IFNULL(sum(pay_price),0) as num,DATE_FORMAT(create_time, '%m-%d') as time FROM yx_store_order where refund_status=0 and is_del=0 and paid=1 and pay_time >= ? GROUP BY DATE_FORMAT(create_time,'%Y-%m-%d') ORDER BY create_time ASC ### Cause: java.sql.SQLSyntaxErrorException: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'yshopb2c.yx_store_order.create_time' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by ; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'yshopb2c.yx_store_order.create_time' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_fu

问题原因:

通过查阅资料发现是因为下载安装的是最新版的mysql5.7.x版本,默认是开启了 only_full_group_by 模式的,但开启这个模式后,原先的类似 group by语句就报错,然后又把它移除了。就可以了。

操作如下: 找到 MySqL 配置文件my.cnf。 增加一行 sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION


[mysql]
default-character-set=utf8
[mysqld]
port = 3306
# Binary Logging
log-bin=mysql-bin
binlog-format=Row
#Server ID
server-id=201901
#basedir=D:\MySQL\mysql-5.7.14-winx64
#datedir=D:\MySQL\mysql-5.7.14-winx64\data
max_connections=200
character-set-server=utf8
default-storage-engine=INNODB
skip-grant-tables
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTI

5. 设置 MySQL 外网访问

操作如下

ini
复制代码
mysql> use mysql;
mysql> update user set host="%" where user='root';
mysql> flush privileges;

允许外网连接

ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root1234.';

mysql 配置说明


1 /etc/my.cnf 这是mysql的主配置文件
2 /var/lib/mysql mysql数据库的数据库文件存放位置
3 /var/log mysql数据库的日志输出存放位置
4.service mysqld start #启动
5.service mysqld restart #重启
6.service mysqld stop # 停掉

如果还是不能 方法,有可能是 阿里云 权限问题 在这里插入图片描述

设置安全组, 首先检查你的阿里或腾讯的服务器控制台是否开启3306端口访问权限,怎么看安全组在哪,自行百度。

在这里插入图片描述 连接成功后,结果如下: 在这里插入图片描述

前端项目部署到Linux

Nginx命令:

启动nginx: nginx

修改配置文件:vi /etc/nginx/nginx.conf

重启: nginx -s reload

完整有序停止: nginx -s quit

检查配置文件是否有问题:nginx -t
  1. 前端项目打包

  2. 安装nginx: yum install nginx

  3. 启动nginx: nginx

  4. 修改配置文件(修改后重启Nginx,nginx -s reload):vi /etc/nginx/nginx.conf

upstream back {
  server 127.0.0.1:3000;
}

server {
  listen 80;
  server_name xxxx;
  access_log /data/wwwlogs/xxxx.log combined;

  location / {
    root /a_ssyg/web;
    index index.html;
    try_files $uri $uri /index.html;
  }
  location /upload {
    alias /a_ssyg/server/static/upload;
  }
  location /api/v1 {
      proxy_pass http://back;
      proxy_redirect off;
      proxy_set_header Host $http_host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

  location /admin {
    alias /a_ssyg/admin;
    index index.html;
  }
  location /webtest {
    alias /a_ssyg/webtest;
    index index.html;
 
  }
 
}
      
// vue.config

  publicPath: '/admin/',
  outputDir: 'dist',
  assetsDir: 'static',

  
  publicPath: '/webtest/',
  outputDir: 'dist',
  assetsDir: 'static',