首页 > 代码库 > linux下安装redis并配置

linux下安装redis并配置

redis官网上给出了安装步骤,这里做一下总结。

1、Download, extract and compile Redis with:

wget http://download.redis.io/releases/redis-3.2.9.tar.gz
tar xzf redis-3.2.9.tar.gz
cd redis-3.2.9
make

2、配置并启动redis。

# 备份redis配置文件
cp redis.conf redis.conf.bak

a>配置reids为后台驻留程序。打开redis.conf,找到daemonize,可以看到reids默认情况下不是后台驻留程序。

# By default Redis does not run as a daemon. Use yes if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize no
# 修改成:
daemonize yes

b>配置redis log文件路径。

# Specify the log file name. Also the empty string can be used to force
# Redis to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile ""
# 修改为如下,意思为把log文件放在redis安装目下logs/redis.log(要自己创建好目录结构并赋予读写权限)
logfile "logs/redis.log"

c>配置redis远程连接

   暂不配置

d>创建redis用户

groupadd redis
useradd -r -g redis -s /bin/false redis

e>修改redis目录拥有者为redis:redis

chown -R redis:redis ./

f>启动redis

# redis3.x
# 启动redis 禁止使用root启动!!!
# 修改redis.conf文件后需指定配置文件路径启动
sudo -u redis src/redis-server /usr/local/redis-3.2.9/redis.conf

# redis客户端连接命令
redis-cli -h 127.0.0.1 -p 6379 -a password

#

g>关闭redis

src/redis-cli shutdown

# 强制关闭redis,找到redis进程号,kill掉。
netstat -anp | grep redis
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      1132/src/redis-serv
kill -9 1132

 

可能遇到的问题:

1、在执行src/redis-cli shutdown时报错:“(error) ERR Errors trying to SHUTDOWN. Check logs.”。日志(上面配置了日志路径)如下:

# User requested shutdown...
* Saving the final RDB snapshot before exiting.
# Failed opening the RDB file dump.rdb (in server root dir /usr/local/redis-3.2.9) for saving: Permission denied
# Error trying to save the DB, cant exit.

解决方法:

You should check your redis.conf file to see the permissions in dir and dbfilename. 
If the file named in the dbfilename which is located in the path specified in the
dir path exists and the permission is also right. then the problem should be fixed. Hope this will help someone. P.S. To find the redis.conf file location, you can use the #ps ax | grep redis to check.
Usually it will be passed to the redis-server as input file. For the dir permissions:it should be 755, for the dbfilename, it should be 644 Sometimes you also need to use top command to check whether the user:group of
the redis
-server and the owner of dir are consistent.
i.e. The redis-server is running by redis:redis, but the dir is under root:root.
In this case, you need to chown redis:redis -R dir.

 

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; background-color: #ffffff } span.s1 { }</style> <style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; background-color: #ffffff } span.s1 { }</style> <style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; background-color: #ffffff } span.s1 { }</style>

linux下安装redis并配置