1. 下载源码包

你可以使用下面命令安装5.0.3版本,也可以去Redis官网选择需要的版本。

1
wget http://download.redis.io/releases/redis-5.0.3.tar.gz

2. 解压并进入redis目录

1
2
3
4
cd /usr/local
tar xzf /root/redis-4.0.1.tar.gz
mv redis-4.0.1 redis
cd redis

3. 编译安装

1
make && make install

4. 拷贝配置文件

1
cp redis.conf /etc/

5. 查看参数

1
redis-server --help

6. 查看版本信息

1
redis-server -v

7. 启动redis

1
redis-server /etc/redis.conf

8. 退出关闭

1
Ctrl+z

9. 设置后台运行

1
2
vim /etc/redis.conf
#修改 `daemonize no` 为 `daemonize yes`

10. 设置开机启动脚本/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
#!/bin/sh
# chkconfig: 2345 10 90
# description: Start and Stop redis
REDISPORT=6379
EXEC=/usr/local/redis/src/redis-server
CLIEXEC=/usr/local/redis/src/redis-cli
PIDFILE=/var/run/redis_$ {
REDISPORT
}
.pid
CONF="/etc/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

注意修改权限: chmod 777 /etc/init.d/redis

11. 测试开机启动

1
2
3
4
5
chkconfig redis on
reboot
[root@localhost ~]# ps aux | grep redis
root 10219 0.0 0.2 50296 9728 ? Ssl 16:30 0:00 /usr/local/redis/src/redis-server 127.0.0.1:6379
root 10336 0.0 0.0 112716 980 pts/0 S+ 16:30 0:00 grep --color=auto redis

12. 基本命令

1
2
3
[root@localhost ~]# service redis start
[root@localhost ~]# service redis restart
[root@localhost ~]# service redis stop

13. 远程访问

在/etc/redis.conf文件中修改:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
################################## NETWORK #####################################
# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server. # It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses. #
# Examples: #
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1

# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 loopback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running). #
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# bind 127.0.0.1
bind 0.0.0.0

注意:大约在69行左右,而不是在55行左右出添加:bind 0.0.0.0

找到90行左右修改protected-mode yes为protected-mode no。

OK,可以远程访问了。

注意,这是在测试阶段使用,生产环境记得修改。


本文早期发布于个人 CSDN 博客:查看原文

站内搜索

没有找到内容!