Linux 下 Redis 的安装

本贴最后更新于 2084 天前,其中的信息可能已经时过境迁

Linux 下 redis 的安装

1. 下载 redis 压缩包

[root@localhost local]# pwd
/usr/local
[root@localhost local]# wget http://download.redis.io/releases/redis-4.0.11.tar.gz

2. 解压缩 redis,并进入 redis 目录

[root@localhost local]# tar -zxvf redis-4.0.11.tar.gz

[root@localhost local]# cd redis-4.0.11/

3. 执行 make 操作进行编译

[root@localhost redis-4.0.11]# make

4. 执行 make install 进行安装

[root@localhost redis-4.0.11]# cd src/
[root@localhost src]# make install PREFIX=/usr/local/redis/

5. 在 redis 目录下建立 conf 文件夹目录

[root@localhost redis]# mkdir conf

6. 拷贝 redis.conf 配置文件到 conf 目录下

[root@localhost redis-4.0.11]# cp redis.conf /usr/local/redis/conf/

7. 启动 redis

[root@localhost redis]# ./bin/redis-server conf/redis.conf

如图:

上述启动只能在前台启动,一旦退出控制台 redis 就停止了,现在改为后台启动
编辑 redis.conf 文件,将 daemonize 的值 no 改为 yes

[root@localhost redis]# vim conf/redis.conf

8. 后台启动 redis

[root@localhost redis]# ./bin/redis-server conf/redis.conf 
7730:C 10 Aug 15:59:32.537 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7730:C 10 Aug 15:59:32.537 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=7730, just started
7730:C 10 Aug 15:59:32.537 # Configuration loaded

查看 redis 进程:

[root@localhost redis]# ps -ef | grep redis
root       7731      1  0 15:59 ?        00:00:00 ./bin/redis-server 127.0.0.1:6379
root       7757   2503  0 16:00 pts/0    00:00:00 grep --color=auto redis

9. 客户端连接 redis

[root@localhost redis]# ./bin/redis-cli 
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> 

至此,redis 安装完成。

10. 允许外网连接 redis

修改 redis.conf 配置文件,将 bind 127.0.0.1 改为 0.0.0.0

[root@localhost redis]# vim conf/redis.conf 

重启 redis,再次连接

[root@localhost redis]# ps -ef | grep redis
root       7876      1  0 16:05 ?        00:00:00 ./bin/redis-server 0.0.0.0:6379
root       7881   2503  0 16:05 pts/0    00:00:00 grep --color=auto redis
[root@localhost redis]# ./bin/redis-cli -h 192.168.118.140 -p 6379
192.168.118.140:6379> keys *
(empty list or set)
192.168.118.140:6379> 

现在可以使用 redis 可视化工具连接服务器上的 redis。

11. 使用密码访问 redis

上述安装完成并设置外网访问后,假如服务器 ip 地址暴露在外,可能会造成安全隐患,redis 提供了使用密码访问的方式
修改 redis.conf 文件,取消 requirepass 前的注释,并设置密码

[root@localhost redis]# vim conf/redis.conf

修改完成后重启 redis,连接 redis,并设置值:

[root@localhost redis]# ./bin/redis-cli -h 192.168.118.140 -p 6379
192.168.118.140:6379> set test redis
(error) NOAUTH Authentication required.
192.168.118.140:6379> 

发现没有权限执行写操作,此时获取权限的方式有如下两种:
第一种是连接完成后执行 auth+password 命令进行授权

192.168.118.140:6379> auth 1992.cool
OK
192.168.118.140:6379> set test redis
OK
192.168.118.140:6379> 

第二种是在连接的时候加上密码参数

[root@localhost redis]# ./bin/redis-cli -h 192.168.118.140 -p 6379 -a 1992.cool
Warning: Using a password with '-a' option on the command line interface may not be safe.
192.168.118.140:6379> set test2 redis2
OK
192.168.118.140:6379> 

可视化工具验证登录:


不听乱世的耳语,只过自己想要的生活

  • Redis

    Redis 是一个开源的使用 ANSI C 语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value 数据库,并提供多种语言的 API。从 2010 年 3 月 15 日起,Redis 的开发工作由 VMware 主持。从 2013 年 5 月开始,Redis 的开发由 Pivotal 赞助。

    284 引用 • 247 回帖 • 176 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...