Centos 7 安装和配置Redis

一. 安装

操作系统:Centos 7. 最小化安装

redis版本: 4.0.6

服务器地址:***

第一步:下载redis安装包(如果有新的,下载最新的redis安装包)

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

1
2
3
4
5
6
7
8
9
10
11
[root@iZwz991stxdwj560bfmadtZ local]# wget http://download.redis.io/releases/redis-4.0.6.tar.gz
--2017-12-13 12:35:12--  http://download.redis.io/releases/redis-4.0.6.tar.gz
Resolving download.redis.io (download.redis.io)... 109.74.203.151
Connecting to download.redis.io (download.redis.io)|109.74.203.151|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1723533 (1.6M) [application/x-gzip]
Saving to: ‘redis-4.0.6.tar.gz’
 
100%[==========================================================================================================>] 1,723,533    608KB/s   in 2.8s  
 
2017-12-13 12:35:15 (608 KB/s) - ‘redis-4.0.6.tar.gz’ saved [1723533/1723533]

第二步:解压压缩包

tar -zxvf redis-4.0.6.tar.gz

1
[root@iZwz991stxdwj560bfmadtZ local]# tar -zxvf redis-4.0.6.tar.gz

 

第三步:yum安装gcc依赖

yum install gcc

1
[root@iZwz991stxdwj560bfmadtZ local]# yum install gcc  
1
遇到选择,输入y即可< br >< br >< br >

第四步:跳转到redis解压目录下

cd redis-4.0.6

1
[root@iZwz991stxdwj560bfmadtZ local]# cd redis-4.0.6

第五步:编译安装

make MALLOC=libc  

1
[root@iZwz991stxdwj560bfmadtZ redis-4.0.6]# make MALLOC=libc

  

将/usr/local/redis-4.0.6/src目录下的文件加到/usr/local/bin目录

cd src && make install

1
2
3
4
5
6
7
8
9
10
[root@iZwz991stxdwj560bfmadtZ redis-4.0.6]# cd src && make install
     CC Makefile.dep
 
Hint: It's a good idea to run 'make test' ;)
 
     INSTALL install
     INSTALL install
     INSTALL install
     INSTALL install
     INSTALL install

  

第六步:测试是否安装成功 

先切换到redis src目录下

1
[root@iZwz991stxdwj560bfmadtZ redis-4.0.6]# cd src

1、直接启动redis

./redis-server

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
[root@iZwz991stxdwj560bfmadtZ src]# ./redis-server
18685:C 13 Dec 12:56:12.507 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
18685:C 13 Dec 12:56:12.507 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=18685, just started
18685:C 13 Dec 12:56:12.507 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
                 _._                                                 
            _.-``__ ''-._                                            
       _.-``    `.  `_.  ''-._           Redis 4.0.6 (00000000/0) 64 bit
   .-`` .-```.  ```\/    _.,_ ''-._                                  
  (    '      ,       .-`  | `,    )     Running in standalone mode
  |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
  |    `-._   `._    /     _.-'    |     PID: 18685
   `-._    `-._  `-./  _.-'    _.-'                                  
  |`-._`-._    `-.__.-'    _.-'_.-'|                                 
  |    `-._`-._        _.-'_.-'    |           http://redis.io       
   `-._    `-._`-.__.-'_.-'    _.-'                                  
  |`-._`-._    `-.__.-'    _.-'_.-'|                                 
  |    `-._`-._        _.-'_.-'    |                                 
   `-._    `-._`-.__.-'_.-'    _.-'                                  
       `-._    `-.__.-'    _.-'                                      
           `-._        _.-'                                          
               `-.__.-'                                              
 
18685:M 13 Dec 12:56:12.508 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
18685:M 13 Dec 12:56:12.508 # Server initialized
18685:M 13 Dec 12:56:12.508 # 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.
18685:M 13 Dec 12:56:12.508 # 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.
18685:M 13 Dec 12:56:12.508 * Ready to accept connections  

如上图:redis启动成功,但是这种启动方式需要一直打开窗口,不能进行其他操作,不太方便。

按 ctrl + c可以关闭窗口。

 

如果执行安装命令时出现“没有这个目录”的错误,可以尝试删除解压目录,然后再重新解压一次。

至此,安装完成。

二. 配置Redis随机启动

1.把redis配置为随机启动,类似于windows的服务,开机启动。

 centos下配置随机启动需要在目录/etc/init.d中添加启动脚本,启动脚本的模板在redis源代码目录的utils文件夹中:redis_init_script

我们把这个文件复制到/etc/init.d文件夹中,并重命名为redis_6379, 我们这个服务名也就为redis_6379了

再来看下这个文件的内容:

#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
# as it does use of the /proc filesystem.
 
REDISPORT = 6379
EXEC = / usr / local / bin / redis - server
CLIEXEC = / usr / local / bin / redis - cli
 
PIDFILE = / var / run / redis_${REDISPORT}.pid
CONF = "/etc/redis/${REDISPORT}.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  - / proc / ${PID} ]
                 do
                     echo  "Waiting for Redis to shutdown ..."
                     sleep  1
                 done
                 echo  "Redis stopped"
         fi
         ;;
     * )
         echo  "Please use start or stop as first argument"
         ;;
esac

注意:

第四行和第五行,是我们后加入的。

$EXEC $CONF 代码中变量赋值,exec为redis-server命令路径,conf为配置文件,配置文件为/etc/redis/6379.conf,这个文件还没有,我们下面来配置它:看到里面的start stop命令,应该就非常熟悉了。注意start命令中,执行的启动命令为

创建目录/etc/redis。 配置文件的模板还在在redis源码中找:redis-4.0.2/redis.conf

将这个配置文件复制到/etc/redis目录 ,并重命名为6379.conf。

打开这个文件并修改:

这里贴出关键修改代码:

# 注释掉它,以便让外网访问
# bind 127.0.0.1
 
# 关闭保护模式
protected - mode no
 
# Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程
# 启用守护进程后,Redis会把pid写到一个pidfile中,在/var/run/redis.pid
daemonize yes
 
 
# 当Redis以守护进程方式运行时,Redis默认会把pid写入/var/run/redis.pid文件,可以通过pidfile指定
pidfile  / var / run / redis_6379.pid
 
# 指定Redis监听端口,默认端口为6379
# 如果指定0端口,表示Redis不监听TCP连接
port  6379
 
 
# 工作目录.
# 指定本地数据库存放目录,文件名由上一个dbfilename配置项指定
#
# Also the Append Only File will be created inside this directory.
#
# 注意,这里只能指定一个目录,不能指定文件名
dir  / var / redis / 6379

最后一行 /var/redis/6379 这个目录还没有,需要我们创建,用于存放redis的持久化文件。

然后执行命令:

#设置开机执行redis脚本
chkconfig redis_6379 on

通过上面的操作后,我们就可以通过 如下命令启动,停止redis了

service redis_6379 start
 
service redis_6379 stop
 

猜你喜欢

转载自www.cnblogs.com/huyueping/p/9437053.html