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