redis安裝
//安裝依賴包
[root@localhost ~]# yum -y install gcc gcc-c++
//升級gcc版本
[root@localhost ~]# yum -y install centos-release-scl && yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils && scl enable devtoolset-9 bash
//安裝redis
[root@localhost redis-6.0.6]# make
//把這個移到/usr/bin下就可以直接使用
[root@localhost src]# cp redis-server redis-cli /usr/bin/
[root@localhost src]# redis-server
24093:C 13 Aug 2020 05:00:31.734 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
24093:C 13 Aug 2020 05:00:31.734 # Redis version=6.0.6, bits=64, commit=00000000, modified=0, pid=24093, just started
24093:C 13 Aug 2020 05:00:31.734 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
24093:M 13 Aug 2020 05:00:31.735 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.0.6 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 24093
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
24093:M 13 Aug 2020 05:00:31.735 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
24093:M 13 Aug 2020 05:00:31.735 # Server initialized
24093:M 13 Aug 2020 05:00:31.735 # 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.
24093:M 13 Aug 2020 05:00:31.735 # 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.
24093:M 13 Aug 2020 05:00:31.735 * Ready to accept connections
//因爲這個是服務在前端,使用開啓是這樣的,也可以放在後臺啓動
[root@localhost src]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:6379 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::6379 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
[root@localhost ~]# redis-cli
127.0.0.1:6379> set a 10
OK
127.0.0.1:6379> quit
[root@localhost ~]# redis-cli get a
"10"
密碼設定
//複製組態檔到/etc/
[root@localhost redis-6.0.6]# cp redis.conf /etc/
//編輯組態檔
[root@localhost redis-6.0.6]# vim /etc/redis.conf
786 requirepass 123 //在786行,密碼可以自行輸入
//放在後台執行並指定組態檔
[root@localhost ~]# nohup redis-server /etc/redis.conf &
[1] 93816
[root@localhost ~]# nohup: 忽略輸入並把輸出追加到"nohup.out"
[root@localhost ~]# redis-cli
127.0.0.1:6379> set f 60
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123
OK
127.0.0.1:6379[1]> config set requirepass 123456 //設定密碼爲123456
OK
127.0.0.1:6379[1]> config get requirepass //檢視密碼
1) "requirepass"
2) "123456"
127.0.0.1:6379[1]> quit
[root@localhost ~]# redis-cli
127.0.0.1:6379> auth 123
(error) WRONGPASS invalid username-password pair
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379>
redis基礎命令
命令 |
作用 |
keys * |
檢視當前數據庫中所有key |
info |
檢視當前redis狀態 |
monitor |
監控redis正在執行的命令 |
select n |
切換到 n 號數據庫 (編號 0 -15) |
del |
刪除key |
flushdb |
清空當前數據庫 |
flushall |
清空所有庫中的數據 |
get |
取出 |
set |
設定 |
incr |
自增1 |
incrby |
自增自定義 |
decr |
自減1 |
decrby |
自減自定義 |
[root@localhost ~]# redis-cli
127.0.0.1:6379> auth 123
OK
127.0.0.1:6379> set a 10 //設定
OK
127.0.0.1:6379> set b 20
OK
127.0.0.1:6379> keys * //檢視當前數據庫中所有key
1) "b"
2) "a"
127.0.0.1:6379> del a //刪除
(integer) 1
127.0.0.1:6379> keys *
1) "b"
127.0.0.1:6379> incr a //自增1
(integer) 1
127.0.0.1:6379> keys *
1) "a"
2) "b"
127.0.0.1:6379> get a
"1"
127.0.0.1:6379> incr b
(integer) 21
127.0.0.1:6379> get b
"21"
127.0.0.1:6379> incrby a 5 //自定義自增
(integer) 6
127.0.0.1:6379> decr b // 自減1
(integer) 20
127.0.0.1:6379> decrby b 5 //decrby 自減自定義
(integer) 15
127.0.0.1:6379> get b
"15"
127.0.0.1:6379> select 5 //切換到5號數據庫
OK
127.0.0.1:6379[5]> select 1
OK
127.0.0.1:6379[1]> set a 2
OK
127.0.0.1:6379[1]> set b 3
OK
127.0.0.1:6379[1]> flushdb //清空當前庫所有數據
OK
127.0.0.1:6379[1]> keys *
(empty array)
127.0.0.1:6379[1]> set b 3
OK
127.0.0.1:6379[1]> select 2
OK
127.0.0.1:6379[2]> set a 2
OK
127.0.0.1:6379[2]> keys *
1) "a"
127.0.0.1:6379[2]> flushall //清空所有庫數據
OK
127.0.0.1:6379[2]> keys *
(empty array)
127.0.0.1:6379[2]> select 1
OK
127.0.0.1:6379[1]> keys *
(empty array)
127.0.0.1:6379[1]> quit //退出