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
1 #拷贝conf文件到/etc目录 2 cp /opt/redis/redis-4.0.1/redis.conf /etc/redis.conf 3 4 5 #redis.conf 参数说明 6 7 ################################## NETWORK ##################################### 8 9 #绑定的主机地址 10 bind 127.0.0.1 11 12 #保护模式,是否容许 没有认证配置的主机或接口链接redis,默认是启动保护模式,则不容许这种状况 13 protected-mode yes 14 15 #指定redis的监听端口,默认端口是6379,做者在本身的一篇博文中解释了为何选用6379做为默认端口,由于6379在手机按键上MERZ对应的号码,而MERZ取自意大利歌女Alessia Merz的名字,嗯,你开发的,你说了算。 16 port 6379 17 18 # In high requests-per-second environments you need an high backlog in order 19 # to avoid slow clients connections issues. Note that the Linux kernel 20 # will silently truncate it to the value of /proc/sys/net/core/somaxconn so 21 # make sure to raise both the value of somaxconn and tcp_max_syn_backlog 22 # in order to get the desired effect. 23 24 tcp-backlog 511 25 26 #客户端连接多长时间后关闭连接,单位是秒,指定为0,则表示关闭该功能 27 timeout 0 28 29 # A reasonable value for this option is 300 seconds, which is the new 30 # Redis default starting with Redis 3.2.1. 31 tcp-keepalive 300 32 33 ################################# GENERAL ##################################### 34 35 #Redis默认不是以守护进程的方式运行,能够经过该配置项修改,使用yes启用守护进程 36 daemonize yes 37 38 # If you run Redis from upstart or systemd, Redis can interact with your 39 # supervision tree. Options: 40 # supervised no - no supervision interaction 41 # supervised upstart - signal upstart by putting Redis into SIGSTOP mode 42 # supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET 43 # supervised auto - detect upstart or systemd method based on 44 # UPSTART_JOB or NOTIFY_SOCKET environment variables 45 # Note: these supervision methods only signal "process is ready." 46 # They do not enable continuous liveness pings back to your supervisor. 47 supervised no 48
1 #服务端启动 2 [root@bogon redis-4.0.1]# cd /usr/local/redis/ 3 [root@bogon redis]# ./bin/redis-server /etc/redis.conf 4 74537:C 13 Aug 18:53:30.774 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 5 74537:C 13 Aug 18:53:30.774 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=74537, just started 6 74537:C 13 Aug 18:53:30.774 # Configuration loaded 7 [root@bogon redis]# ps axu | grep redis 8 root 74538 0.6 0.2 145248 2168 ? Ssl 18:53 0:00 ./bin/redis-server 127.0.0.1:6379 9 root 74665 0.0 0.0 112648 968 pts/4 S+ 18:53 0:00 grep --color=auto redis 10 11 #客户端启动 12 redis-cli [-h 127.0.0.1] [-p 6379] 13 127.0.0.1:6379> ping 14 PONG 15 16 #存储键值对 17 127.0.0.1:6379> set name imooc 18 OK 19 20 #获取name对应的value 21 127.0.0.1:6379> get name 22 "imooc" 23 24 #获取全部keys 25 127.0.0.1:6379> keys * 26 1) "name" 27 28 #删除keys 29 127.0.0.1:6379> del name 30 (integer) 1 31 127.0.0.1:6379> get name 32 (nil) 33 34 #关闭服务端 35 redis-cli shutdow
redis最多支持16个数据,下标0-15表示第几个数据库。默认是在0号数据。切换数据库能够经过select dbnumber 来切换,也能够经过move 来移动key从当前数据到指定的数据库。python