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