Redis是一个开源、支持网络、基于内存、键值对存储数据库。与其他非关系型数据库主要不同在于,Redis中值的类型不仅限于字符串(Strings),还支持如下抽象数据类型:(列表)Lists、(集合)Sets、(有序集合)Sorted sets 、(哈希)Hashes。Redis 通过 RDB、AOF 两种方式来实现数据的持久化。
安装Redis
Redis 源码包保存路径 /usr/local/src
Redis 软件安装路径 /usr/local/redis
编译 Redis 前先安装 tcl,若不安装,在 make test 阶段会提示错误。
[root@z-dig ~]# yum -q -y install tcl
[root@z-dig ~]# cd /usr/local/src/ [root@z-dig src]# wget http://download.redis.io/releases/redis-3.0.5.tar.gz
[root@z-dig src]# tar xf redis-3.0.5.tar.gz [root@z-dig src]# cd redis-3.0.5 [root@z-dig redis-3.0.5]# make [root@z-dig redis-3.0.5]# make test ... o/ All tests passed without errors! Cleanup: may take some time... OK make[1]: Leaving directory `/usr/local/src/redis-3.0.5/src' [root@z-dig redis-3.0.5]# make PREFIX=/usr/local/redis-3.0.5 install
[root@z-dig redis-3.0.5]# mkdir /usr/local/redis-3.0.5/conf -p [root@z-dig redis-3.0.5]# ln -s /usr/local/redis-3.0.5/ /usr/local/redis
[root@z-dig redis-3.0.5]# cp redis.conf /usr/local/redis/conf/
[root@z-dig redis-3.0.5]# cd /usr/local/redis [root@z-dig redis]# pwd /usr/local/redis [root@z-dig redis]# tree . . ├── bin │ ├── redis-benchmark │ ├── redis-check-aof │ ├── redis-check-dump │ ├── redis-cli │ └── redis-server └── conf └── redis.conf 2 directories, 6 files [root@z-dig redis]#
到此 Redis 已安装在 /usr/local/redis
修改配置文件
由于以下配置在某些配置项后添加了注释,所以不能直接使用,使用时需要把后面的注释删除。
[root@z-dig redis]# grep -Ev '#|^$' conf/redis.conf daemonize yes #run as a daemon pidfile /var/run/redis.pid # pid file port 6379 # server port tcp-backlog 511 # < /proc/sys/net/core/somaxconn bind 127.0.0.1 # bind network interface timeout 0 # Close the connection after a client is idle for N seconds tcp-keepalive 0 loglevel warning # verbosity level logfile "/var/log/redis.log" databases 16 # number of databases begain with 0 save 900 1 # after 900 sec (15 min) if at least 1 key changed save 300 10 # after 300 sec (5 min) if at least 10 keys changed save 60 10000 # after 60 sec if at least 10000 keys changed stop-writes-on-bgsave-error yes rdbcompression yes # compress dump.rdb rdbchecksum yes # check dump.rdb dbfilename dump.rdb # dump file name dir ./ # dump file path slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 requirepass www.z-dig.com # password for redis when client connect to this server appendonly no # use AOF or RDB appendfilename "appendonly.aof" # AOF file name appendfsync everysec # save frequency no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" 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 [root@z-dig redis]#
调整内核参数
[root@z-dig redis]# echo 512 > /proc/sys/net/core/somaxconn
启动 redis
[root@z-dig redis]# /usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf [root@z-dig redis]# lsof -i:6379 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME redis-ser 20504 root 4u IPv4 139391 0t0 TCP localhost:6379 (LISTEN) [root@z-dig redis]#
测试
[root@z-dig redis]# ln -s /usr/local/redis/bin/redis-cli /usr/local/sbin/ [root@z-dig redis]# redis-cli -h 127.0.0.1 -p 6379 -a www.z-dig.com 127.0.0.1:6379> set name mr.zhou OK 127.0.0.1:6379> get name "mr.zhou" 127.0.0.1:6379> 127.0.0.1:6379> help shutdown SHUTDOWN [NOSAVE] [SAVE] summary: Synchronously save the dataset to disk and then shut down the server since: 1.0.0 group: server 127.0.0.1:6379> 127.0.0.1:6379> shutdown save not connected> not connected> get name Could not connect to Redis at 127.0.0.1:6379: Connection refused not connected> not connected> quit [root@z-dig redis]# lsof -i:6379 [root@z-dig redis]# [root@z-dig redis]# /usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf [root@z-dig redis]# lsof -i:6379 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME redis-ser 20675 root 4u IPv4 143036 0t0 TCP localhost:6379 (LISTEN) [root@z-dig redis]# redis-cli -h 127.0.0.1 -p 6379 -a www.z-dig.com get name "mr.zhou" [root@z-dig redis]#
基础测试完成。根据实际环境进行配置调优。
多实例主从配置
[root@z-dig redis]# redis-cli -h 127.0.0.1 -p 6379 -a www.z-dig.com shutdown save [root@z-dig redis]# cp conf/redis.conf conf/redis-1.conf [root@z-dig redis]# grep -Ev '#|^$' conf/redis-1.conf daemonize yes pidfile /var/run/redis-1.pid port 16379 tcp-backlog 511 bind 127.0.0.1 timeout 0 tcp-keepalive 0 loglevel warning logfile "/var/log/redis-1.log" databases 16 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump-1.rdb dir ./ slaveof 127.0.0.1 6379 masterauth www.z-dig.com slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 requirepass www.z-dig.com appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" 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 [root@z-dig redis]#
启动两个实例
[root@z-dig redis]# /usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf [root@z-dig redis]# /usr/local/redis/bin/redis-server /usr/local/redis/conf/redis-1.conf [root@z-dig redis]# lsof -i:6379 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME redis-ser 20930 root 4u IPv4 145992 0t0 TCP localhost:6379 (LISTEN) redis-ser 20930 root 5u IPv4 146031 0t0 TCP localhost:6379->localhost:39445 (ESTABLISHED) redis-ser 20938 root 5u IPv4 146029 0t0 TCP localhost:39445->localhost:6379 (ESTABLISHED) [root@z-dig redis]# lsof -i:16379 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME redis-ser 20938 root 4u IPv4 146024 0t0 TCP localhost:16379 (LISTEN) [root@z-dig redis]#
[root@z-dig redis]# redis-cli -h 127.0.0.1 -p 6379 -a www.z-dig.com get name "mr.zhou" [root@z-dig redis]# redis-cli -h 127.0.0.1 -p 16379 -a www.z-dig.com get name "mr.zhou" [root@z-dig redis]#
测试主从
[root@z-dig redis]# redis-cli -h 127.0.0.1 -p 6379 -a www.z-dig.com info replication # Replication role:master connected_slaves:1 slave0:ip=127.0.0.1,port=16379,state=online,offset=337,lag=1 master_repl_offset:337 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:336 [root@z-dig redis]# [root@z-dig redis]# redis-cli -h 127.0.0.1 -p 16379 -a www.z-dig.com info replication # Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:9 master_sync_in_progress:0 slave_repl_offset:351 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 [root@z-dig redis]#
[root@z-dig redis]# redis-cli -h 127.0.0.1 -p 16379 -a www.z-dig.com get age (nil) [root@z-dig redis]# [root@z-dig redis]# redis-cli -h 127.0.0.1 -p 16379 -a www.z-dig.com set age 23 (error) READONLY You can't write against a read only slave. [root@z-dig redis]# [root@z-dig redis]# redis-cli -h 127.0.0.1 -p 6379 -a www.z-dig.com get age (nil) [root@z-dig redis]# [root@z-dig redis]# redis-cli -h 127.0.0.1 -p 6379 -a www.z-dig.com set age 23 OK [root@z-dig redis]# [root@z-dig redis]# redis-cli -h 127.0.0.1 -p 16379 -a www.z-dig.com get age "23" [root@z-dig redis]#
多实例主从已配置完成,并测试成功。配置过程中需要注意各实例的端口、PID文件、dump 文件不能相同。若配置了密码访问,则应注意从实例中进行相应的密码配置。从库是否只读等。
配置文件中还有很多高级配置项,可以进行调优配置,并组成Redis集群。RDB、AOF可根据实际的情况进行配置。后期会进行介绍。