- 1 环境准备
- #官网下载 redis 3.2.5版本
- wget http://download.redis.io/releases/redis-4.0.1.tar.gz
- #安装 C 编译环境
- yum -y install cpp binutils glibc glibc-kernheaders glibc-common glibc-devel gcc gcc-c++
- 2 安装
- 解压安装包后,进入文件目录编译,编译结束时,会提示 Hint: It's a good idea to run 'make test' ,建议在安装前先测试预安装下,make test预安装后,遇到错误:You need tcl 8.5 or newer in order to run the Redis test ,缺失安装包tcl,所以需要先安装这个 安装包后再次运行 make test,正常后再进行redis安装。
- 详细步骤如下:
- #解压二进制包
- tar -zvxf /opt/redis-3.2.5
- #进入到文件目录
- cd redis-3.2.5
- #编译
- make
- #测试安装(稍微耗费点时间)
- make test
- #可能会提醒需要安装最新版的tcl
- #yum install tcl
- #指定路径安装
- make PREFIX=/usr/local/redis install
- #拷贝conf文件到/etc目录
- cp /opt/redis/redis-4.0.1/redis.conf /etc/redis.conf
- #redis.conf 参数说明
- ################################## NETWORK #####################################
- #绑定的主机地址
- bind 127.0.0.1
- #保护模式,是否允许 没有认证配置的主机或接口连接redis,默认是启动保护模式,则不允许这种情况
- protected-mode yes
- #指定redis的监听端口,默认端口是6379,作者在自己的一篇博文中解释了为什么选用6379作为默认端口,因为6379在手机按键上MERZ对应的号码,而MERZ取自意大利歌女Alessia Merz的名字,嗯,你开发的,你说了算。
- port 6379
- # In high requests-per-second environments you need an high backlog in order
- # to avoid slow clients connections issues. Note that the Linux kernel
- # will silently truncate it to the value of /proc/sys/net/core/somaxconn so
- # make sure to raise both the value of somaxconn and tcp_max_syn_backlog
- # in order to get the desired effect.
- tcp-backlog 511
- #客户端链接多长时间后关闭链接,单位是秒,指定为0,则表示关闭该功能
- timeout 0
- # A reasonable value for this option is 300 seconds, which is the new
- # Redis default starting with Redis 3.2.1.
- tcp-keepalive 300
- ################################# GENERAL #####################################
- #Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程
- daemonize yes
- # If you run Redis from upstart or systemd, Redis can interact with your
- # supervision tree. Options:
- # supervised no - no supervision interaction
- # supervised upstart - signal upstart by putting Redis into SIGSTOP mode
- # supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
- # supervised auto - detect upstart or systemd method based on
- # UPSTART_JOB or NOTIFY_SOCKET environment variables
- # Note: these supervision methods only signal "process is ready."
- # They do not enable continuous liveness pings back to your supervisor.
- supervised no
- #服务端启动
- [root@bogon redis-4.0.1]# cd /usr/local/redis/
- [root@bogon redis]# ./bin/redis-server /etc/redis.conf
- 74537:C 13 Aug 18:53:30.774 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
- 74537:C 13 Aug 18:53:30.774 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=74537, just started
- 74537:C 13 Aug 18:53:30.774 # Configuration loaded
- [root@bogon redis]# ps axu | grep redis
- root 74538 0.6 0.2 145248 2168 ? Ssl 18:53 0:00 ./bin/redis-server 127.0.0.1:6379
- root 74665 0.0 0.0 112648 968 pts/4 S+ 18:53 0:00 grep --color=auto redis
- #客户端启动
- redis-cli [-h 127.0.0.1] [-p 6379]
- 127.0.0.1:6379> ping
- PONG
- #存储键值对
- 127.0.0.1:6379> set name imooc
- OK
- #获取name对应的value
- 127.0.0.1:6379> get name
- "imooc"
- #获取所有keys
- 127.0.0.1:6379> keys *
- 1) "name"
- #删除keys
- 127.0.0.1:6379> del name
- (integer) 1
- 127.0.0.1:6379> get name
- (nil)
- #关闭服务端
- redis-cli shutdow
redis 最多支持 16 个数据,下标 0-15 表示第几个数据库。默认是在 0 号数据。切换数据库可以通过 select dbnumber 来切换,也可以通过 move 来移动 key 从当前数据到指定的数据库。
来源: http://www.cnblogs.com/xinysu/p/7366142.html