Linux下Redis的安装、部署、卸载

本文详细介绍Redis在Linux环境下的安装步骤及部署过程,包括解决安装过程中遇到的问题、如何配置Redis服务自启动、设置密码验证等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Linux下Redis的安装和部署

一、Redis介绍

Redis是当前比较热门的NOSQL系统之一,它是一个key-value存储系统。和Memcache类似,但很大程度补偿了Memcache的不足,它支持存储的value类型相对更多,包括string、list、set、zset和hash。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作。在此基础上,Redis支持各种不同方式的排序。 和Memcache一样,Redis数据都是缓存在计算机内存中,不同的是,Memcache只能将数据缓存到内存中,无法自动定期写入硬盘,这就表示,一断电或重启,内存清空,数据丢失。所以Memcache的应用场景适用于缓存无需持久化的数据。而Redis不同的是它会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,实现数据的持久化。

二、Redis的安装

下面介绍在Linux环境下,Redis的安装与部署

1、首先上官网下载Redis 压缩包,地址:http://redis.io/download 下载稳定版3.0.7即可。

2、通过远程管理工具,将压缩包拷贝到Linux服务器中,执行解压操作

[root@localhost ~]# wget http://download.redis.io/releases/redis-3.0.7.tar.gz
--2018-08-15 14:39:52--  http://download.redis.io/releases/redis-3.0.7.tar.gz
正在解析主机 download.redis.io (download.redis.io)... 109.74.203.151
正在连接 download.redis.io (download.redis.io)|109.74.203.151|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1375200 (1.3M) [application/x-gzip]
正在保存至: “redis-3.0.7.tar.gz”

3、执行make 对Redis解压后文件进行编译

[root@localhost ~]# tar xzf redis-3.0.7.tar.gz 
[root@localhost ~]# cd redis-3.0.7/
[root@localhost redis-3.0.7]# make
####################报错
cd hiredis && make static
make[3]: 进入目录“/root/redis-3.0.7/deps/hiredis”
gcc -std=c99 -pedantic -c -O3 -fPIC  -Wall -W -Wstrict-prototypes -Wwrite-strings -g -ggdb  net.c
make[3]: gcc:命令未找到
make[3]: *** [net.o] 错误 127
make[3]: 离开目录“/root/redis-3.0.7/deps/hiredis”
make[2]: *** [hiredis] 错误 2
make[2]: 离开目录“/root/redis-3.0.7/deps”
make[1]: [persist-settings] 错误 2 (忽略)
    CC adlist.o
/bin/sh: cc: 未找到命令
make[1]: *** [adlist.o] 错误 127
make[1]: 离开目录“/root/redis-3.0.7/src”
make: *** [all] 错误 2
######################
#解决上述错误需要安装下面的插件  要等一段时间
[root@localhost ~]# yum -y install gcc gcc-c++ libstdc++-devel

编译完成之后,可以看到解压文件redis-3.0.7 中会有对应的src、conf等文件夹,这和windows下安装解压的文件一样,大部分安装包都会有对应的类文件、配置文件和一些命令文件。

[root@localhost ~]# cd redis-3.0.7/
[root@localhost redis-3.0.7]# make
cd src && make all
make[1]: 进入目录“/root/redis-3.0.7/src”
    CC adlist.o
In file included from adlist.c:34:0:
zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录
 #include <jemalloc/jemalloc.h>
                             ^
编译中断。


####解决方案是:
[root@localhost redis-3.0.7]# make MALLOC=libc

###成功安装如下所示:
    LINK redis-benchmark
    CC redis-check-dump.o
    LINK redis-check-dump
    CC redis-check-aof.o
    LINK redis-check-aof

Hint: It's a good idea to run 'make test' ;)

make[1]: 离开目录“/root/redis-3.0.7/src”

4、编译成功后,进入src文件夹,执行make install进行Redis安装

5、安装完成,界面如下

[root@localhost src]# make install

Hint: It's a good idea to run 'make test' ;)

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install

三、Redis的部署

安装成功后,下面对Redis 进行部署

1、首先为了方便管理,将Redis文件中的conf配置文件和常用命令移动到统一文件中

a)创建bin和redis.conf文件

复制代码代码如下:

[root@localhost src]# mkdir -p /usr/local/redis/bin
[root@localhost src]# mkdir -p /usr/local/redis/ect

b)执行Linux文件移动命令:

复制代码代码如下:

[root@localhost redis-3.0.7]# mv /root/redis-3.0.7/redis.conf /usr/local/redis/ect/
[root@localhost redis-3.0.7]# cd /root/redis-3.0.7/src/
[root@localhost src]# mv mkreleasehdr.sh  redis-benchmark redis-check-aof redis-check-dump redis-cli redis-server /usr/local/redis/bin
1、自启动

执行Redis-server 命令,启动Redis 服务

[root@localhost redis-3.0.7]# cd /usr/local/redis/bin/
[root@localhost bin]# ./redis-server 
21386:C 15 Aug 15:02:57.988 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
21386:M 15 Aug 15:02:57.988 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.0.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 21386
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

21386:M 15 Aug 15:02:57.991 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
21386:M 15 Aug 15:02:57.991 # Server started, Redis version 3.0.7
21386:M 15 Aug 15:02:57.991 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. 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.
21386:M 15 Aug 15:02:57.991 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
21386:M 15 Aug 15:02:57.991 * The server is now ready to accept connections on port 6379
21386:M 15 Aug 16:02:58.001 * 1 changes in 3600 seconds. Saving...
21386:M 15 Aug 16:02:58.007 * Background saving started by pid 22459
22459:C 15 Aug 16:02:58.015 * DB saved on disk
22459:C 15 Aug 16:02:58.017 * RDB: 0 MB of memory used by copy-on-write
21386:M 15 Aug 16:02:58.109 * Background saving terminated with success
2、设置后台启动

在redis.conf的配置文件里面。做如下的修改:

### 路径为/usr/local/redis/ect/redis.conf
daemonize no
修改为:
daemonize yes
#启动redis
[root@localhost root]# cd /usr/local/redis/
[root@localhost redis]# ./bin/redis-server ./ect/redis.conf 
[root@localhost redis]# ps -ef|grep -i redis
root      10921      1  0 09:46 ?        00:00:06 ./redis-server *:6379
root      12123  11516  0 10:58 pts/1    00:00:00 grep --color=auto -i redis
#关闭redis
[root@localhost redis]# ./bin/redis-cli shutdown
[root@localhost redis]# ps -ef|grep -i redis
root      12215  11516  0 10:59 pts/1    00:00:00 grep --color=auto -i redis
3、redis服务自动启动

在redis.conf的配置文件里面。做如下的修改:

### 路径为/usr/local/redis/ect/redis.conf
daemonize no
修改为:
daemonize yes

下面需要设置redis服务器开机自动启动:

要先让redis服务自动启动的话,首先需要在/etc/init.d目录下创建redis的启动脚本。

将redis安装目录下的utils/redis_init_script复制到/etc/init.d目录下,命名为redis(名字简单,使用方便)

[root@localhost redis]# cd /root/redis-3.0.7/utils/
[root@localhost utils]# cp redis_init_script /etc/init.d/redis
[root@localhost utils]# ll /etc/init.d/redis

继续编辑启动文件,修改其中指定的pid和配置文件。

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

首先创建存放pid的目录为/var/redis/run

[root@localhost init.d]# cd /var/
[root@localhost var]# mkdir redis
[root@localhost var]# cd redis/
[root@localhost redis]# mkdir run

接下来,我们需要把配置文件复制一份到/etc/redis这个目录下面去

[root@localhost redis]# cd /etc/
[root@localhost etc]# mkdir redis
[root@localhost etc]# cd redis
[root@localhost redis]# ll
总用量 0
[root@localhost redis]# cp /usr/local/redis/ect/redis.conf ./
[root@localhost redis]# ll

修改配置文件中的pid为前面配置的pid

pidfile /var/run/redis_6379.pid
修改为
pidfile /var/redis/run/redis_6379.pid

至此为止,我们已经可以通过service redis start/stop来启动和关闭redis服务了。

  最后只需要通过chkconfig redis on命令来设置开机启动即可。

  如果提示redis 服务不支持 chkconfig的话,只需要在/etc/init.d/redis这个启动脚本的第二行后面加上下面的内容即可。

# chkconfig:2345 90 10
#
# description:Redis is a persistent key-value database
[root@localhost ~]# chkconfig redis on
redis 服务不支持 chkconfig
[root@localhost ~]# vi /etc/init.d/redis
[root@localhost ~]# chkconfig redis on
[root@localhost ~]#
4、卸载

卸载redis非常的简单,只需要简单的三步

1、停止redis服务器

  首先,通过下面的命令查看redis服务是否在运行

[root@localhost ~]# ps aux|grep redis
root      2553  0.2  0.1  41964  1916 ?        Ssl  09:38   0:00 redis-server 127.0.0.1:6379
root      2565  0.0  0.0   6048   780 pts/0    S+   09:39   0:00 grep redis
[root@localhost ~]#

可以看到,在6379端口,有redis-server的监听

  通过下面的命令停止redis服务器。

[root@localhost ~]# redis-cli shutdown
[root@localhost ~]# ps aux|grep redis

root      2575  0.0  0.0   6048   780 pts/0    S+   09:41   0:00 grep redis
[root@localhost ~]#

  可以看到,已经停止了redis服务了。

  需要注意的是,由于我的redis命令都安装到/usr/local/bin目录下面了,并且添加到环境变量PATH里面了,所以可以直接运行。

2、删除make的时候生成的几个redisXXX的文件

[root@localhost local]# ll /usr/local/bin
总用量 30908
-rwxr-xr-x. 1 root root 4985307 92 21:13 redis-benchmark
-rwxr-xr-x. 1 root root 7185872 92 21:13 redis-check-aof
-rwxr-xr-x. 1 root root 7185872 92 21:13 redis-check-rdb
-rwxr-xr-x. 1 root root 5092475 92 21:13 redis-cli
lrwxrwxrwx. 1 root root      12 92 21:13 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 7185872 92 21:13 redis-server
[root@localhost local]# rm -f /usr/local/bin/redis*
[root@localhost local]# ll /usr/local/bin
总用量 0
[root@localhost local]#

3、顺便也删除掉解压后的文件目录和所以文件

[root@localhost local]# ll
总用量 40
drwxr-xr-x. 2 root root 4096 93 09:43 bin
drwxr-xr-x. 2 root root 4096 923 2011 etc
drwxr-xr-x. 2 root root 4096 923 2011 games
drwxr-xr-x. 2 root root 4096 923 2011 include
drwxr-xr-x. 2 root root 4096 923 2011 lib
drwxr-xr-x. 2 root root 4096 923 2011 libexec
drwxrwxr-x. 6 root root 4096 92 21:11 redis
drwxr-xr-x. 2 root root 4096 923 2011 sbin
drwxr-xr-x. 5 root root 4096 41 04:48 share
drwxr-xr-x. 2 root root 4096 923 2011 src
[root@localhost local]# rm -rf redis
[root@localhost local]# ll
总用量 36
drwxr-xr-x. 2 root root 4096 93 09:43 bin
drwxr-xr-x. 2 root root 4096 923 2011 etc
drwxr-xr-x. 2 root root 4096 923 2011 games
drwxr-xr-x. 2 root root 4096 923 2011 include
drwxr-xr-x. 2 root root 4096 923 2011 lib
drwxr-xr-x. 2 root root 4096 923 2011 libexec
drwxr-xr-x. 2 root root 4096 923 2011 sbin
drwxr-xr-x. 5 root root 4096 41 04:48 share
drwxr-xr-x. 2 root root 4096 923 2011 src
[root@localhost local]#

这样,redis就卸载完成了。

四、客户端redisClient

windows下redis可视化客户端redisClient的安装及基本使用

五、登录和密码设置

[root@localhost ~]# redis-cli   登录redis
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> config set requirepass test123
OK
127.0.0.1:6379> 
[root@localhost ~]# redis-cli
127.0.0.1:6379> auth test123
OK
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "test123"
### 完全卸载现有Redis 为了确保旧版本的 Redis 被彻底移除,在 Linux 中可以按照如下方法操作: 对于基于 Debian 的系统,可以通过 `apt-get` 来删除安装的服务包: ```bash sudo apt-get purge redis-server ``` 这会移除与 Redis 相关的所有配置文件和服务定义[^1]。 接着清理任何残留的数据和依赖项。先查找是否有剩余的 Redis 数据库实例正在运行,并安全关闭它们: ```bash redis-cli shutdown ``` 如果设置了密码,则需指定密码来访问 CLI 工具;同样地,如果有自定义端口号也需要指明: ```bash redis-cli -a password -p port_number shutdown ``` 之后定位到 `/usr/local/bin/` 或其他路径下找到所有以 "redis-" 开头的相关二进制文件并将其删除: ```bash ls -l /usr/local/bin/redis-* rm -rf /usr/local/bin/redis-* ``` 最后确认没有任何关于 Redis 的进程还在后台活动,可利用 ps aux | grep redis 查看是否存在未被终止的任务[^3]。 ### 新建安装准备 完成上述步骤后,建议重启计算机以确保所有的更改生效。随后可以从官方源获取最新稳定版的 Redis 发行包来进行新一次的部署工作。 #### 获取最新的 Redis 版本 前往官方网站下载页面或是使用 Git 克隆仓库获得最近发布的 tarball 文件: ```bash wget http://download.redis.io/releases/redis-stable.tar.gz tar xvzf redis-stable.tar.gz cd redis-stable make ``` 编译完成后,将生成好的二进制文件复制至合适的位置比如 `/usr/local/bin/` 下面以便全局调用: ```bash cp src/redis* /usr/local/bin/ ``` 创建一个新的配置文件夹用于保存定制化的设置选项: ```bash mkdir /etc/redis cp redis.conf /etc/redis/ ``` 编辑此配置文件调整监听地址、端口以及其他必要的参数以适应实际应用场景的需求[^4]。 #### 设置开机自动启动 为了让 Redis 可以随操作系统一同启动,还需要编写相应的 Systemd 服务单元文件放置于 `/lib/systemd/system/` 目录内: ```ini [Unit] Description=Redis In-Memory Data Store After=network.target [Service] User=nobody Group=nogroup ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf ExecStop=/usr/local/bin/redis-cli shutdown Restart=always [Install] WantedBy=multi-user.target ``` 保存修改后的文档命名为 `redis.service` ,然后执行以下指令使该服务成为默认加载的一部分: ```bash systemctl enable redis.service ``` 现在应该已经成功完成了对旧有 Redis 实例的清除以及新版软件的成功搭建!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值