一、安装redis
环境:centos7/redis3.0.7(其它版本安装方法一致)
#mkdir /usr/local/redis
进入此文件夹,解压官网下载的redis-3.0.7.tar.gz 或是使用下面命令在线下载,
wget http://download.redis.io/releases/redis-3.0.7.tar.gz
#tar -zxvf redis-3.0.7.tar.gz(解压)#make(编译,编译结束后src文件夹下会多出redis-server,redis-cli等脚本)
测试一下:
[root@ser211 redis]# ./usr/local/redis/src/redis-server1074:C 03 Feb 16:34:16.941 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf 1074:M 03 Feb 16:34:16.943 * Increased maximum number of open files to 10032 (it was originally set to 1024). 1074:M 03 Feb 16:34:16.943 # Warning: 32 bit instance detected but no memory limit set. Setting 3 GB maxmemory limit with 'noeviction' policy now. _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 3.0.7 (00000000/0) 32 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 1074 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 1074:M 03 Feb 16:34:16.944 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 1074:M 03 Feb 16:34:16.944 # Server started, Redis version 3.0.7 1074:M 03 Feb 16:34:16.944 # 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. 1074:M 03 Feb 16:34:16.944 * The server is now ready to accept connections on po
此时redis安装成功。
二、配置redis并添加到服务
将/usr/local/redis/utils/redis_init_script脚本拷贝到/etc/init.d/redis
cp -r /usr/local/redis/redis-3.0.7/utils/redis_init_script /etc/init.dmv redis_init_script redis(将redis_init_script重命名为redis)
打开脚本并修改成下面这样:
#!/bin/sh #chkconfig: 2345 80 90 (加了这一行是后面要把redis添加成系统服务的标识,不加的话后面会报服务 redis 不支持 chkconfig)# Simple Redis init.d script conceived to work on Linux systems # 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.pid (保持跟conf里面的pid一张,不然开和关pid不一样,会出现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 [ -x /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
仔细阅读脚本中的配置,创建数据目录/var/redis/6379,将redis-server、redis-cli移动到/usr/local/bin下,将/usr/local/redis/redis.conf移动到/etc/redis/6379.conf,并做如下修改:
daemonize yes //由no改为yes,以后台方式运行 dir /var/redis/6379 //配置数据库文件存放目录
最后将redis添加为服务:
# chkconfig --add redis
测试:
# service redis startStarting Redis server...
打开客户端:
[root@run]# service redis startStarting Redis server...[root@run]# redis-cli127.0.0.1:6379> set hi helloOK127.0.0.1:6379> get hi"hello"127.0.0.1:6379>
关闭:
[root@run]# service redis stopStopping ...Redis stopped
到此安装配置完成