发布于 

cenos8安装redis7.0.11

1.下载:

1:https://redis.io/download/
2:https://download.redis.io/releases/?_gl=1*10x9wj*_ga*MTQ5MDEyMDY4Mi4xNjk1MTE3MjI2*_ga_8BKGRQKRPV*MTY5NTI2NDM2MS4yLjAuMTY5NTI2NDM2NC41Ny4wLjA.

下载好后把安装包上传至服务器

2.安装:

###解压redis:

1
2
[root@node202 ~]# cd /usr/local/soft/
[root@node202 soft]# tar -zxvf redis-7.0.11.tar.gz

###安装:

1
2
3
4
[root@node202 soft]# cd redis-7.0.11/
#安装必要类库
[root@node202 redis-7.0.11]# yum -y install gcc automake autoconf libtool make
[root@node202 redis-7.0.11]# make & make install

###启动

1
2
[root@node202 redis-7.0.11]# cd src
[root@node202 src]# ./redis-server /usr/local/soft/redis-7.0.11/redis.conf

日志:

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
8789:C 11 May 2023 01:53:49.076 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8789:C 11 May 2023 01:53:49.076 # Redis version=7.0.11, bits=64, commit=00000000, modified=0, pid=8789, just started
8789:C 11 May 2023 01:53:49.076 # Configuration loaded
8789:M 11 May 2023 01:53:49.077 * Increased maximum number of open files to 10032 (it was originally set to 1024).
8789:M 11 May 2023 01:53:49.077 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 7.0.11 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 8789
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'

8789:M 11 May 2023 01:53:49.078 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
8789:M 11 May 2023 01:53:49.078 # Server initialized
8789:M 11 May 2023 01:53:49.078 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
8789:M 11 May 2023 01:53:49.078 * Ready to accept connections

###使用

1
2
3
4
5
6
[root@node202 src]# ./redis-cli 
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"
127.0.0.1:6379>

##配置

创建一个用于存储 Redis 配置文件目录(/etc/redis)和数据目录(/var/redis):

1
2
mkdir -p /etc/redis
mkdir -p /var/redis

复制配置文件:

1
cp /usr/local/soft/redis-7.0.11/redis.conf /etc/redis/

复制编辑启动脚本:

1
cp /usr/local/soft/redis-7.0.11/utils/redis_init_script /etc/init.d/redis
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
vim /etc/init.d/redis
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

### BEGIN INIT INFO
# Provides: redis_6379
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Redis data structure server
# Description: Redis data structure server. See https://redis.io
### END INIT INFO

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/redis.conf"

case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart)
"$0" stop
sleep 3
"$0" start
;;
*)
echo "Please use start or stop or restart as first argument"
;;
esac

设置开机自启动:

1
2
3
4
5
6
chmod +x /etc/init.d/redis  #给脚本设置权限

chkconfig --add redis #添加redis服务

chkconfig redis on #设置redis服务开机启动

完结。

解决服务器Redis无法连接问题

找到你的redis配置文件,进行以下步骤修改。(本人的在/etc/redis.conf,如果找不到,直接创建一个,然后度娘一个默认的redis配置文件粘贴上去即可,启动时使用命令redis-cli +文件路径,下文会讲)

修改bind,默认为bind 127.0.0.1,将其注释(前面加个#),如果没有找到bind 127.0.0.1或已经注释,跳过此步,注意搜索一下127.0.0.1,可能不止1个地方有

1
2
3
4
5
6
7
# By default Redis listens for connections from all the network interfaces
# available on the server. It is possible to listen to just one or multiple
# interfaces using the "bind" configuration directive, followed by one or
# more IP addresses.
#
# Examples:
# bind 127.0.0.1

关闭保护模式,默认为protected-mode yes,将yes修改为no,如果没有找到protected-mode yes,可以随意另起一行添加protected-mode no;或已经修改为protected-mode no,跳过此步。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
# "bind" directive.
# 2) No password is configured.
#
# The server only accepts connections from clients connecting from the
# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
# sockets.
#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode no

如果有需求,设置密码(没有需求则跳过),添加一行requirepass 123456,作用是设置连接密码为123456,如有需求可以修改密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
################################## SECURITY ###################################

# Require clients to issue AUTH <PASSWORD> before processing any other
# commands. This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
# requirepass foobared
requirepass 123456

重启你的redis,在安装redis的位置使用命令redis-cli shutdown,然后再使用命令./redis-server /etc/redis.conf启动redis,请注意,如果你修改的配置文件在其他地方:例如/opt/redis/redis.conf,请你使用./redis-server /opt/redis/redis.conf启动redis。
如果启动后无法进行其他操作,请使用Ctrl+C结束该进程后,找到刚刚修改的配置文件,找到并修改为或者添加daemonize=yes(稍微提一嘴,windows版本不支持),然后再启动redis。

1
daemonize=yes

最后关闭你的防火墙,或者将redis加入白名单,这里只做关闭处理,这一步操作因linux版本而异

1
2
systemctl status firewalld.service  #查看防火墙状态
systemctl stop firewalld.service #关闭防火墙