学习redis最好的办法就是动手一边学习尝试它。在学习redis最核心内容之前,先来熟悉下如何安装及运行redis,以及redis的基础知识,下面一边介绍一边实践。
一、redis安装
安装redis需要了解redis的版本制定规则以选择最合适自己的版本,redis约定次版本号(第一个小数点后的数字)为偶数的是稳定版本,如2.4、2.6等,奇数为非稳定版,建议使用稳定版本的redis。
1、源码编译安装(推荐)
下载地址:http://download.redis.io/redis-stable.tar.gz redis的安装非常简单,如下:
tar zxvf redis-3.0.2.tar.gz cd redis-3.0.2 make
2、启动和停止redis
在启动之前需要了解redis可执行文件有哪些以及它们的作用。
最常用的就是redis-server和redis-cli,而redis-server就是redis的服务器,启动它就是启动redis了。redis-cli是它自带的命令行客户端。
1)直接启动(开发环境)
直接启动适合开发环境,比较简单,运行redis-server即可,默认端口是6379,通过--port参数可自定义端口
[root@darren src]# redis-server 26263:C 08 Oct 15:10:24.465 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf 26263:M 08 Oct 15:10:24.468 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ‘‘-._ _.-`` `. `_. ‘‘-._ Redis 3.0.2 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ‘‘-._ ( ‘ , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|‘` _.-‘| Port: 6379 | `-._ `._ / _.-‘ | PID: 26263 `-._ `-._ `-./ _.-‘ _.-‘ |`-._`-._ `-.__.-‘ _.-‘_.-‘| | `-._`-._ _.-‘_.-‘ | http://redis.io `-._ `-._`-.__.-‘_.-‘ _.-‘ |`-._`-._ `-.__.-‘ _.-‘_.-‘| | `-._`-._ _.-‘_.-‘ | `-._ `-._`-.__.-‘_.-‘ _.-‘ `-._ `-.__.-‘ _.-‘ `-._ _.-‘ `-.__.-‘ 26263:M 08 Oct 15:10:24.506 # Server started, Redis version 3.0.2 26263:M 08 Oct 15:10:24.508 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add ‘vm.overcommit_memory = 1‘ to /etc/sysctl.conf and then reboot or run the command ‘sysctl vm.overcommit_memory=1‘ for this to take effect. 26263:M 08 Oct 15:10:24.509 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command ‘echo never > /sys/kernel/mm/transparent_hugepage/enabled‘ as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled. 26263:M 08 Oct 15:10:24.509 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 26263:M 08 Oct 15:10:24.510 * The server is now ready to accept connections on port 6379
2)脚本启动(生产环境)
生产环境推荐使用初始化脚步进行启动,主要步骤如下:
[root@darren utils]# cp redis_init_script /etc/init.d/redis_6379
1 #!/bin/sh 2 # 3 # Simple Redis init.d script conceived to work on Linux systems 4 # as it does use of the /proc filesystem. 5 6 REDISPORT=6379 #端口号 7 EXEC=/usr/local/bin/redis-server #redis-server可执行文件所在目录 8 CLIEXEC=/usr/local/bin/redis-cli #redis-cli可执行文件所在目录 9 10 PIDFILE=/var/run/redis_${REDISPORT}.pid 11 CONF="/etc/redis/${REDISPORT}.conf" 12 13 case "$1" in 14 start) 15 if [ -f $PIDFILE ] 16 then 17 echo "$PIDFILE exists, process is already running or crashed" 18 else 19 echo "Starting Redis server..." 20 $EXEC $CONF 21 fi 22 ;; 23 stop) 24 if [ ! -f $PIDFILE ] 25 then 26 echo "$PIDFILE does not exist, process is not running" 27 else 28 PID=$(cat $PIDFILE) 29 echo "Stopping ..." 30 $CLIEXEC -p $REDISPORT shutdown 31 while [ -x /proc/${PID} ] 32 do 33 echo "Waiting for Redis to shutdown ..." 34 sleep 1 35 done 36 echo "Redis stopped" 37 fi 38 ;; 39 *) 40 echo "Please use start or stop as first argument" 41 ;; 42 esac
[root@darren utils]# cd /etc [root@darren etc]# mkdir redis [root@darren etc]# mkdir /var/redis/6379 -p
[root@darren redis-3.0.2]# cp redis.conf /etc/redis/6379.conf [root@darren redis-3.0.2]# vim /etc/redis/6379.conf
37 daemonize yes
39 # When running daemonized, Redis writes a pid file in /var/run/redis.pid by
40 # default. You can specify a custom pid file location here.
41 pidfile /var/run/redis_6379.pid
43 # Accept connections on the specified port, default is 6379.
44 # If port 0 is specified Redis will not listen on a TCP socket.
45 port 6379
187 dir /var/redis/6379 #持久化文件目录
具体配置参数说明如下:
daemonize: 是否以后台daemon方式运行 pidfile: pid文件位置 port: 监听的端口号 timeout: 请求超时时间 loglevel: log信息级别 logfile: log文件位置 databases: 开启数据库的数量 save * *: 保存快照的频率,第一个*表示多长时间,第三个*表示执行多少次写操作。在一定时间内执行一定数量的写操作时,自动保存快照。可设置多个条件。 rdbcompression: 是否使用压缩 dbfilename: 数据快照文件名(只是文件名,不包括目录) dir: 数据快照的保存目录(这个是目录) appendonly: 是否开启appendonlylog,开启的话每次写操作会记一条log,这会提高数据抗风险能力,但影响效率。 appendfsync: appendonlylog如何同步到磁盘(三个选项,分别是每次写都强制调用fsync、每秒启用一次fsync、不调用fsync等待系统自己同步)
[root@darren redis]# /etc/init.d/redis_6379 start Starting Redis server... [root@darren redis]# ps axu|grep redis root 26400 0.2 0.3 137440 7432 ? Ssl 15:49 0:00 /home/software/redis-3.0.2/src/redis-server *:6379 root 26404 0.0 0.0 103256 844 pts/3 S+ 15:49 0:00 grep redis [root@darren redis]#
这样redis就启动了。
3)停止redis
[root@darren redis]# /home/software/redis-3.0.2/src/redis-cli shutdown #或者kill [root@darren redis]# pkill -9 redis
3、redis命令行客户端
进入命令行:
[root@darren redis]# redis-cli 127.0.0.1:6379> PING PONG 127.0.0.1:6379>
原文:http://www.cnblogs.com/mysql-dba/p/4990023.html