官網(wǎng) http://redis.io
中文網(wǎng) http://redis.cn
命令參考 http://redisdoc.cn
Redis(Remote Dictionary Server)是一個(gè)開(kāi)源的key-value內(nèi)存存儲(chǔ)的NoSQL數(shù)據(jù)庫(kù)馅笙,具有非常強(qiáng)悍的讀寫(xiě)性能抒抬,現(xiàn)階段正越來(lái)越多地被用于互聯(lián)網(wǎng)高并發(fā)場(chǎng)景匪燕。
1 安裝
1.1 Linux(CentOS 7.4)下yum方式安裝
yum install redis
1.2 手動(dòng)編譯安裝
下載安裝redis 4.0.9至/usr/local/src目錄
wget -P /usr/local/src http://download.redis.io/releases/redis-4.0.9.tar.gz
解壓至/opt/redis目錄
mkdir /opt/reids && tar xzvf /usr/local/src/redis-4.0.9.tar.gz -C /opt/redis
編譯安裝(需要依賴(lài)gcc)
cd /opt/redis/redis-4.0.9 && make MALLOC=libc
復(fù)制相關(guān)運(yùn)行文件到/usr/local/bin目錄
make install
[可選]安裝后的測(cè)試(需要依賴(lài)tcl 8.5+)
make test
2 啟動(dòng)
2.1 交互模式啟動(dòng)
[root@VM_0_171_centos ~]# redis-server
23173:C 26 Mar 16:13:02.365 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.3 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 23173
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
23173:M 26 Mar 16:13:02.366 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
23173:M 26 Mar 16:13:02.366 # Server started, Redis version 3.2.3
23173:M 26 Mar 16:13:02.366 # 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.
23173:M 26 Mar 16:13:02.366 # 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.
23173:M 26 Mar 16:13:02.366 * DB loaded from disk: 0.000 seconds
23173:M 26 Mar 16:13:02.366 * The server is now ready to accept connections on port 6379
2.2 后臺(tái)啟動(dòng)
step 1:需要修改配置文件/etc/redis.conf
# By default Redis does not run as a daemon. Use 'yes' if you need it.
daemonize yes
step 2:使用配置文件啟動(dòng)
[root@VM_0_171_centos ~]# redis-server /etc/redis.conf
[root@VM_0_171_centos ~]#
3 客戶(hù)端連接
[root@VM_0_171_centos ~]# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>
4 停止服務(wù)
[root@VM_0_171_centos ~]# redis-shutdown
[root@VM_0_171_centos ~]#
5 其它程序
redis通過(guò)yum方式安裝后,所有程序均位于/usr/bin目錄
[root@VM_0_171_centos ~]# ls -l /usr/bin | grep redis
-rwxr-xr-x 1 root root 86832 8月 5 2016 redis-benchmark
-rwxr-xr-x 1 root root 15408 8月 5 2016 redis-check-aof
-rwxr-xr-x 1 root root 975248 8月 5 2016 redis-check-rdb
-rwxr-xr-x 1 root root 173344 8月 5 2016 redis-cli
lrwxrwxrwx 1 root root 12 3月 25 23:41 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 975248 8月 5 2016 redis-server
-rwxr-xr-x 1 root root 918 8月 5 2016 redis-shutdown
[root@VM_0_171_centos ~]#
其中redis-benchmark可以用來(lái)測(cè)試redis的性能昔善,官網(wǎng)給出的信息是redis的讀速度:110000次/s,寫(xiě)速度:81000次/s
[root@VM_0_171_centos ~]# redis-benchmark
====== PING_INLINE ======
100000 requests completed in 0.69 seconds
50 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
145137.88 requests per second
====== PING_BULK ======
100000 requests completed in 0.65 seconds
50 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
153846.16 requests per second
====== SET ======
100000 requests completed in 0.65 seconds
50 parallel clients
3 bytes payload
keep alive: 1
99.97% <= 1 milliseconds
100.00% <= 1 milliseconds
153609.83 requests per second
====== GET ======
100000 requests completed in 0.64 seconds
50 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
156250.00 requests per second
====== INCR ======
100000 requests completed in 0.64 seconds
50 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
156006.25 requests per second
====== LPUSH ======
100000 requests completed in 0.65 seconds
50 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
153846.16 requests per second
====== RPUSH ======
100000 requests completed in 0.64 seconds
50 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
156985.86 requests per second
====== LPOP ======
100000 requests completed in 0.65 seconds
50 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
153374.23 requests per second
====== RPOP ======
100000 requests completed in 0.65 seconds
50 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
153374.23 requests per second
====== SADD ======
100000 requests completed in 0.65 seconds
50 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
152671.77 requests per second
====== SPOP ======
100000 requests completed in 0.65 seconds
50 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
154083.20 requests per second
====== LPUSH (needed to benchmark LRANGE) ======
100000 requests completed in 0.65 seconds
50 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
153846.16 requests per second
====== LRANGE_100 (first 100 elements) ======
100000 requests completed in 0.64 seconds
50 parallel clients
3 bytes payload
keep alive: 1
99.97% <= 1 milliseconds
100.00% <= 1 milliseconds
155279.50 requests per second
====== LRANGE_300 (first 300 elements) ======
100000 requests completed in 0.64 seconds
50 parallel clients
3 bytes payload
keep alive: 1
99.99% <= 1 milliseconds
100.00% <= 1 milliseconds
155763.23 requests per second
====== LRANGE_500 (first 450 elements) ======
100000 requests completed in 0.65 seconds
50 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
154320.98 requests per second
====== LRANGE_600 (first 600 elements) ======
100000 requests completed in 0.65 seconds
50 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
153374.23 requests per second
====== MSET (10 keys) ======
100000 requests completed in 0.63 seconds
50 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
159744.41 requests per second
[root@VM_0_171_centos ~]#
6 配置文件
通過(guò)yum方式安裝后,redis的配置文件的位置為:/etc/redis.conf积瞒,包括前面提到的后臺(tái)啟動(dòng)也是需要通過(guò)修改配置文件實(shí)現(xiàn),配置項(xiàng)比較多登下,需要對(duì)redis有進(jìn)一步了解
6.1 設(shè)置密碼
如果要限制連接redis必須使用密碼茫孔,需要配置requirepass選項(xiàng):requriepass <password>
################################## 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 123456
設(shè)置密碼后需要重啟服務(wù),再連接redis-cli時(shí)會(huì)提示需要驗(yàn)證授權(quán)被芳,通過(guò)auth <password>命令驗(yàn)證
[root@VM_0_171_centos ~]# vim /etc/redis.conf
[root@VM_0_171_centos ~]# redis-server /etc/redis.conf
[root@VM_0_171_centos ~]# redis-cli
127.0.0.1:6379> ping
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>
7 幫助
客戶(hù)端連接redis后缰贝,可以通過(guò)help獲取幫助,幫助信息里面給出了3種不同方式獲取幫助畔濒,具體用法其它章節(jié)會(huì)進(jìn)一步描述
127.0.0.1:6379> help
redis-cli 3.2.3
To get help about Redis commands type:
"help @<group>" to get a list of commands in <group>
"help <command>" for help on <command>
"help <tab>" to get a list of possible help topics
"quit" to exit
To set redis-cli perferences:
":set hints" enable online hints
":set nohints" disable online hints
Set your preferences in ~/.redisclirc
8.常用命令
8.1查找匹配的key(支持通配符剩晴?*)
keys pattern
8.2鍵的總數(shù)
dbsize
8.3檢查鍵是否存在
exists key
8.4刪除鍵
del key
8.5設(shè)置鍵過(guò)期時(shí)間
expire key seconds
8.6查看鍵剩余過(guò)期時(shí)間
# 大于等于0的整數(shù):鍵剩余的過(guò)期時(shí)間
# -1:鍵未設(shè)置過(guò)期時(shí)間
# -2:鍵不存在
ttl key
8.7鍵的數(shù)據(jù)類(lèi)型
type key