linux下安装redis并配置

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

1、Download, extract and compile Redis with:

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

tar -zxvf redis-5.0.2.tar.gz
cd redis-5.0.2

yum install -y gcc
# 直接使用make命令会出现以下错误
fatal error: jemalloc/jemalloc.h: No such file or directory

make MALLOC=libc

# 直接make test会出现以下错误
You need tcl 8.5 or newer in order to run the Redis test
# 解决方法
yum install -y tcl
make test

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-1>配置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"

b-2>配置db file location

# The working directory.
# dir ./
dir /usr/local/redis-5.0.2/

c>配置redis远程连接

#注释掉所有bind 127.0.0.1
#bind 127.0.0.1 ::1
#bind 127.0.0.1
bind 0.0.0.0

#创建密码
requirepass password

 d>创建redis用户

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

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

chown -R redis:gredis ./

f>启动redis

# redis3.x
# 启动redis。禁止使用root启动!!!注意可能造成的文件的读写权限。
# 修改redis.conf文件后需指定配置文件路径启动
sudo -u redis src/redis-server ./redis.conf

# redis客户端连接命令
src/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, can't 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.

 

QQ技术交流群:282575808

--------------------------------------

声明: 原创文章,未经允许,禁止转载!

--------------------------------------

posted @ 2017-07-05 14:12  景岳  阅读(11273)  评论(0编辑  收藏  举报