Redis服务与连接那些事儿

Posted 积跬Coder

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis服务与连接那些事儿相关的知识,希望对你有一定的参考价值。

启动示例

当我们需要使用Redis的时候需要把redis的服务开启。如下

1# 启动
2redis-server
3# 守护进程方式启动
4redis-server &
5# 使用自定义redis.conf启动
6redis-server path

redis-server如图

这样虽然是启动了,但是这个终端却用不了了,我个人并不是很喜欢。那么有没有可以让他既可以运行,而且保证不会占用我们的终端呢

这里有两种方法

  • 使用redis-server &明显启动示例即可

  • redis-server ----daemonize yes (以守护进程的方式运行redis)

    Redis服务与连接那些事儿

小技巧:

redis-server --配置名 配置的值

例如:redis-server --port 8765 此时你就可以在你的8765端口上运行redis

这样就可以无需修改redis.conf,就可以定制化的运行redis

Redis.conf

既然看过redis的配置文档,不自己亲手试一试怎么能行。话不多说直接开干。以下为给出最基本的的redis.conf,当然如果有需要也按需添加一些。

1mkdir /database/6379
2cat > /database/6379/redis.conf<<EOF
3daemonize yes
4port 6379
5logfile /database/6379/redis.log
6dir /database/6379
7dbfilename dump.rdb
8EOF

守护进程(后台)运行:daemonize yes
配置端口号 port 6379
配置日志 logfile /database/6379/redis.log
持久化文件存储位置 dir /database/6379
RDB持久化数据文件 dbfilename dump.rdb

Redis服务与连接那些事儿

以上就已经完成了redis服务启动的部分,那么我们接下来看看redis的连接部分

连接

1# 本地连接
2redis-cli    # 相当于 redis-cli -h 127.0.0.1 -p 6379
3
4# 远程连接
5redis-cli -h host -p port -a passwd
Redis服务与连接那些事儿

如图:出现此“unicode”编码显示问题,改如何解决?

Redis服务与连接那些事儿

本地连接直接使用redis-cli,直接在本地连接即可。此过不多赘述

Redis服务与连接那些事儿

DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions:
1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent.
2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server.
3) If you started the server manually just for testing, restart it with the '--protected-mode no' option.
4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

只需通过从服务器运行的同一主机连接到Redis,从环回接口发送命令'CONFIG SET protected mode no'来禁用保护模式,但是如果这样做,请确保Redis不能从internet公开访问。使用CONFIG REWRITE将此更改永久化

您可以通过编辑Redis配置文件,将protectedmode选项设置为no,然后重新启动服务器来禁用protectedmode。

如果只是为了测试而手动启动服务器,请使用“-protected mode no”选项重新启动服务器

服务器就可以开始接受来自外部的连接。

那么从以上得知,redis是默认关闭远程连接以及开启保护模式。开启远程连接的方式有以下几种

  1. 在配置文件中关闭保护模式protected mode no,重启redis服务(不推荐)

  2. 关闭redis服务,使用redis-server -protected mode no,启动服务

  3. 配置ip访问或密码(最推荐)

综上,我们来配置一下我们的redis.conf,如下

1daemonize yes
2port 6379
3logfile /database/6379/redis.log
4dir /database/6379
5dbfilename dump.rdb
6requirepass 123321

性能测试

说到性能与测试这两个都是,大家一直关心的问题。那么redis的性能测试该怎么做呢。这里我们了解一下redis-brnchmark

Redis服务与连接那些事儿

具体参数如下

 1Usage: redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests>] [-k <boolean>]
2
3 -h <hostname>      Server hostname (default 127.0.0.1)
4 -p <port>          Server port (default 6379)
5 -s <socket>        Server socket (overrides host and port)
6 -a <password>      Password for Redis Auth
7 --user <username>  Used to send ACL style 'AUTH username pass'. Needs -a.
8 -c <clients>       Number of parallel connections (default 50)
9 -n <requests>      Total number of requests (default 100000)
10 -d <size>          Data size of SET/GET value in bytes (default 3)
11 --dbnum <db>       SELECT the specified db number (default 0)
12 --threads <num>    Enable multi-thread mode.
13 --cluster          Enable cluster mode.
14 --enable-tracking  Send CLIENT TRACKING on before starting benchmark.
15 -k <boolean>       1=keep alive 0=reconnect (default 1)
16 -r <keyspacelen>   Use random keys for SET/GET/INCR, random values for SADD,
17                    random members and scores for ZADD.
18  Using this option the benchmark will expand the string __rand_int__
19  inside an argument with a 12 digits number in the specified range
20  from 0 to keyspacelen-1. The substitution changes every time a command
21  is executed. Default tests use this to hit random keys in the
22  specified range.
23 -P <numreq>        Pipeline <numreq> requests. Default 1 (no pipeline).
24 -e                 If server replies with errors, show them on stdout.
25                    (no more than 1 error per second is displayed)
26 -q                 Quiet. Just show query/sec values
27 --precision        Number of decimal places to display in latency output (default 0)
28 --csv              Output in CSV format
29 -l                 Loop. Run the tests forever
30 -t <tests>         Only run the comma separated list of tests. The test
31                    names are the same as the ones produced as output.
32 -I                 Idle mode. Just open N idle connections and wait.
33
34Examples:
35
36 Run the benchmark with the default configuration against 127.0.0.1:6379:
37   $ redis-benchmark
38
39 Use 20 parallel clients, for a total of 100k requests, against 192.168.1.1:
40   $ redis-benchmark -h 192.168.1.1 -p 6379 -n 100000 -c 20
41
42 Fill 127.0.0.1:6379 with about 1 million keys only using the SET test:
43   $ redis-benchmark -t set -n 1000000 -r 100000000
44
45 Benchmark 127.0.0.1:6379 for a few commands producing CSV output:
46   $ redis-benchmark -t ping,set,get -n 100000 --csv
47
48 Benchmark a specific command line:
49   $ redis-benchmark -r 10000 -n 10000 eval 'return redis.call("ping")' 0
50
51 Fill a list with 10000 random elements:
52   $ redis-benchmark -r 10000 -n 10000 lpush mylist __rand_int__
53
54 On user specified command lines __rand_int__ is replaced with a random integer
55 with a range of values selected by the -r option.

在线修改配置

1CONFIG GET    *(配置名,例如daemonize,protected-mode等)              # 查看配置
2CONFIG RESETSTAT    # 命令用于重置 INFO 命令中的某些统计数据
3CONFIG REWRITE        # 将修改的设置回写配置文件
4CONFIG SET              # 设置参数
 1CONFIG GET    *
2# 输出结果如下
3  1) "rdbchecksum"                                                                                                           
4  2) "yes"                                                                                                                   
5  3) "daemonize"                                                                                                             
6  4) "yes"                                                                                                                   
7  5) "io-threads-do-reads"                                                                                                   
8  6) "no"                                                                                                                    
9  7) "lua-replicate-commands"                                                                                                
10  8) "yes"                                                                                                                   
11  9) "always-show-logo"                                                                                                      
12 10) "no"                                                                                                                    
13 11) "protected-mode"                                                                                                        
14 12) "yes"                                                                                                                   
15 13) "rdbcompression"                                                                                                        
16 14) "yes"                                                                                                                   
17 15) "rdb-del-sync-files"                                                                                                    
18 16) "no"                                                                                                                    
19 17) "activerehashing"                                                                                                       
20 18) "yes"                                                                                                                   
21 19) "stop-writes-on-bgsave-error"                                                                                           
22 20) "yes"                                                                                                                   
23 21) "dynamic-hz"                                                                                                            
24 22) "yes"                                                                                                                   
25 23) "lazyfree-lazy-eviction"                                                                                                
26 24) "no"                                                                                                                    
27 25) "lazyfree-lazy-expire"                                                                                                  
28 26) "no"                                                                                                                    
29 27) "lazyfree-lazy-server-del"                                                                                              
30 28) "no"                                                                                                                    
31 29) "lazyfree-lazy-user-del"                                                                                                
32 30) "no"                                                                                                                    
33 31) "repl-disable-tcp-nodelay"                                                                                              
34 32) "no"                                                                                                                    
35 33) "repl-diskless-sync"                                                                                                    
36 34) "no"                                                                                                                    
37 35) "gopher-enabled"                                                                                                        
38 36) "no"                                                                                                                    
39 37) "aof-rewrite-incremental-fsync"
40 38) "yes"
41 39) "no-appendfsync-on-rewrite"
42 40) "no"
43 41) "cluster-require-full-coverage"
44 42) "yes"
45 43) "rdb-save-incremental-fsync"
46 44) "yes"
47 45) "aof-load-truncated"
48 46) "yes"
49 47) "aof-use-rdb-preamble"
50 48) "yes"
51 49) "cluster-replica-no-failover"
52 50) "no"
53 51) "cluster-slave-no-failover"
54 52) "no"
55 53) "replica-lazy-flush"
56 54) "no"
57 55) "slave-lazy-flush"
58 56) "no"
59 57) "replica-serve-stale-data"
60 58) "yes"
61 59) "slave-serve-stale-data"
62 60) "yes"
63 61) "replica-read-only"
64 62) "yes"
65 63) "slave-read-only"
66 64) "yes"
67 65) "replica-ignore-maxmemory"
68 66) "yes"
69 67) "slave-ignore-maxmemory"
70 68) "yes"
71 69) "jemalloc-bg-thread"
72 70) "yes"
73 71) "activedefrag"
74 72) "no"
75 73) "syslog-enabled"
76 74) "no"
77 75) "cluster-enabled"
78 76) "no"
79 77) "appendonly"
80 78) "no"
81 79) "cluster-allow-reads-when-down"
82 80) "no"
83 81) "aclfile"
84 82) ""
85 83) "unixsocket"
86 84) ""
87 85) "pidfile"
88 86) "/var/run/redis.pid"
89 87) "replica-announce-ip"
90 88) ""
91 89) "slave-announce-ip"
92 90) ""
93 91) "masteruser"
94 92) ""
95 93) "masterauth"
96 94) ""
97 95) "cluster-announce-ip"
98 96) ""
99 97) "syslog-ident"
100 98) "redis"
101 99) "dbfilename"
102100) "dump.rdb"
103101) "appendfilename"
104102) "appendonly.aof"
105103) "server_cpulist"
106104) ""
107105) "bio_cpulist"
108106) ""
109107) "aof_rewrite_cpulist"
110108) ""
111109) "bgsave_cpulist"
112110) ""
113111) "ignore-warnings"
114112) "ARM64-COW-BUG"
115113) "supervised"
116114) "no"
117115) "syslog-facility"
118116) "local0"
119117) "repl-diskless-load"
120118) "disabled"
121119) "loglevel"
122120) "notice"
123121) "maxmemory-policy"
124122) "noeviction"
125123) "appendfsync"
126124) "everysec"
127125) "oom-score-adj"
128126) "no"
129127) "databases"
130128) "16"
131129) "port"
132130) "6379"
133131) "io-threads"
134132) "1"
135133) "auto-aof-rewrite-percentage"
136134) "100"
137135) "cluster-replica-validity-factor"
138136) "10"
139137) "cluster-slave-validity-factor"
140138) "10"
141139) "list-max-ziplist-size"
142140) "-2"
143141) "tcp-keepalive"
144142) "300"
145143) "cluster-migration-barrier"
146144) "1"
147145) "active-defrag-cycle-min"
148146) "1"
149147) "active-defrag-cycle-max"
150148) "25"
151149) "active-defrag-threshold-lower"
152150) "10"
153151) "active-defrag-threshold-upper"
154152) "100"
155153) "lfu-log-factor"
156154) "10"
157155) "lfu-decay-time"
158156) "1"
159157) "replica-priority"
160158) "100"
161159) "slave-priority"
162160) "100"
163161) "repl-diskless-sync-delay"
164162) "5"
165163) "maxmemory-samples"
166164) "5"
167165) "timeout"
168166) "0"
169167) "replica-announce-port"
170168) "0"
171169) "slave-announce-port"
172170) "0"
173171) "tcp-backlog"
174172) "511"
175173) "cluster-announce-bus-port"
176174) "0"
177175) "cluster-announce-port"
178176) "0"
179177) "repl-timeout"
180178) "60"
181179) "repl-ping-replica-period"
182180) "10"
183181) "repl-ping-slave-period"
184182) "10"
185183) "list-compress-depth"
186184) "0"
187185) "rdb-key-save-delay"
188186) "0"
189187) "key-load-delay"
190188) "0"
191189) "active-expire-effort"
192190) "1"
193191) "hz"
194192) "10"
195193) "min-replicas-to-write"
196194) "0"
197195) "min-slaves-to-write"
198196) "0"
199197) "min-replicas-max-lag"
200198) "10"
201199) "min-slaves-max-lag"
202200) "10"
203201) "maxclients"
204202) "10000"
205203) "active-defrag-max-scan-fields"
206204) "1000"
207205) "slowlog-max-len"
208206) "128"
209207) "acllog-max-len"
210208) "128"
211209) "lua-time-limit"
212210) "5000"
213211) "cluster-node-timeout"
214212) "15000"
215213) "slowlog-log-slower-than"
216214) "10000"
217215) "latency-monitor-threshold"
218216) "0"
219217) "proto-max-bulk-len"
220218) "536870912"
221219) "stream-node-max-entries"
222220) "100"
223221) "repl-backlog-size"
224222) "1048576"
225223) "maxmemory"
226224) "0"
227225) "hash-max-ziplist-entries"
228226) "512"
229227) "set-max-intset-entries"
230228) "512"

小技巧:

config get 支持模糊匹配,例如包含所有re开头的配置名,config get re*,

所有配置名包含re的配置,可以使用config get *re*

在线修改密码

原本的密码是123321,这里我们将它修改为123123123,再一次去连接它。发现此时的密码已经修改


以上是关于Redis服务与连接那些事儿的主要内容,如果未能解决你的问题,请参考以下文章

个推首席架构师Qcon分享 |微服务架构的那些事儿

golang长连接和短连接的那些事儿

Linux中软硬链接那些事儿

jedis连接redis

Neutron社区每周记(11.28-12.2)| HAProxy 里的那些事儿

分布式系统的那些事儿 - 系统与系统之间的调用