Linux安装redis、配置及卸载

一、安装
1、我们通过在官网下载tar.gz的安装包,或通过wget的方式下载
[root@centos101 opt]# wget http://download.redis.io/releases/redis-4.0.1.tar.gz
--2017-09-03 09:51:27--  http://download.redis.io/releases/redis-4.0.1.tar.gz
正在解析主机 download.redis.io... 109.74.203.151
正在连接 download.redis.io|109.74.203.151|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1711660 (1.6M) [application/x-gzip]
正在保存至: “redis-4.0.1.tar.gz”

57% [=====================================>                             ] 981,091      298K/s eta(英国中部时
64% [==========================================>                        ] 1,105,869    309K/s eta(英国中部时
72% [===============================================>                   ] 1,237,657    321K/s eta(英国中部时
80% [====================================================>              ] 1,376,455    334K/s eta(英国中部时
86% [========================================================>          ] 1,478,801    341K/s eta(英国中部时
92% [============================================================>      ] 1,582,549    349K/s eta(英国中部时
100%[==================================================================>] 1,711,660    380K/s   in 4.6s

2、解压
[root@centos101 opt]# ll
总用量 1672
-rw-r--r--. 1 root root 1711660 7月  24 2017 redis-4.0.1.tar.gz
[root@centos101 opt]# pwd
/opt
[root@centos101 opt]# tar -zxvf redis-4.0.1.tar.gz -C /usr/local/software/
[root@centos101 opt]# cd /usr/local/software/
[root@centos101 software]# ll
drwxrwxr-x. 6 root root 4096 9月  22 09:52 redis-4.0.1
[root@centos101 software]#

3、修改当前redis-4.0.1文件夹为redis
[root@centos101 software]# mv redis-4.0.1/ redis/
[root@centos101 software]# ll
drwxrwxr-x. 6 root root 4096 9月  22 09:52 redis

4、接下来就是编译了,通过make命令,如果编译的时候报gcc命令找不到的话,可以通过下面的命令安装gcc命令,gcc是c的编译命令
yum install gcc-c++

下面就通过make来编译,make是自动编译,会根据Makefile中描述的内容来进行编译。
[root@centos101 redis]# make

5、可以看到在src目录下生成了几个新的文件。
[root@centos101 redis]# ll -tr src
-rw-r--r--. 1 root root   89528 9月  22 09:45 rax.o
-rwxr-xr-x. 1 root root 8309731 9月  22 09:45 redis-server
-rwxr-xr-x. 1 root root 8309731 9月  22 09:45 redis-sentinel
-rw-r--r--. 1 root root  225992 9月  22 09:45 redis-cli.o
-rwxr-xr-x. 1 root root 5730518 9月  22 09:45 redis-cli
-rw-r--r--. 1 root root   70240 9月  22 09:45 redis-benchmark.o
-rwxr-xr-x. 1 root root 5599558 9月  22 09:45 redis-benchmark
-rwxr-xr-x. 1 root root 8309731 9月  22 09:45 redis-check-rdb
-rwxr-xr-x. 1 root root 8309731 9月  22 09:45 redis-check-aof
-rw-r--r--. 1 root root   16088 9月  22 09:47 Makefile.dep
[root@centos101 redis]# 

为了使用方便,我们需要将这个几个文件加到/usr/local/bin目录下去。这个目录在Path下面的话,就可以直接执行这几个命令了。
[root@centos101 redis]# make install

可以看到,这几个文件就已经被加载到bin目录下了
[root@centos101 redis]# cd /usr/local/bin/
[root@centos101 bin]# ll
总用量 35424
-rw-r--r--. 1 root root     157 9月  22 09:51 dump.rdb
-rwxr-xr-x. 1 root root 5599558 9月  22 09:47 redis-benchmark
-rwxr-xr-x. 1 root root 8309731 9月  22 09:47 redis-check-aof
-rwxr-xr-x. 1 root root 8309731 9月  22 09:47 redis-check-rdb
-rwxr-xr-x. 1 root root 5730518 9月  22 09:47 redis-cli
lrwxrwxrwx. 1 root root      12 9月  22 09:47 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 8309731 9月  22 09:47 redis-server
[root@centos101 bin]#

6、下面启动服务器,来看看安装是否成功。使用redis-server命令。
[root@centos101 local]# redis-server
29248:C 22 Sep 10:38:51.244 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
29248:C 22 Sep 10:38:51.244 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=29248, just started
29248:C 22 Sep 10:38:51.244 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
29248:M 22 Sep 10:38:51.245 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 4.0.1 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 29248
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

29248:M 22 Sep 10:38:51.247 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
29248:M 22 Sep 10:38:51.247 # Server initialized
29248:M 22 Sep 10:38:51.247 # 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.
29248:M 22 Sep 10:38:51.247 # 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.
29248:M 22 Sep 10:38:51.247 * Ready to accept connections

看到这个界面的话,就表示安装成功了。
可以ctrl+c停止服务 或运行server-cli shutdown命令来关闭服务。

二、配置
1、可以看到,前面在启动redis服务器后,都是在前台启动的,需要重新启动一个客户端来进行登陆操作。这样非常不方便,
所以,我们需要设置后台启动。

在redis.conf的配置文件里面。做如下的修改:(大概在130多行)
daemonize no
修改为:
daemonize yes
[root@centos101 redis]# pwd
/usr/local/software/redis
[root@centos101 redis]# vi redis.conf

2、下面需要设置redis服务器开机自动启动:
要先让redis服务自动启动的话,首先需要在/etc/init.d目录下创建redis的启动脚本。
将redis安装目录下的utils/redis_init_script复制到/etc/init.d目录下,命名为redis(名字简单,使用方便)
[root@centos101 redis]# cd utils/
[root@centos101 utils]# cp redis_init_script /etc/init.d/redis
[root@centos101 utils]# ll /etc/init.d/redis 
-rwxr-xr-x. 1 root root 1175 9月  22 10:08 /etc/init.d/redis

3、在编辑启动脚本文件之前先创建pid的目录为/var/redis/run
[root@centos101 var]# mkdir redis
[root@centos101 var]# cd redis/
[root@centos101 redis]# mkdir run

我们需要把配置文件复制一份到/etc/redis这个目录下面去
[root@centos101 redis]# cd /etc/
[root@centos101 etc]# mkdir redis
[root@centos101 redis]# cp /usr/local/software/redis/redis.conf ./
[root@centos101 redis]# ll
总用量 60
-rw-r--r--. 1 root root 57769 9月  22 10:18 redis.conf

4、编辑启动脚本文件,修改其中指定的pid和配置文件。
[root@centos101 init.d]# pwd
/etc/init.d
[root@centos101 init.d]# vi redis
修改内容如下:
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"
修改为
PIDFILE=/var/redis/run/redis_${REDISPORT}.pid
CONF="/etc/redis/redis.conf"

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

5、最后只需要通过chkconfig redis on命令来设置开机启动即可。
如果提示redis 服务不支持 chkconfig的话,只需要在/etc/init.d/redis这个启动脚本的第二行后面加上下面的内容即可。
# chkconfig:2345 90 10
#
# description:Redis is a persistent key-value database
[root@centos101 ~]# chkconfig redis on
redis 服务不支持 chkconfig
[root@centos101 ~]# vi /etc/init.d/redis
[root@centos101 ~]# chkconfig redis on

三、卸载redis
卸载redis非常的简单,只需要简单的三步
1、停止redis服务器
首先,通过下面的命令查看redis服务是否在运行
[root@centos101 ~]# 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@centos101 ~]#
可以看到,在6379端口,有redis-server的监听
[root@centos101 ~]# redis-cli shutdown
[root@centos101 ~]# ps aux|grep redis
root      2575  0.0  0.0   6048   780 pts/0    S+   09:41   0:00 grep redis
[root@centos101 ~]#
可以看到,已经停止了redis服务了。
需要注意的是,由于我的redis命令都安装到/usr/local/bin目录下面了,并且添加到环境变量PATH里面了,所以可以直接运行。

2、删除make的时候生成的几个redisXXX的文件
[root@centos101 ~]# ll /usr/local/bin/
总用量 35424
-rw-r--r--. 1 root root     157 9月  22 09:51 dump.rdb
-rwxr-xr-x. 1 root root 5599558 9月  22 09:47 redis-benchmark
-rwxr-xr-x. 1 root root 8309731 9月  22 09:47 redis-check-aof
-rwxr-xr-x. 1 root root 8309731 9月  22 09:47 redis-check-rdb
-rwxr-xr-x. 1 root root 5730518 9月  22 09:47 redis-cli
lrwxrwxrwx. 1 root root      12 9月  22 09:47 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 8309731 9月  22 09:47 redis-server
[root@centos101 ~]# rm -rf /usr/local/bin/redis*
[root@centos101 ~]# ll /usr/local/bin/
总用量 0

3、顺便也删除掉解压后的文件目录和所以文件
[root@centos101 ~]# cd /usr/local/software/
[root@centos101 software]# ll
总用量 8
-rw-r--r--. 1 root root  157 9月  22 09:50 dump.rdb
drwxrwxr-x. 6 root root 4096 9月  22 09:52 redis
[root@centos101 software]# rm -rf redis/ dump.rdb
这样,redis就卸载完成了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值