1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
#!/bin/bash
# 安装必须工具
yum -q -y install gcc wget tar
cd /usr/local/
# 下载并解压redis, 其他版本替换版本号即可 6.2.14
wget http://download.redis.io/releases/redis-5.0.4.tar.gz
tar -zxf redis-5.0.4.tar.gz
cd redis-5.0.4
# 编译和安装redis
make
make install PREFIX=/usr/local/redis
cp ./redis.conf ../redis/bin/
cd /usr/local/redis/bin/
# 创建软链接
ln -s /usr/local/redis/bin/redis-cli /usr/bin/redis-cli
ln -s /usr/local/redis/bin/redis-server /usr/bin/redis-server
# 生成随机密码
PD=`date | md5sum | cut -b 1-8`
### 修改配置文件
# 后台运行
sed -i 's/^daemonize\ no$/daemonize\ yes/g' redis.conf
# 关闭保护模式
sed -i 's/^protected-mode\ yes$/protected-mode\ no/g' redis.conf
# 修改密码
sed -i "s/^#\ requirepass\ foobared$/requirepass\ $PD/g" redis.conf
### 设置开机启动
touch /etc/systemd/system/redis.service
cat>/etc/systemd/system/redis.service<<EOF
[Unit]
Description=redis-server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/bin/redis.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start redis.service
systemctl enable redis.service
### 打印随机密码
echo '### PASSWORD ###'
echo $PD
echo '################'
echo ''
echo '############################################'
echo '### Redis Installed In /usr/local/redis/ ###'
echo '############################################'
redis-server -v
echo '################'
# 查询redis进程
ps -ef | grep -v grep | grep redis-server
|