经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Redis » 查看文章
Redis基础之配置文件
来源:cnblogs  作者:Alan.hsiang  时间:2021/1/18 16:20:41  对本文有异议

一般情况下,Redis配置文件中提供了很多默认的选项,可以不做任何修改而直接使用,本文主要简述配置文件中常用的配置选项,仅供学习分享使用,如有不足之处,还请指正。

Redis启动方式

Redis的启动时,必须有一个配置文件与之相匹配(如:/root/myredis/redis.conf),如下所示:

  1. 1 [root@localhost bin]# ./redis-server /root/myredis/redis.conf

 

Redis配置文件详解

Redis配置文件项,主要分为以下几个部分:

  1. INCLUDES:一个配置文件,可以导入其他配置文件。
  2. MODULES:启动时加载的模块。
  3. NETWORK:网络相关配置。
  4. TLS/SSL:安全传输相关模块。
  5. GENERAL:通用配置相关。
  6. SNAPSHOTTING:快照配置相关。
  7. REPLICATION:主从复制相关配置项。
  8. KEYS TRACKING:键的追踪相关配置项。
  9. SECURITY:安全相关配置。
  10. CLIENTS:客户端相关配置。
  11. MEMORY MANAGEMENT:内存管理相关配置项。
  12. LAZY FREEING:延迟释放配置项。
  13. THREADED I/O:多线程I/O相关配置项。
  14. KERNEL OOM CONTROL:Linux内核防止内存占用过大配置选项。
  15. APPEND ONLY MODE:数据持久化追加模式。
  16. LUA SCRIPTING:Lua脚本相关配置项。
  17. REDIS CLUSTER:Redis集群相关配置项。
  18. CLUSTER DOCKER/NAT support:Docker/Nat集群支持配置项。
  19. SLOW LOG:耗时日志。
  20. LATENCY MONITOR:延迟监控配置相关选项。
  21. EVENT NOTIFICATION:事件通知相关配置项。
  22. GOPHER SERVER:Gopher服务器。
  23. ADVANCED CONFIG:高级配置。
  24. ACTIVE DEFRAGMENTATION:活动碎片整理相关配置。

说明:Redis配置文件,以# 开头,表示注释或者说明;如启用,则将#去掉即可。

INCLUDES

如果redis启动时有一个标准的配置模板,但是又需要自定义每一个实例的相关配置,则可以采用include 配置文件的方式导入。默认不需要导入文件,则此处是注释的。如下所示:

  1. 1 # include /path/to/local.conf
  2. 2 # include /path/to/other.conf

MODULES

启动时加载需要启动的模块,默认不需要启动,所以也是注释的。如下所示:

  1. 1 # loadmodule /path/to/my_module.so
  2. 2 # loadmodule /path/to/other_module.so

NETWORK

NETWORK主要用于配置网络相关的项,具体如下所示:

bind配置可以访问的ip地址,可以配置多个,一般建议局域网内部ip。如果是在internet下运行redis,并绑定了所有的ip地址,则是非常危险的操作。

如果需要所有ip都可以访问,则可以注释掉下面的语句。如下所示:

  1. 1 # Examples:
  2. 2 #
  3. 3 # bind 192.168.1.100 10.0.0.1
  4. 4 # bind 127.0.0.1 ::1
  5. 5
  6. 6 bind 127.0.0.1

port指定redis启动时的端口号,默认6379,不建议使用1000以下的端口号,因为容易与操作系统端口号引起冲突。如下所示:

  1. 1 # Accept connections on the specified port, default is 6379 (IANA #815344).
  2. 2 # If port 0 is specified Redis will not listen on a TCP socket.
  3. 3 port 6379

tcp-backlog配置完整连接队列的大小,默认511,但是此值一般不能大于/proc/sys/net/core/somaxconn 配置的值。如下所示:

  1. 1 # In high requests-per-second environments you need a high backlog in order
  2. 2 # to avoid slow clients connection issues. Note that the Linux kernel
  3. 3 # will silently truncate it to the value of /proc/sys/net/core/somaxconn so
  4. 4 # make sure to raise both the value of somaxconn and tcp_max_syn_backlog
  5. 5 # in order to get the desired effect.
  6. 6 tcp-backlog 511

关于/proc/sys/net/core/somaxconn的值,如下所示:

  1. 1 [root@localhost bin]# cat /proc/sys/net/core/somaxconn
  2. 2 128

timeout设置超时时间,用于设置客户端多久没连接则断开连接人时间,0表示失效。如下所示:

  1. 1 # Close the connection after a client is idle for N seconds (0 to disable)
  2. 2 timeout 0

tcp-keepalive设置保持连接时长,超过时间,则发送TCP ACK到客户端,如下所示:

  1. 1 # A reasonable value for this option is 300 seconds, which is the new
  2. 2 # Redis default starting with Redis 3.2.1.
  3. 3 tcp-keepalive 300

GENERAL

daemonize用于配置redis是否以守护进程运行,默认为no,启动时会单独启动一个窗口,当窗口关闭时,redis进程结束。所以为了让redis在后台运行,需要将此项改为yes。如下所示:

  1. 1 # By default Redis does not run as a daemon. Use 'yes' if you need it.
  2. 2 # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
  3. 3 daemonize yes

pidfile配置进程文件,当redis以守护进行启动时,会在启动时生成,关闭时删除启动文件,如下所示:

  1. 1 # If a pid file is specified, Redis writes it where specified at startup
  2. 2 # and removes it at exit.
  3. 3 #
  4. 4 # When the server runs non daemonized, no pid file is created if none is
  5. 5 # specified in the configuration. When the server is daemonized, the pid file
  6. 6 # is used even if not specified, defaulting to "/var/run/redis.pid".
  7. 7 #
  8. 8 # Creating a pid file is best effort: if Redis is not able to create it
  9. 9 # nothing bad happens, the server will start and run normally.
  10. 10 pidfile /var/run/redis_6379.pid

loglevel日志等级,共四种等级:debug,verbose,notice,warning默认为notice,如下所示:

  1. 1 # Specify the server verbosity level.
  2. 2 # This can be one of:
  3. 3 # debug (a lot of information, useful for development/testing)
  4. 4 # verbose (many rarely useful info, but not a mess like the debug level)
  5. 5 # notice (moderately verbose, what you want in production probably)
  6. 6 # warning (only very important / critical messages are logged)
  7. 7 loglevel notice

logfile日志文件配置,默认为空,则不记录日志,如下所示:

  1. 1 # Specify the log file name. Also the empty string can be used to force
  2. 2 # Redis to log on the standard output. Note that if you use standard
  3. 3 # output for logging but daemonize, logs will be sent to /dev/null
  4. 4 logfile ""

syslog-enabled是否记录系统日志,默认不记录,如下所示:

  1. 1 # To enable logging to the system logger, just set 'syslog-enabled' to yes,
  2. 2 # and optionally update the other syslog parameters to suit your needs.
  3. 3 # syslog-enabled no
  4. 4
  5. 5 # Specify the syslog identity.
  6. 6 # syslog-ident redis
  7. 7
  8. 8 # Specify the syslog facility. Must be USER or between LOCAL0-LOCAL7.
  9. 9 # syslog-facility local0

databases数据库个数,redis默认共有16个数据库,默认是0,如下所示:

  1. 1 # Set the number of databases. The default database is DB 0, you can select
  2. 2 # a different one on a per-connection basis using SELECT <dbid> where
  3. 3 # dbid is a number between 0 and 'databases'-1
  4. 4 databases 16

always-show-logo设置logo是否显示,默认为yes,如下所示:

  1. 1 # By default Redis shows an ASCII art logo only when started to log to the
  2. 2 # standard output and if the standard output is a TTY. Basically this means
  3. 3 # that normally a logo is displayed only in interactive sessions.
  4. 4 #
  5. 5 # However it is possible to force the pre-4.0 behavior and always show a
  6. 6 # ASCII art logo in startup logs by setting the following option to yes.
  7. 7 always-show-logo yes

SNAPSHOTTING

快照主要用于将内存中的数据保存到硬盘上。

save命令,配置多久保存一次,可以设置多种模式,如下所示:

  1. 1 # Save the DB on disk:
  2. 2 #
  3. 3 # save <seconds> <changes>
  4. 4 #
  5. 5 # Will save the DB if both the given number of seconds and the given
  6. 6 # number of write operations against the DB occurred.
  7. 7 #
  8. 8 # In the example below the behavior will be to save:
  9. 9 # after 900 sec (15 min) if at least 1 key changed
  10. 10 # after 300 sec (5 min) if at least 10 keys changed
  11. 11 # after 60 sec if at least 10000 keys changed
  12. 12
  13. 13 save 900 1
  14. 14 save 300 10
  15. 15 save 60 10000

stop-writes-on-bgsave-error 当正在保存或者错误时,是否停止写入,默认为yes,如下所示:

  1. 1 # However if you have setup your proper monitoring of the Redis server
  2. 2 # and persistence, you may want to disable this feature so that Redis will
  3. 3 # continue to work as usual even if there are problems with disk,
  4. 4 # permissions, and so forth.
  5. 5 stop-writes-on-bgsave-error yes

rdbcompression 快照文件是否压缩,如果不压缩,则数据库文件将会很大,如下所示:

  1. 1 # Compress string objects using LZF when dump .rdb databases?
  2. 2 # By default compression is enabled as it's almost always a win.
  3. 3 # If you want to save some CPU in the saving child set it to 'no' but
  4. 4 # the dataset will likely be bigger if you have compressible values or keys.
  5. 5 rdbcompression yes

dbfilename数据库文件名,默认为dump.rdb。如下所示:

  1. 1 # The filename where to dump the DB
  2. 2 dbfilename dump.rdb

dir数据库文件的保存目录,如下所示:

  1. 1 # The working directory.
  2. 2 #
  3. 3 # The DB will be written inside this directory, with the filename specified
  4. 4 # above using the 'dbfilename' configuration directive.
  5. 5 #
  6. 6 # The Append Only File will also be created inside this directory.
  7. 7 #
  8. 8 # Note that you must specify a directory here, not a file name.
  9. 9 dir ./

SECURITY

requirepass设置默认用户登录的密码,默认无密码,如下所示:

  1. 1 # IMPORTANT NOTE: starting with Redis 6 "requirepass" is just a compatibility
  2. 2 # layer on top of the new ACL system. The option effect will be just setting
  3. 3 # the password for the default user. Clients will still authenticate using
  4. 4 # AUTH <password> as usually, or more explicitly with AUTH default <password>
  5. 5 # if they follow the new protocol: both will work.
  6. 6 #
  7. 7 # requirepass foobared

CLIENTS

maxclients同一时间允许连接的最大客户端数,如下所示:

  1. 1 # Set the max number of connected clients at the same time. By default
  2. 2 # this limit is set to 10000 clients, however if the Redis server is not
  3. 3 # able to configure the process file limit to allow for the specified limit
  4. 4 # the max number of allowed clients is set to the current file limit
  5. 5 # minus 32 (as Redis reserves a few file descriptors for internal uses).
  6. 6 #
  7. 7 # Once the limit is reached Redis will close all the new connections sending
  8. 8 # an error 'max number of clients reached'.
  9. 9 #
  10. 10 # maxclients 10000

当配置redis集群时,redis连接数和集群总线共享。

Redis命令模式配置

redis除了可以通过修改配置文件的方式变更配置项,也可以通过客户端进行修改。示例如下:

config get * 用于获取所有的配置项,如下所示:

  1. 1 127.0.0.1:6379> config get *
  2. 2 1) "rdbchecksum"
  3. 3 2) "yes"
  4. 4 3) "daemonize"
  5. 5 4) "yes"
  6. 6 5) "io-threads-do-reads"
  7. 7 6) "no"
  8. 8 7) "lua-replicate-commands"
  9. 9 8) "yes"
  10. 10 9) "always-show-logo"
  11. 11 10) "yes"
  12. 12 11) "protected-mode"
  13. 13 12) "yes"
  14. 14 13) "rdbcompression"
  15. 15 14) "yes"
  16. 16 15) "rdb-del-sync-files"
  17. 17 16) "no"
  18. 18 17) "activerehashing"
  19. 19 18) "yes"
  20. 20 19) "stop-writes-on-bgsave-error"
  21. 21 20) "yes"
  22. 22 21) "dynamic-hz"
  23. 23 22) "yes"
  24. 24 23) "lazyfree-lazy-eviction"
  25. 25 24) "no"
  26. 26 25) "lazyfree-lazy-expire"
  27. 27 26) "no"
  28. 28 27) "lazyfree-lazy-server-del"
  29. 29 28) "no"
  30. 30 29) "lazyfree-lazy-user-del"
  31. 31 30) "no"
  32. 32 31) "repl-disable-tcp-nodelay"
  33. 33 32) "no"
  34. 34 33) "repl-diskless-sync"
  35. 35 34) "no"
  36. 36 35) "gopher-enabled"
  37. 37 36) "no"
  38. 38 37) "aof-rewrite-incremental-fsync"
  39. 39 38) "yes"
  40. 40 39) "no-appendfsync-on-rewrite"
  41. 41 40) "no"
  42. 42 41) "cluster-require-full-coverage"
  43. 43 42) "yes"
  44. 44 43) "rdb-save-incremental-fsync"
  45. 45 44) "yes"
  46. 46 45) "aof-load-truncated"
  47. 47 46) "yes"
  48. 48 47) "aof-use-rdb-preamble"
  49. 49 48) "yes"
  50. 50 49) "cluster-replica-no-failover"
  51. 51 50) "no"
  52. 52 51) "cluster-slave-no-failover"
  53. 53 52) "no"
  54. 54 53) "replica-lazy-flush"
  55. 55 54) "no"
  56. 56 55) "slave-lazy-flush"
  57. 57 56) "no"
  58. 58 57) "replica-serve-stale-data"
  59. 59 58) "yes"
  60. 60 59) "slave-serve-stale-data"
  61. 61 60) "yes"
  62. 62 61) "replica-read-only"
  63. 63 62) "yes"
  64. 64 63) "slave-read-only"
  65. 65 64) "yes"
  66. 66 65) "replica-ignore-maxmemory"
  67. 67 66) "yes"
  68. 68 67) "slave-ignore-maxmemory"
  69. 69 68) "yes"
  70. 70 69) "jemalloc-bg-thread"
  71. 71 70) "yes"
  72. 72 71) "activedefrag"
  73. 73 72) "no"
  74. 74 73) "syslog-enabled"
  75. 75 74) "no"
  76. 76 75) "cluster-enabled"
  77. 77 76) "no"
  78. 78 77) "appendonly"
  79. 79 78) "no"
  80. 80 79) "cluster-allow-reads-when-down"
  81. 81 80) "no"
  82. 82 81) "oom-score-adj"
  83. 83 82) "no"
  84. 84 83) "aclfile"
  85. 85 84) ""
  86. 86 85) "unixsocket"
  87. 87 86) ""
  88. 88 87) "pidfile"
  89. 89 88) "/var/run/redis_6379.pid"
  90. 90 89) "replica-announce-ip"
  91. 91 90) ""
  92. 92 91) "slave-announce-ip"
  93. 93 92) ""
  94. 94 93) "masteruser"
  95. 95 94) ""
  96. 96 95) "masterauth"
  97. 97 96) ""
  98. 98 97) "cluster-announce-ip"
  99. 99 98) ""
  100. 100 99) "syslog-ident"
  101. 101 100) "redis"
  102. 102 101) "dbfilename"
  103. 103 102) "dump.rdb"
  104. 104 103) "appendfilename"
  105. 105 104) "appendonly.aof"
  106. 106 105) "server_cpulist"
  107. 107 106) ""
  108. 108 107) "bio_cpulist"
  109. 109 108) ""
  110. 110 109) "aof_rewrite_cpulist"
  111. 111 110) ""
  112. 112 111) "bgsave_cpulist"
  113. 113 112) ""
  114. 114 113) "supervised"
  115. 115 114) "no"
  116. 116 115) "syslog-facility"
  117. 117 116) "local0"
  118. 118 117) "repl-diskless-load"
  119. 119 118) "disabled"
  120. 120 119) "loglevel"
  121. 121 120) "notice"
  122. 122 121) "maxmemory-policy"
  123. 123 122) "noeviction"
  124. 124 123) "appendfsync"
  125. 125 124) "everysec"
  126. 126 125) "databases"
  127. 127 126) "16"
  128. 128 127) "port"
  129. 129 128) "6379"
  130. 130 129) "io-threads"
  131. 131 130) "1"
  132. 132 131) "auto-aof-rewrite-percentage"
  133. 133 132) "100"
  134. 134 133) "cluster-replica-validity-factor"
  135. 135 134) "10"
  136. 136 135) "cluster-slave-validity-factor"
  137. 137 136) "10"
  138. 138 137) "list-max-ziplist-size"
  139. 139 138) "-2"
  140. 140 139) "tcp-keepalive"
  141. 141 140) "300"
  142. 142 141) "cluster-migration-barrier"
  143. 143 142) "1"
  144. 144 143) "active-defrag-cycle-min"
  145. 145 144) "1"
  146. 146 145) "active-defrag-cycle-max"
  147. 147 146) "25"
  148. 148 147) "active-defrag-threshold-lower"
  149. 149 148) "10"
  150. 150 149) "active-defrag-threshold-upper"
  151. 151 150) "100"
  152. 152 151) "lfu-log-factor"
  153. 153 152) "10"
  154. 154 153) "lfu-decay-time"
  155. 155 154) "1"
  156. 156 155) "replica-priority"
  157. 157 156) "100"
  158. 158 157) "slave-priority"
  159. 159 158) "100"
  160. 160 159) "repl-diskless-sync-delay"
  161. 161 160) "5"
  162. 162 161) "maxmemory-samples"
  163. 163 162) "5"
  164. 164 163) "timeout"
  165. 165 164) "0"
  166. 166 165) "replica-announce-port"
  167. 167 166) "0"
  168. 168 167) "slave-announce-port"
  169. 169 168) "0"
  170. 170 169) "tcp-backlog"
  171. 171 170) "511"
  172. 172 171) "cluster-announce-bus-port"
  173. 173 172) "0"
  174. 174 173) "cluster-announce-port"
  175. 175 174) "0"
  176. 176 175) "repl-timeout"
  177. 177 176) "60"
  178. 178 177) "repl-ping-replica-period"
  179. 179 178) "10"
  180. 180 179) "repl-ping-slave-period"
  181. 181 180) "10"
  182. 182 181) "list-compress-depth"
  183. 183 182) "0"
  184. 184 183) "rdb-key-save-delay"
  185. 185 184) "0"
  186. 186 185) "key-load-delay"
  187. 187 186) "0"
  188. 188 187) "active-expire-effort"
  189. 189 188) "1"
  190. 190 189) "hz"
  191. 191 190) "10"
  192. 192 191) "min-replicas-to-write"
  193. 193 192) "0"
  194. 194 193) "min-slaves-to-write"
  195. 195 194) "0"
  196. 196 195) "min-replicas-max-lag"
  197. 197 196) "10"
  198. 198 197) "min-slaves-max-lag"
  199. 199 198) "10"
  200. 200 199) "maxclients"
  201. 201 200) "10000"
  202. 202 201) "active-defrag-max-scan-fields"
  203. 203 202) "1000"
  204. 204 203) "slowlog-max-len"
  205. 205 204) "128"
  206. 206 205) "acllog-max-len"
  207. 207 206) "128"
  208. 208 207) "lua-time-limit"
  209. 209 208) "5000"
  210. 210 209) "cluster-node-timeout"
  211. 211 210) "15000"
  212. 212 211) "slowlog-log-slower-than"
  213. 213 212) "10000"
  214. 214 213) "latency-monitor-threshold"
  215. 215 214) "0"
  216. 216 215) "proto-max-bulk-len"
  217. 217 216) "536870912"
  218. 218 217) "stream-node-max-entries"
  219. 219 218) "100"
  220. 220 219) "repl-backlog-size"
  221. 221 220) "1048576"
  222. 222 221) "maxmemory"
  223. 223 222) "0"
  224. 224 223) "hash-max-ziplist-entries"
  225. 225 224) "512"
  226. 226 225) "set-max-intset-entries"
  227. 227 226) "512"
  228. 228 227) "zset-max-ziplist-entries"
  229. 229 228) "128"
  230. 230 229) "active-defrag-ignore-bytes"
  231. 231 230) "104857600"
  232. 232 231) "hash-max-ziplist-value"
  233. 233 232) "64"
  234. 234 233) "stream-node-max-bytes"
  235. 235 234) "4096"
  236. 236 235) "zset-max-ziplist-value"
  237. 237 236) "64"
  238. 238 237) "hll-sparse-max-bytes"
  239. 239 238) "3000"
  240. 240 239) "tracking-table-max-keys"
  241. 241 240) "1000000"
  242. 242 241) "repl-backlog-ttl"
  243. 243 242) "3600"
  244. 244 243) "auto-aof-rewrite-min-size"
  245. 245 244) "67108864"
  246. 246 245) "logfile"
  247. 247 246) ""
  248. 248 247) "client-query-buffer-limit"
  249. 249 248) "1073741824"
  250. 250 249) "watchdog-period"
  251. 251 250) "0"
  252. 252 251) "dir"
  253. 253 252) "/usr/local/redis/bin"
  254. 254 253) "save"
  255. 255 254) "900 1 300 10 60 10000"
  256. 256 255) "client-output-buffer-limit"
  257. 257 256) "normal 0 0 0 slave 268435456 67108864 60 pubsub 33554432 8388608 60"
  258. 258 257) "unixsocketperm"
  259. 259 258) "0"
  260. 260 259) "slaveof"
  261. 261 260) ""
  262. 262 261) "notify-keyspace-events"
  263. 263 262) ""
  264. 264 263) "bind"
  265. 265 264) "127.0.0.1"
  266. 266 265) "requirepass"
  267. 267 266) ""
  268. 268 267) "oom-score-adj-values"
  269. 269 268) "0 200 800"
View Code

config get setting-name,获取单独的配置项,如loglevel,如下所示:

  1. 1 127.0.0.1:6379> config get loglevel
  2. 2 1) "loglevel"
  3. 3 2) "notice"
  4. 4 127.0.0.1:6379>

 config set setting-name setting-value的格式来设置项,如下所示:

  1. 1 127.0.0.1:6379> config set loglevel verbose
  2. 2 OK
  3. 3 127.0.0.1:6379> config get loglevel
  4. 4 1) "loglevel"
  5. 5 2) "verbose"
  6. 6 127.0.0.1:6379>

本文主要简述常见配置,更多配置,可参考菜鸟教程

备注

洞仙歌·冰肌玉骨

朝代:宋朝|作者:苏轼

仆七岁时,见眉州老尼,姓朱,忘其名,年九十岁。自言尝随其师入蜀主孟昶宫中,一日大热,蜀主与花蕊夫人夜纳凉摩诃池上,作一词,朱具能记之。
今四十年,朱已死久矣,人无知此词者,但记其首两句,暇日寻味,岂《洞仙歌》令乎?乃为足之云。
 
冰肌玉骨,自清凉无汗。
水殿风来暗香满。
绣帘开,一点明月窥人,人未寝,欹(qī)枕钗横鬓乱。
 
起来携素手,庭户无声,时见疏星渡河汉。
试问夜如何?
夜已三更,金波淡,玉绳低转。
但屈指西风几时来,
又不道流年暗中偷换。

原文链接:http://www.cnblogs.com/hsiang/p/14287098.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号