Linux安装MySQL、nodejs、Java、redis、ftp服务器脚本

安装MySQL脚本

# 安装mysql
yum -y install mysql mysql-server

# 查看mysql的状态
service mysqld status

# 开启mysql服务
systemctl start  mysqld.service

# 查看mysql的状态
service mysqld status

# 执行sql语句,更改用户的密码与远程连接设置
mysql -u root -e "ALTER USER USER() IDENTIFIED BY '0925';use mysql;select Host, User from user;update user set Host='%' where User='root';select Host, User from user; flush privileges;"

安装Java

yum install -y java-1.8.0-openjdk-devel.x86_64

安装nodejs

yum -y install nodejs
npm config set https://registry.npmmirror.com/

安装redis(不过redis还有一些配置需要修改,如密码、开放的端口)

yum install redis -y
systemctl start redis
systemctl status redis

安装ftp服务器

yum install vsftpd -y
service vsftpd start
service vsftpd status

vi /etc/vsftpd/vsftpd.conf # 添加下面三行

pasv_enable=YES			# 允许被动模式
pasv_min_port=5000     # 被动模式下服务器使用的最小端口
pasv_max_port=5000     # 被动模式下服务器使用的最大端口

改为bash脚本如下

yum install vsftpd -y
echo -e "pasv_enable=YES\npasv_min_port=5000\npasv_max_port=5000" | sudo tee -a /etc/vsftpd/vsftpd.conf
service vsftpd start
service vsftpd status

在安装并开启好vsftpd服务后,还需要创建用户,如jack用户,采用如下命令

useradd jack
passwd jack

并且为它设置一下密码,然后就可以了,需要注意的是有一些用户默认是不可以采用ftp连接的,在 /etc/vsftpd/user_list 里面,采用如下命令可以查看到内容

nl /etc/vsftpd/user_list
1	# vsftpd userlist
2	# If userlist_deny=NO, only allow users in this file
3	# If userlist_deny=YES (default), never allow users in this file, and
4	# do not even prompt for a password.
5	# Note that the default vsftpd pam config also checks /etc/vsftpd/ftpusers
6	# for users that are denied.
7	root
8	bin
9	daemon
10	adm
11	lp
12	sync
13	shutdown
14	halt
15	mail
16	news
17	uucp
18	operator
19	games
20	nobody

简单引入依赖

<dependency>
    <groupId>commons-net</groupId>
    <artifactId>commons-net</artifactId>
    <version>3.8.0</version>
</dependency>

工具类代码

package com.boot.util;

import org.apache.commons.net.ftp.FTPClient;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

public class FtpUtil {
    private final String host;
    private final Integer port;
    private final String username;
    private final String password;
    private final String path;

    public FtpUtil(String host, Integer port, String username, String password, String path) {
        this.host = host;
        this.port = port;
        this.username = username;
        this.password = password;
        this.path = path;
    }

    private FTPClient getFtpClient() throws IOException {
        FTPClient ftpClient = new FTPClient();
        ftpClient.connect(host, port);
        ftpClient.login(username, password);
        ftpClient.enterLocalPassiveMode();
        ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
        ftpClient.changeWorkingDirectory(path);
        return ftpClient;
    }

    public void uploadFile(String fileName, InputStream inputStream) throws Exception {
        FTPClient ftpClient = getFtpClient();
        ftpClient.storeFile(new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1), inputStream);
        ftpClient.disconnect();
    }

    public void downloadFile(String fileName, OutputStream outputStream) throws Exception {
        FTPClient ftpClient = getFtpClient();
        try (InputStream inputStream = ftpClient.retrieveFileStream(new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1))) {
            byte[] buf = new byte[1024];
            int read;
            while ((read = inputStream.read(buf)) != -1) {
                outputStream.write(buf, 0, read);
            }
            ftpClient.disconnect();
        }
    }

    public static void main(String[] args) throws Exception {
        FtpUtil ftpUtil = new FtpUtil("****", 21, "****", "****", "****");
        ftpUtil.uploadFile("测试文件.txt", Files.newInputStream(Paths.get("C:/file/测试文件.txt")));

        try (FileOutputStream outputStream = new FileOutputStream("C:/file/测试文件2.txt")) {
            ftpUtil.downloadFile("测试文件.txt", outputStream);
        }
    }

}
  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值