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

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"
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值