Redis在百度百科里的解释:Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API,包括C#、Java、PHP等等,甚至连Javascript都做了很好的封装。
可见Redis的数据是持久化的,可以分担一个项目中的部分业务,Redis的数据是存储在服务器内存当中的,这样可以极大的加快访问速度,因为内存的读取速度远远超过磁盘和数据库,这在很大程序的上解决了大并发的困惑;同时Redis和Memcached不同,会按照设定的规律定期将数据同步到磁盘数据库,这样在Redis服务器宕机时也能保证数据的完整性。
大家都知道Redis在全球最大的用户是新浪微博,新浪有最大的Redis集群,新浪最开始使用Memcached与Mysql配合,后来新浪又增加了Redis,而不是弃用Memcached,新浪加入Redis集群的初衷:
1、Memcached存在命中率问题,这样大量请求会发送到Mysql,对于新浪微博这么大的并发无疑是一个灾难,Redis不存在命中率问题
2、Redis相对于Memcached支持更多的数据类型,如string(字符串)、list(链表)、set(集合)、zset(有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的
3、Redis主从集群配置技术容易实现,维护成本低,项目复杂度低
4、Redis的持久化存储从某种程度上解决了Cache宕机带来的雪崩现象
当然,新浪并没有弃用Memcached+Mysql,用户的全量信息是在此结构中的,使用Redis带来的问题是,数据是持久化的,这是相当占用内存的,成本不可小觑。
Redis(2.8.19目前最新稳定版本)的安装:
1.下载:
http://pan.baidu.com/s/1jGvJxBo
2.安装
解压下载文件:
[root@jhq0229 src]# tar xzvf redis-stable.tar.gz
编译安装:
[root@jhq0229 src]# cd redis-stable
[root@jhq0229 redis-stable]# make
[root@jhq0229 redis-stable]# make PREFIX=/usr/local install
配置:
[root@jhq0229 redis-stable]# mkdir /etc/redis
[root@jhq0229 redis-stable]# cp redis.conf /etc/redis/
创建Redis数据及日志存放目录:
[root@jhq0229 redis-stable]# mkdir /data/redis
Redis配置文件详解:
daemonize:如果需要在后台运行,把该项心为yes
pidfile:配置多个pid的地址,默认在/var/run/redis.pid
bind:绑定ip,设置后只接受来自该ip的请求
port:监听端口,默认为6379
timeout:设置客户端连接时的超时时间,单位为秒
loglevel:分为4级,debug、verbose、notice、warning
logfile:配置log文件地址
databases:设置数据库的个数,默认使用的数据库为0
save:设置redis进行数据库镜像的频率
rdbcompression:在进行镜像备份时,是否进行压缩
Dbfilename:镜像备份文件的文件名
Dir:数据库镜像备份的文件放置路径
Slaveof:设置数据库为其他数据库的从数据库
Masterauth:主数据库连接需要的密码验证
Requirepass:设置登录登录时需要使用的密码
Maxclients:限制同时连接的客户数量
Maxmemory:设置redis能够使用的最大内存
Appendonly:开启append only模式
Appendfsync:设置对appendonly.aof文件同步的频率
vm-enabled:是否开启虚拟内存支持
vm-swap-file:设置虚拟内存的交换文件路径
vm-max-memory:设置redis使用的最大物理内存大小
vm-page-size:设置虚拟内存的页大小
vm-pages:设置交换文件的总的page数量
vm-max-threads:设置VMIO同时使用的线程数量
Glueoutputbuf:把小的输出缓存存放在一起
hash-max-zipmap-entries:设置hash的临界值
Activerehashing:重新hash
配置自己的Redis集群(若不配置集群可以只配置Master):
[root@jhq0229 redis-stable]# vim /etc/redis/redis.conf
我的Redis主从配置:
主:192.168.1.18 认证密码:01130229
从:192.168.1.16
主Redis(Master)配置:
# Redis configuration file example
# Note on units: when memory size is needed, it is possible to specify
# it in the usual form of 1k 5GB 4M and so forth:
#
# 1k => 1000 bytes
# 1kb => 1024 bytes
# 1m => 1000000 bytes
# 1mb => 1024*1024 bytes
# 1g => 1000000000 bytes
# 1gb => 1024*1024*1024 bytes
#
# units are case insensitive so 1GB 1Gb 1gB are all the same.
################################## INCLUDES ###################################
# Include one or more other config files here. This is useful if you
# have a standard template that goes to all Redis servers but also need
# to customize a few per-server settings. Include files can include
# other files, so use this wisely.
#
# Notice option "include" won't be rewritten by command "CONFIG REWRITE"
# from admin or Redis Sentinel. Since Redis always uses the last processed
# line as value of a configuration directive, you'd better put includes
# at the beginning of this file to avoid overwriting config change at runtime.
#
# If instead you are interested in using includes to override configuration
# options, it is better to use include as the last line.
#
# include /path/to/local.conf
# include /path/to/other.conf
################################ GENERAL #####################################
# 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
# When running daemonized, Redis writes a pid file in /var/run/redis.pid by
# default. You can specify a custom pid file location here.
#以后台进行运行,需指定pid
pidfile /var/run/redis.pid
# Accept connections on the specified port, default is 6379.
# If port 0 is specified Redis will not listen on a TCP socket.
port 6379
# TCP listen() backlog.
#
# 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
# By default Redis listens for connections from all the network interfaces
# available on the server. It is possible to listen to just one or multiple
# interfaces using the "bind" configuration directive, followed by one or
# more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
bind 192.168.1.18
# Close the connection after a client is idle for N seconds (0 to disable)
#客户端闲置N秒后关闭连接
timeout 30
tcp-keepalive 0
# Specify the server verbosity level.
# This can be one of:
# debug (a lot of information, useful for development/testing)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably)
# warning (only very important / critical messages are logged)
#日志级别
loglevel notice
#记录日志的文件名称
logfile "redlog"
# To enable logging to the system logger, just set 'syslog-enabled' to yes,
# and optionally update the other syslog parameters to suit your needs.
# syslog-enabled no
# Specify the syslog identity.
# syslog-ident redis
# Specify the syslog facility. Must be USER or between LOCAL0-LOCAL7.
# syslog-facility local0
# Set the number of databases. The default database is DB 0, you can select
# a different one on a per-connection basis using SELECT <dbid> where
# dbid is a number between 0 and 'databases'-1
#可用数据库数量
databases 16
################################ SNAPSHOTTING ################################
#
# Save the DB on disk:
#
# save <seconds> <changes>
#
# Will save the DB if both the given number of seconds and the given
# number of write operations against the DB occurred.
#
# In the example below the behaviour will be to save:
# after 900 sec (15 min) if at least 1 key changed
# after 300 sec (5 min) if at least 10 keys changed
# after 60 sec if at least 10000 keys changed
#
# Note: you can disable saving completely by commenting out all "save" lines.
#
# It is also possible to remove all the previously configured save
# points by adding a save directive with a single empty string argument
# like in the following example:
#
# save ""
#当有一条数据更新,900秒后同步数据到磁盘数据库
save 900 1
#当有10条数据更新,300秒后同步数据到磁盘数据库
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
# Compress string objects using LZF when dump .rdb databases?
# For default that's set to 'yes' as it's almost always a win.
# If you want to save some CPU in the saving child set it to 'no' but
# the dataset will likely be bigger if you have compressible values or keys.
#当dump .rdb的时候是否压缩数据对象,默认值为yes
rdbcompression yes
rdbchecksum yes
# The filename where to dump the DB
# 磁盘数据库文件名称
dbfilename myredis.rdb
#本地数据库存放路径
dir /data/redis/
################################# REPLICATION #################################
#Redis主从复制配置
#若当前服务为slave,在此处设置主服务的ip及端口
# slaveof <masterip> <masterport>
#若当前服务为slave,设置主服务的认证密码
# masterauth <master-password>
slave-serve-stale-data yes
#若当前为slave服务,设置slave是否为只读服务
#slave-read-only yes
# Replication SYNC strategy: disk or socket.
#
# -------------------------------------------------------
# WARNING: DISKLESS REPLICATION IS EXPERIMENTAL CURRENTLY
# -------------------------------------------------------
#
# New slaves and reconnecting slaves that are not able to continue the replication
# process just receiving differences, need to do what is called a "full
# synchronization". An RDB file is transmitted from the master to the slaves.
# The transmission can happen in two different ways:
#
# 1) Disk-backed: The Redis master creates a new process that writes the RDB
# file on disk. Later the file is transferred by the parent
# process to the slaves incrementally.
# 2) Diskless: The Redis master creates a new process that directly writes the
# RDB file to slave sockets, without touching the disk at all.
#
# With disk-backed replication, while the RDB file is generated, more slaves
# can be queued and served with the RDB file as soon as the current child producing
# the RDB file finishes its work. With diskless replication instead once
# the transfer starts, new slaves arriving will be queued and a new transfer
# will start when the current one terminates.
#
# When diskless replication is used, the master waits a configurable amount of
# time (in seconds) before starting the transfer in the hope that multiple slaves
# will arrive and the transfer can be parallelized.
#
# With slow disks and fast (large bandwidth) networks, diskless replication
# works better.
repl-diskless-sync no
repl-diskless-sync-delay 5
# Slaves send PINGs to server in a predefined interval. It's possible to change
# this interval with the repl_ping_slave_period option. The default value is 10
# seconds.
#
# repl-ping-slave-period 10
# repl-timeout 60
repl-disable-tcp-nodelay no
slave-priority 100
################################## SECURITY ###################################
# Require clients to issue AUTH <PASSWORD> before processing any other
# commands. This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#认证密码
requirepass 01130229
################################### LIMITS ####################################
# Set the max number of connected clients at the same time. By default
# this limit is set to 10000 clients, however if the Redis server is not
# able to configure the process file limit to allow for the specified limit
# the max number of allowed clients is set to the current file limit
# minus 32 (as Redis reserves a few file descriptors for internal uses).
#
# Once the limit is reached Redis will close all the new connections sending
# an error 'max number of clients reached'.
# Don't use more memory than the specified amount of bytes.
# When the memory limit is reached Redis will try to remove keys
# according to the eviction policy selected (see maxmemory-policy).
#
# If Redis can't remove keys according to the policy, or if the policy is
# set to 'noeviction', Redis will start to reply with errors to commands
# that would use more memory, like SET, LPUSH, and so on, and will continue
# to reply to read-only commands like GET.
#
# This option is usually useful when using Redis as an LRU cache, or to set
# a hard memory limit for an instance (using the 'noeviction' policy).
#
# WARNING: If you have slaves attached to an instance with maxmemory on,
# the size of the output buffers needed to feed the slaves are subtracted
# from the used memory count, so that network problems / resyncs will
# not trigger a loop where keys are evicted, and in turn the output
# buffer of slaves is full with DELs of keys evicted triggering the deletion
# of more keys, and so forth until the database is completely emptied.
#
# In short... if you have slaves attached it is suggested that you set a lower
# limit for maxmemory so that there is some free RAM on the system for slave
# output buffers (but this is not needed if the policy is 'noeviction').
# maxmemory <bytes>
# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
# is reached. You can select among five behaviors:
#
# volatile-lru -> remove the key with an expire set using an LRU algorithm
# allkeys-lru -> remove any key according to the LRU algorithm
# volatile-random -> remove a random key with an expire set
# allkeys-random -> remove a random key, any key
# volatile-ttl -> remove the key with the nearest expire time (minor TTL)
# noeviction -> don't expire at all, just return an error on write operations
#
# Note: with any of the above policies, Redis will return an error on write
# operations, when there are no suitable keys for eviction.
#
# At the date of writing these commands are: set setnx setex append
# incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd
# sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby
# zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby
# getset mset msetnx exec sort
#
# The default is:
#
# maxmemory-policy volatile-lru
# LRU and minimal TTL algorithms are not precise algorithms but approximated
# algorithms (in order to save memory), so you can select as well the sample
#
# maxmemory-samples 3
############################## APPEND ONLY MODE ###############################
# By default Redis asynchronously dumps the dataset on disk. This mode is
# good enough in many applications, but an issue with the Redis process or
# a power outage may result into a few minutes of writes lost (depending on
# the configured save points).
#
appendonly no
# The name of the append only file (default: "appendonly.aof")#更新日志文件名称
#
#
# If unsure, use "everysec".
# appendfsync always
appendfsync everysec
# appendfsync no
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
################################ LUA SCRIPTING ###############################
# Max execution time of a Lua script in milliseconds.
#
# If the maximum execution time is reached Redis will log that a script is
# still in execution after the maximum allowed time and will start to
# reply to queries with an error.
#
lua-time-limit 5000
################################## SLOW LOG ###################################
# The Redis Slow Log is a system to log queries that exceeded a specified
# execution time. The execution time does not include the I/O operations
# like talking with the client, sending the reply and so forth,
# but just the time needed to actually execute the command (this is the only
# stage of command execution where the thread is blocked and can not serve
# other requests in the meantime).
slowlog-log-slower-than 10000
slowlog-max-len 128
################################ LATENCY MONITOR ##############################
# The Redis latency monitoring subsystem samples different operations
# at runtime in order to collect data related to possible sources of
# latency of a Redis instance.
#
latency-monitor-threshold 0
############################# Event notification ##############################
notify-keyspace-events ""
############################### ADVANCED CONFIG ###############################
# Hashes are encoded using a memory efficient data structure when they have a
# small number of entries, and the biggest entry does not exceed a given
# threshold. These thresholds can be configured using the following directives.
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
从Redis(Slave)配置,配置与Master类似,不一致内容如下:
#绑定IP地址
bind 192.168.1.16
#若当前服务为slave,在此处设置主服务的ip及端口
slaveof 192.168.1.18 6379
#若当前服务为slave,设置主服务的认证密码
masterauth 01130229
配置Redis开机启动:
[root@jhq0229 redis-stable]# vim /etc/init.d/redis
内容如下:
#!/bin/bash
#
# redis - this script starts and stops the redis-server daemon
#
# chkconfig: - 80 12
# description: Redis is a persistent key-value database
# processname: redis-server
# config: /etc/redis/redis.conf
# pidfile: /var/run/redis.pid
source /etc/init.d/functions
BIN="/usr/local/bin"
CONFIG="/etc/redis/redis.conf"
PIDFILE="/var/run/redis.pid"
### Read configuration
[ -r "$SYSCONFIG" ] && source "$SYSCONFIG"
RETVAL=0
prog="redis-server"
desc="Redis Server"
start() {
if [ -e $PIDFILE ];then
echo "$desc already running...."
exit 1
fi
echo -n $"Starting $desc: "
daemon $BIN/$prog $CONFIG
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
return $RETVAL
}
stop() {
echo -n $"Stop $desc: "
killproc $prog
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog $PIDFILE
return $RETVAL
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
condrestart)
[ -e /var/lock/subsys/$prog ] && restart
RETVAL=$?
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|status}"
RETVAL=1
esac
exit $RETVAL
[root@jhq0229 redis-stable]# chmod +x /etc/init.d/redis
[root@jhq0229 redis-stable]# chkconfig redis on
补充配置,配置停止服务前同步数据到磁盘:
[root@jhq0229 redis-stable]# vim /etc/sysctl.conf
进行如下修改:
vm.overcommit_memory=1
应用修改:
sysctl -p
启动Redis:
[root@jhq0229 redis-stable]# service redis start
主从Redis的安装和配置只是配置文件不同,其他的均一样,一般主从配好就可以测试主从操作了,主server上set,从server马上就可以get到了。
最后,奉上公司同事辛勤总结的Redis学习资料。希望可以帮到你。
下载地址: http://pan.baidu.com/s/1qWLV87a
PPT内容版权归公司所有。