CentOS系统配置redis
1.切换到
/usr/sr
cd /usr/src
wget http://download.redis.io/releases/redis-3.2.0.tar.gz
2.解压,安装
tar xzf redis-3.2.0.tar.gz //解压
cd redis-3.2.0 //进入redis所在目录
make && make install //安装redis
3.进入redis目录,修改redis.conf
daemonize yes //修改daemonize为yes,即默认以后台程序方式运行
loglevel notice
logfile /var/log/redis.log
dir ./
4.设置系统的overcommit_memory,执行
vi /etc/sysctl.conf
在文件中添加一行,保存::wq!
vm.overcommit_memory = 1 //这个是指定内核针对内存分配的策略,其值可以是0、1、2
注:0: 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
1: 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
2: 表示内核允许分配超过所有物理内存和交换空间总和的内存
5.添加启动脚本,执行:
vi /etc/init.d/redis
#!/bin/sh # # redis Startup script for Redis Server # # chkconfig: - 90 10 # description: Redis is an open source, advanced key-value store. # # processname: redis-server # config: /etc/redis.conf # pidfile: /var/run/redis.pid REDISPORT=6379 EXEC=/usr/local/bin/redis-server REDIS_CLI=/usr/local/bin/redis-cli PIDFILE=/var/run/redis.pid
# 这里就是redis的目录 CONF="/usr/src/redis-3.2.0/redis.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 if [ "$?"="0" ] then echo "Redis is running..." fi ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE does not exist, process is not running" else PID=$(cat $PIDFILE) echo "Stopping ..." $REDIS_CLI -p $REDISPORT SHUTDOWN while [ -x ${PIDFILE} ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi ;; restart|force-reload) ${0} stop ${0} start ;; *) echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2 exit 1 esac
6.设置权限和开机启动:
chmod +x /etc/init.d/redis
chkconfig --add redis
chkconfig redis on
7.如果需要外网访问,需要给redis配置密码,执行以下命令即可
redis-cli
config set requirepass admin
查看密码
config get requirepass
内网如果测试
redis-cli
auth admin
然后进行其他操作
8.测试redis
redis-cli
redis常用命令
如果遇到redis服务不可用:比如下
[root@SNDA-192-168-1-114 ~]# redis-cli
Could not connect to Redis at 127.0.0.1:6379: Connection refused
Could not connect to Redis at 127.0.0.1:6379: Connection refused
执行以下命令即可
[root@SNDA-192-168-1-114 ~]# redis-server /usr/src/redis-3.2.0/redis.conf
[root@SNDA-192-168-1-114 ~]# redis-server /usr/src/redis-3.2.0/redis.conf
[root@SNDA-192-168-1-114 ~]# redis-cli
关闭redis连接
redis 127.0.0.1:6379> QUIT