17.Redis客户端使用

 

Redis客户端使用

1. 工具安装

(1) 安装redis-desktop-manager

(2) 安装redis plus

2. 连接redis

2.1 配置redis.conf文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 设置为0.0.0.0,允许所有地址访问
bind 0.0.0.0

port 6379

daemonize yes

pidfile /var/run/redis_6379.pid

logfile "/usr/local/redis-6.0.9/my_redis_tools/redis-6379.log"

dbfilename dump-6379.rdb

dir /usr/local/redis-6.0.9/my_redis_tools/

# 设置连接密码
requirepass foobared

2.2 关闭防火墙:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Centos6
查看防火墙状态:
[root@centos6 ~]# service iptables status
iptables:未运行防火墙。
开启防火墙:
[root@centos6 ~]# service iptables start
关闭防火墙:
[root@centos6 ~]# service iptables stop

centos7
查看防火墙状态:
firewall-cmd --state
关闭防火墙
systemctl stop firewalld.service
开启防火墙
systemctl start firewalld.service
禁用防火墙
systemctl disable firewalld.service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
[root@localhost my_redis_tools]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2020-12-10 22:00:12 CST; 1h 18min ago
Docs: man:firewalld(1)
Main PID: 773 (firewalld)
CGroup: /system.slice/firewalld.service
└─773 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid

Dec 10 22:00:23 localhost.localdomain firewalld[773]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -t nat -D PREROU...in?).
Dec 10 22:00:23 localhost.localdomain firewalld[773]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -t nat -D OUTPUT...in?).
Dec 10 22:00:23 localhost.localdomain firewalld[773]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -t nat -F DOCKER...name.
Dec 10 22:00:23 localhost.localdomain firewalld[773]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -t nat -X DOCKER...name.
Dec 10 22:00:23 localhost.localdomain firewalld[773]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -t filter -F DOC...name.
Dec 10 22:00:23 localhost.localdomain firewalld[773]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -t filter -X DOC...name.
Dec 10 22:00:23 localhost.localdomain firewalld[773]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -t filter -F DOC...name.
Dec 10 22:00:23 localhost.localdomain firewalld[773]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -t filter -X DOC...name.
Dec 10 22:00:23 localhost.localdomain firewalld[773]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -D FORWARD -i do...in?).
Dec 10 22:00:23 localhost.localdomain firewalld[773]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -D FORWARD -i do...in?).
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost my_redis_tools]#
[root@localhost my_redis_tools]#
[root@localhost my_redis_tools]# systemctl stop firewalld
[root@localhost my_redis_tools]#
[root@localhost my_redis_tools]#
[root@localhost my_redis_tools]#
[root@localhost my_redis_tools]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Thu 2020-12-10 23:19:19 CST; 3s ago
Docs: man:firewalld(1)
Process: 773 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
Main PID: 773 (code=exited, status=0/SUCCESS)

Dec 10 22:00:23 localhost.localdomain firewalld[773]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -t nat -F DOCKER...name.
Dec 10 22:00:23 localhost.localdomain firewalld[773]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -t nat -X DOCKER...name.
Dec 10 22:00:23 localhost.localdomain firewalld[773]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -t filter -F DOC...name.
Dec 10 22:00:23 localhost.localdomain firewalld[773]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -t filter -X DOC...name.
Dec 10 22:00:23 localhost.localdomain firewalld[773]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -t filter -F DOC...name.
Dec 10 22:00:23 localhost.localdomain firewalld[773]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -t filter -X DOC...name.
Dec 10 22:00:23 localhost.localdomain firewalld[773]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -D FORWARD -i do...in?).
Dec 10 22:00:23 localhost.localdomain firewalld[773]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -D FORWARD -i do...in?).
Dec 10 23:19:18 localhost.localdomain systemd[1]: Stopping firewalld - dynamic firewall daemon...
Dec 10 23:19:19 localhost.localdomain systemd[1]: Stopped firewalld - dynamic firewall daemon.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost my_redis_tools]#
1
2
3
4
5
6
[root@localhost my_redis_tools]# firewall-cmd --state
running
[root@localhost my_redis_tools]# systemctl stop firewalld.service
[root@localhost my_redis_tools]# firewall-cmd --state
not running
[root@localhost my_redis_tools]#

2.3 启动redis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost my_redis_tools]# /usr/local/redis-6.0.9/bin/redis-server redis.conf 
[root@localhost my_redis_tools]#
[root@localhost my_redis_tools]# ps -ef | grep redis
root 65611 1 0 23:17 ? 00:00:00 /usr/local/redis-6.0.9/bin/redis-server 0.0.0.0:6379
root 65617 1749 0 23:17 pts/0 00:00:00 grep --color=auto redis
[root@localhost my_redis_tools]#
[root@localhost my_redis_tools]# /usr/local/redis-6.0.9/bin/redis-cli
127.0.0.1:6379>
127.0.0.1:6379> ping
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth foobared
OK
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>

2.4 工具连接

(1) redis-desktop-manager

(2) redis plus

2.5 工具使用

(1) redis-desktop-manager

key值使用:分隔,例如student:name,student:age,可以实现文件夹层级

(2) redis plus

没有实现key值文件夹层级

2.6 连接集群

2.6.1 新建文件夹

1
2
3
4
[root@localhost redis-6.0.9]# mkdir redis_cluster
[root@localhost redis-6.0.9]# cd redis_cluster/
[root@localhost redis_cluster]# pwd
/usr/local/redis-6.0.9/redis_cluster

2.6.2 准备服务端程序

复制redis-serverredis-cli文件

1
2
3
4
5
6
7
[root@localhost redis_cluster]# cp /usr/local/redis-6.0.9/bin/redis-server .
[root@localhost redis_cluster]# cp /usr/local/redis-6.0.9/bin/redis-cli .
[root@localhost redis_cluster]# ls -lh
total 14M
-rwxr-xr-x. 1 root root 4.9M Dec 9 21:00 redis-cli
-rwxr-xr-x. 1 root root 8.7M Dec 9 20:59 redis-server
[root@localhost redis_cluster]#

2.6.3 准备6个redis的配置文件

创建文件夹,配置redis.conf文件

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost redis_cluster]# mkdir redis-1
[root@localhost redis_cluster]# mkdir redis-2
[root@localhost redis_cluster]# mkdir redis-3
[root@localhost redis_cluster]# mkdir redis-4
[root@localhost redis_cluster]# mkdir redis-5
[root@localhost redis_cluster]# mkdir redis-6

[root@localhost redis_cluster]# cp /usr/local/src/redis-6.0.9/redis.conf redis-1/redis-7001.conf
[root@localhost redis_cluster]# cp /usr/local/src/redis-6.0.9/redis.conf redis-2/redis-7002.conf
[root@localhost redis_cluster]# cp /usr/local/src/redis-6.0.9/redis.conf redis-3/redis-7003.conf
[root@localhost redis_cluster]# cp /usr/local/src/redis-6.0.9/redis.conf redis-4/redis-7004.conf
[root@localhost redis_cluster]# cp /usr/local/src/redis-6.0.9/redis.conf redis-5/redis-7005.conf
[root@localhost redis_cluster]# cp /usr/local/src/redis-6.0.9/redis.conf redis-6/redis-7006.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

bind 192.168.168.130

port 7001
# port 7002 ...

pidfile /var/run/redis_7001.pid
# pidfile /var/run/redis_7002.pid ...

logfile "/usr/local/redis-6.0.9/redis_cluster/redis-1/redis-7001.log"
# logfile "/usr/local/redis-6.0.9/redis_cluster/redis-2/redis-7002.log" ...

dbfilename dump-7001.rdb
# dbfilename dump-7002.rdb

dir /usr/local/redis-6.0.9/redis_cluster/redis-1/
# dir /usr/local/redis-6.0.9/redis_cluster/redis-2/ ...

daemonize yes

appendonly yes

cluster-enabled yes

cluster-config-file nodes-7001.conf
# cluster-config-file nodes-7002.conf ...

cluster-node-timeout 5000

2.6.4 同时启动所有的redis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@localhost redis_cluster]# ./redis-server redis-1/redis-7001.conf
[root@localhost redis_cluster]# ./redis-server redis-2/redis-7002.conf
[root@localhost redis_cluster]# ./redis-server redis-3/redis-7003.conf
[root@localhost redis_cluster]# ./redis-server redis-4/redis-7004.conf
[root@localhost redis_cluster]# ./redis-server redis-5/redis-7005.conf
[root@localhost redis_cluster]# ./redis-server redis-6/redis-7006.conf
[root@localhost redis_cluster]#
[root@localhost redis_cluster]# ps -ef | grep redis
root 2429 1 0 23:00 ? 00:00:01 ./redis-server 192.168.168.130:7001 [cluster]
root 2435 1 0 23:00 ? 00:00:02 ./redis-server 192.168.168.130:7002 [cluster]
root 2441 1 0 23:00 ? 00:00:02 ./redis-server 192.168.168.130:7003 [cluster]
root 2460 1 0 23:01 ? 00:00:01 ./redis-server 192.168.168.130:7004 [cluster]
root 2466 1 0 23:01 ? 00:00:01 ./redis-server 192.168.168.130:7005 [cluster]
root 2472 1 0 23:01 ? 00:00:02 ./redis-server 192.168.168.130:7006 [cluster]
root 2516 1759 0 23:14 pts/0 00:00:00 grep --color=auto redis
[root@localhost redis_cluster]#

2.6.5 创建集群(分配槽)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
[root@localhost redis_cluster]# ./redis-cli --cluster create 192.168.168.130:7001 192.168.168.130:7002 192.168.168.130:7003 192.168.168.130:7004 192.168.168.130:7005 192.168.168.130:7006 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.168.130:7005 to 192.168.168.130:7001
Adding replica 192.168.168.130:7006 to 192.168.168.130:7002
Adding replica 192.168.168.130:7004 to 192.168.168.130:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 99abc1f93ec1832530f32c2040149f10fd6341b4 192.168.168.130:7001
slots:[0-5460] (5461 slots) master
M: 9ba7a3caa97ae0accfc21d38a88c2f986f46e06a 192.168.168.130:7002
slots:[5461-10922] (5462 slots) master
M: feedb1e4cde43310045d00de50c77c067686e1e3 192.168.168.130:7003
slots:[10923-16383] (5461 slots) master
S: 923c9615d2dd90cdf07f5043acf2892ec8fbd46f 192.168.168.130:7004
replicates feedb1e4cde43310045d00de50c77c067686e1e3
S: 0e2148ede5c79e4c9a275d16aa67860a3eabec59 192.168.168.130:7005
replicates 99abc1f93ec1832530f32c2040149f10fd6341b4
S: 8651ce57669506d66cdb747226dd0d0dbb524d60 192.168.168.130:7006
replicates 9ba7a3caa97ae0accfc21d38a88c2f986f46e06a
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 192.168.168.130:7001)
M: 99abc1f93ec1832530f32c2040149f10fd6341b4 192.168.168.130:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: feedb1e4cde43310045d00de50c77c067686e1e3 192.168.168.130:7003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 8651ce57669506d66cdb747226dd0d0dbb524d60 192.168.168.130:7006
slots: (0 slots) slave
replicates 9ba7a3caa97ae0accfc21d38a88c2f986f46e06a
S: 0e2148ede5c79e4c9a275d16aa67860a3eabec59 192.168.168.130:7005
slots: (0 slots) slave
replicates 99abc1f93ec1832530f32c2040149f10fd6341b4
M: 9ba7a3caa97ae0accfc21d38a88c2f986f46e06a 192.168.168.130:7002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 923c9615d2dd90cdf07f5043acf2892ec8fbd46f 192.168.168.130:7004
slots: (0 slots) slave
replicates feedb1e4cde43310045d00de50c77c067686e1e3
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost redis_cluster]#
1
2
3
4
5
6
7
8
9
10
11
[root@localhost redis_cluster]# ./redis-cli -c -h 192.168.168.130 -p 7001
192.168.168.130:7001> ping
PONG
192.168.168.130:7001> get k1
-> Redirected to slot [12706] located at 192.168.168.130:7003
(nil)
192.168.168.130:7003> set number 100
-> Redirected to slot [7743] located at 192.168.168.130:7002
OK
192.168.168.130:7002> quit
[root@localhost redis_cluster]#

2.6.5 redis plue连接集群

redis-desktop-manager连接集群有bug,因此使用redis plus连接集群

1
2
3
4
5
6
7
[root@localhost redis_cluster]# ./redis-cli -c -h 192.168.168.130 -p 7001
192.168.168.130:7001> get student:name
"Tom"
192.168.168.130:7001> get number
-> Redirected to slot [7743] located at 192.168.168.130:7002
"100"
192.168.168.130:7002>

3. IDEA安装使用redis插件redis simple