参考:http://blog.csdn.net/ludonqin/article/details/47211109
一.安装redis
1) 下载redis安装包
可去官网http://redis.io ,也可通过wget命令:
[root@ip-172-31-46-4 ~]# wget http://download.redis.io/redis-stable.tar.gz
2) 解压
执行命令:
[root@ip-172-31-46-4 ~]# tar –zxvf redis-stable.tar.gz
3) 编译、安装
执行命令:
[root@ip-172-31-46-4 ~]# cd redis-stable
执行命令:
[root@ip-172-31-46-4 redis-stable]# make
在make成功以后,需要执行命令:
[root@ip-172-31-46-4 redis-stable]# make test
成功后可手动拷贝src目录下redis-server、redis-cli、redis-check-aof、redis-check-dump等至/usr/local/bin目录下,也可执行命令:
[root@ip-172-31-46-4 redis-stable]# make install
验证系统目录下启动脚本:
[root@ip-172-31-46-4 ~]# cd /usr/local/bin
[root@ip-172-31-46-4 bin]# ls -ltrh
total 26M
-rwxr-xr-x 1 root root 7.5M Jan 7 08:03 redis-server
-rwxr-xr-x 1 root root 5.4M Jan 7 08:03 redis-benchmark
-rwxr-xr-x 1 root root 5.5M Jan 7 08:03 redis-cli
-rwxr-xr-x 1 root root 7.5M Jan 7 08:03 redis-check-rdb
lrwxrwxrwx 1 root root 12 Jan 7 08:03 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 22K Jan 7 08:03 redis-check-aof
二.修改配置文件.conf
1) 创建配置文件目录,dump file 目录,进程pid目录,log目录等
配置文件一般放在/etc/下,创建redis目录
[root@ip-172-31-46-4 bin]# cd /etc/
[root@ip-172-31-46-4 etc]# mkdir redis
ll 查看创建的redis目录
~
dump file、进程pid、log目录等,一般放在/var/目录下,
[root@ip-172-31-46-4 etc]# cd /var/
[root@ip-172-31-46-4 var]# mkdir redis
[root@ip-172-31-46-4 var]# cd redis
[root@ip-172-31-46-4 redis]# mkdir data log run
至此,目录创建完毕
2) 修改配置文件,配置参数
首先拷贝解压包下的redis.conf文件至/etc/redis
[root@ip-172-31-46-4 redis]# cp ~/redis-stable/redis.conf /etc/redis/
打开配置文件:
[root@ip-172-31-46-4 redis]# vi /etc/redis/redis.conf
修改端口(默认6379)
# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 6379
修改pid目录为新建目录
# Creating a pid file is best effort: if Redis is not able to create it
# nothing bad happens, the server will start and run normally.
pidfile /var/redis/run/redis_6379.pid
修改dump目录为新建目录
# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir /var/redis/data
修改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 /var/redis/log/redis.log
修改配置文件使得redis在background运行(默认redis服务是console模式运行的)
# 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 yes
3)持久化
默认rdb,可选择是否开启aof,若开启,修改配置文件appendonly
4)启动redis,查看各目录下文件
[root@ip-172-31-46-4 redis]# cd /usr/bin/
[root@ip-172-31-46-4 bin]# redis-server /etc/redis/redis.conf
redis已启动,查看进程,dump, log, pid等
5)客户端连接redis
[root@ip-172-31-46-4 bin]# redis-cli
默认端口6379
6)至此,redis基础配置完毕,若有其他相关配置调整,可查找文档再修改
三.服务及开机自启动
1) 创建redis启动脚本
拷贝解压包下utils下redis启动脚本至/etc/init.d/
[root@ip-172-31-46-4 bin]# cp ~/redis-stable/utils/redis_init_script /etc/init.d/
修改脚本名称(也可不修改)为redis
[root@ip-172-31-46-4 bin]# mv /etc/init.d/redis_init_script /etc/init.d/redis
修改脚本pid及conf路径为实际路径
[root@ip-172-31-46-4 bin]# vi /etc/init.d/redis_init_script
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/redis/run/redis_${REDISPORT}.pid
CONF="/etc/redis/redis.conf"
至此,在/etc/init.d/目录下,已经可以通过:
service redis start/stop 命令启动和关闭redis
若在其他目录下,不能够使用这2个命令,请继续配置2),添加权限
2) 给启动脚本添加权限
chmod +x /etc/init.d/redis
实际命令,根据目录的不同,会不一样
相应的删除权限是
chmod –x /etc/init.d/redis
如果需要在开机的时候,redis服务自动启动,可继续3)
3) 设置自启动
chkconfig redis on
如果运行报错,提示
是因为没有在启动脚本里加入redis启动优先级信息,可添加如下
再次执行chkconfig redis on,成功
至此,自启动配置完毕
--------------------------------------------------------
安装可能出错的地方:
a.如果提示:
gcc command不识别,请自行安装gcc;
(执行:yum -y install gcc)
b.如果提示:
couldn’t execute tcl : no such file or dicrectory,请自行安装tcl;
(执行:yum install -y tcl)
c.如果提示:
zmalloc.h:51:31: error: jemalloc/jemalloc.h: No such file or directory
原因是编译依赖或原来编译遗留出现的问题
(执行:make distclean)
d.如果提示:
zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory
zmalloc.h:55:2: error: #error "Newer version of jemalloc required"
make[1]: *** [adlist.o] Error 1
make[1]: Leaving directory `/data0/src/redis-2.6.2/src'
make: *** [all] Error 2zmalloc.h:50:31: error: jemalloc/jemalloc.h:;
原因是jemalloc重载了Linux下的ANSI C的malloc和free函数
(执行:make MALLOC=libc)
进阶:
配置文件说明,请参考:http://www.cnblogs.com/kreo/p/4423362.html
redis集群搭建,请参考:http://www.cnblogs.com/wuxl360/p/5920330.html