Ubuntu16.04安装Redis并配置

时间:2021-08-15 03:58:07

Ubuntu16.04安装Redis并配置

2018年05月22日 10:40:35 Hello_刘 阅读数:29146

Ubuntu16.04安装Redis并配置
1):安装:

1:终端命令下载redis-4.0.9.tar.gz包

wget http://download.redis.io/releases/redis-4.0.9.tar.gz包

2:解压

tar xzf redis-4.0.9.tar.gz

3:移动,放到usr/local⽬录下

sudo mv ./redis-4.0.9 /usr/local/redis/

4:进⼊redis⽬录

cd /usr/local/redis/

5:生成

sudo make

6:测试,这段运⾏时间会较⻓

sudo make test

7:安装,将redis的命令安装到/usr/local/bin/⽬录

sudo make install

8:安装完成后,进入目录/usr/local/bin中查看

  1. cd /usr/local/bin
  2. ls -all

Ubuntu16.04安装Redis并配置

  • redis-server redis服务器
  • redis-cli redis命令行客户端
  • redis-benchmark redis性能测试工具
  • redis-check-aof AOF文件修复工具
  • redis-check-rdb RDB文件检索工具

9:把配置⽂件移动到/etc/redis⽬录下
配置⽂件⽬录为/usr/local/redis/redis.conf

在/etc/目录下创建redis目录,然后移动配置文件

sudo cp /usr/local/redis/redis.conf /etc/redis/

2):配置
1:查看
Redis的配置信息在/etc/redis/redis.conf下

sudo vim /etc/redis/redis.conf

2:核心配置

绑定ip:如果需要远程访问,可将此⾏注释,或绑定⼀个真实ip
bind 127.0.0.1

端⼝,默认为6379
port 6379

是否以守护进程运⾏

  • 如果以守护进程运⾏,则不会在命令⾏阻塞,类似于服务
  • 如果以⾮守护进程运⾏,则当前终端被阻塞
  • 设置为yes表示守护进程,设置为no表示⾮守护进程
  • 推荐设置为yes

daemonize yes

数据⽂件
dbfilename dump.rdb

数据⽂件存储路径
dir /var/lib/redis

⽇志⽂件
logfile "/var/log/redis/redis-server.log"

数据库,默认有16个
database 16

主从复制,类似于双机备份。

slaveof

详细配置参数:

  1. # Redis configuration file example
  2. # Note on units: when memory size is needed, it is possible to specify
  3. # it in the usual form of 1k 5GB 4M and so forth:
  4. # 内存大小的配置,下面是内存大小配置的转换方式
  5. #
  6. # 1k => 1000 bytes
  7. # 1kb => 1024 bytes
  8. # 1m => 1000000 bytes
  9. # 1mb => 1024*1024 bytes
  10. # 1g => 1000000000 bytes
  11. # 1gb => 1024*1024*1024 bytes
  12. #
  13. # units are case insensitive so 1GB 1Gb 1gB are all the same.
  14. # 内存大小的配置,不区分大小写
  15. ################################## INCLUDES ###################################
  16. # Include one or more other config files here. This is useful if you
  17. # have a standard template that goes to all Redis server but also need
  18. # to customize a few per-server settings. Include files can include
  19. # other files, so use this wisely.
  20. #
  21. # Notice option "include" won't be rewritten by command "CONFIG REWRITE"
  22. # from admin or Redis Sentinel. Since Redis always uses the last processed
  23. # line as value of a configuration directive, you'd better put includes
  24. # at the beginning of this file to avoid overwriting config change at runtime.
  25. #
  26. # If instead you are interested in using includes to override configuration
  27. # options, it is better to use include as the last line.
  28. #
  29. # include /path/to/local.conf
  30. # include /path/to/other.conf
  31. # 当配置多个redis时,可能大部分配置一样,而对于不同的redis,只有少部分配置需要定制
  32. # 就可以配置一个公共的模板配置。
  33. # 对于具体的reids,只需设置少量的配置,并用include把模板配置包含进来即可。
  34. #
  35. # 值得注意的是,对于同一个配置项,redis只对最后一行的有效
  36. # 所以为避免模板配置覆盖当前配置,应在配置文件第一行使用include
  37. # 当然,如果模板配置的优先级比较高,就在配置文件最后一行使用include
  38. ################################ GENERAL #####################################
  39. # By default Redis does not run as a daemon. Use 'yes' if you need it.
  40. # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
  41. # yes为使用守护进程,此时redis的进程ID会被写进 pidfile的配置中
  42. daemonize yes
  43. # When running daemonized, Redis writes a pid file in /var/run/redis.pid by
  44. # default. You can specify a custom pid file location here.
  45. # 当redis以守护进程的方式启动时,redis的进程ID将会写在这个文件中
  46. pidfile /var/run/redis.pid
  47. # Accept connections on the specified port, default is 6379.
  48. # If port 0 is specified Redis will not listen on a TCP socket.
  49. # redis 启动的端口。【应该知道redis是服务端吧】
  50. port 6379
  51. # TCP listen() backlog.
  52. #
  53. # In high requests-per-second environments you need an high backlog in order
  54. # to avoid slow clients connections issues. Note that the Linux kernel
  55. # will silently truncate it to the value of /proc/sys/net/core/somaxconn so
  56. # make sure to raise both the value of somaxconn and tcp_max_syn_backlog
  57. # in order to get the desired effect.
  58. # 最大链接缓冲池的大小,这里应该是指的未完成链接请求的数量
  59. #(测试值为1时,仍可以有多个链接)
  60. # 但该值与listen函数中的backlog意义应该是相同的,源码中该值就是被用在了listen函数中
  61. # 该值同时受/proc/sys/net/core/somaxconn 和 tcp_max_syn_backlog(/etc/sysctl.conf中配置)的限制
  62. # tcp_max_syn_backlog 指的是未完成链接的数量
  63. tcp-backlog 511
  64. # By default Redis listens for connections from all the network interfaces
  65. # available on the server. It is possible to listen to just one or multiple
  66. # interfaces using the "bind" configuration directive, followed by one or
  67. # more IP addresses.
  68. # 绑定ip,指定ip可以连接到redis
  69. #
  70. # Examples:
  71. #
  72. # bind 192.168.1.100 10.0.0.1
  73. # bind 127.0.0.1
  74. # Specify the path for the Unix socket that will be used to listen for
  75. # incoming connections. There is no default, so Redis will not listen
  76. # on a unix socket when not specified.
  77. #
  78. # 这个应该就是以文件形式创建的socket
  79. # unixsocket /tmp/redis.sock
  80. # unixsocketperm 755
  81. # Close the connection after a client is idle for N seconds (0 to disable)
  82. # 超时断链机制,如果一个链接在N秒内没有任何操作,则断开该链接
  83. # N为0时,该机制失效
  84. timeout 0
  85. # TCP keepalive.
  86. #
  87. # If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence
  88. # of communication. This is useful for two reasons:
  89. #
  90. # 1) Detect dead peers.
  91. # 2) Take the connection alive from the point of view of network
  92. # equipment in the middle.
  93. #
  94. # On Linux, the specified value (in seconds) is the period used to send ACKs.
  95. # Note that to close the connection the double of the time is needed.
  96. # On other kernels the period depends on the kernel configuration.
  97. # 就像心跳检测一样,检查链接是否保持正常,同时也可以保持正常链接的通信
  98. # 建议值为60
  99. #
  100. # A reasonable value for this option is 60 seconds.
  101. tcp-keepalive 0
  102. # Specify the server verbosity level.
  103. # This can be one of:
  104. # debug (a lot of information, useful for development/testing)
  105. # verbose (many rarely useful info, but not a mess like the debug level)
  106. # notice (moderately verbose, what you want in production probably)
  107. # warning (only very important / critical messages are logged)
  108. # 日志级别
  109. loglevel notice
  110. # Specify the log file name. Also the empty string can be used to force
  111. # Redis to log on the standard output. Note that if you use standard
  112. # output for logging but daemonize, logs will be sent to /dev/null
  113. # 日志存放路径,默认是输出到标准输出,但当以守护进程方式启动时,默认输出到/dev/null(传说中的linux黑洞)
  114. logfile ""
  115. # To enable logging to the system logger, just set 'syslog-enabled' to yes,
  116. # and optionally update the other syslog parameters to suit your needs.
  117. # yes 表示将日志写到系统日志中
  118. # syslog-enabled no
  119. # Specify the syslog identity.
  120. # 当syslog-enabled为yes时,指定系统日志的标示为 redis
  121. # syslog-ident redis
  122. # Specify the syslog facility. Must be USER or between LOCAL0-LOCAL7.
  123. # 指定系统日志的设备
  124. # syslog-facility local0
  125. # Set the number of databases. The default database is DB 0, you can select
  126. # a different one on a per-connection basis using SELECT <dbid> where
  127. # dbid is a number between 0 and 'databases'-1
  128. # redis的数据库格式,默认16个(0~15),默认使用第0个。
  129. databases 16
  130. ################################ SNAPSHOTTING ################################
  131. #
  132. # Save the DB on disk:
  133. #
  134. # save <seconds> <changes>
  135. #
  136. # Will save the DB if both the given number of seconds and the given
  137. # number of write operations against the DB occurred.
  138. # 快照,即将数据写到硬盘上,在<seconds>秒内,至少有<changes>次写入数据库操作
  139. # 则会将数据写入硬盘一次。
  140. # 将save行注释掉则永远不会写入硬盘
  141. # save "" 表示删除所有的快照点
  142. #
  143. # In the example below the behaviour will be to save:
  144. # after 900 sec (15 min) if at least 1 key changed
  145. # after 300 sec (5 min) if at least 10 keys changed
  146. # after 60 sec if at least 10000 keys changed
  147. #
  148. # Note: you can disable saving at all commenting all the "save" lines.
  149. #
  150. # It is also possible to remove all the previously configured save
  151. # points by adding a save directive with a single empty string argument
  152. # like in the following example:
  153. #
  154. # save ""
  155. save 900 1
  156. save 300 10
  157. save 60 10000
  158. # By default Redis will stop accepting writes if RDB snapshots are enabled
  159. # (at least one save point) and the latest background save failed.
  160. # This will make the user aware (in a hard way) that data is not persisting
  161. # on disk properly, otherwise chances are that no one will notice and some
  162. # disaster will happen.
  163. #
  164. # If the background saving process will start working again Redis will
  165. # automatically allow writes again.
  166. #
  167. # However if you have setup your proper monitoring of the Redis server
  168. # and persistence, you may want to disable this feature so that Redis will
  169. # continue to work as usual even if there are problems with disk,
  170. # permissions, and so forth.
  171. # 当做快照失败的时候,redis会停止继续向其写入数据,保证第一时间发现redis快照出现问题
  172. # 当然,通过下面配置为 no,即使redis快照失败,也能继续向redis写入数据
  173. stop-writes-on-bgsave-error yes
  174. # Compress string objects using LZF when dump .rdb databases?
  175. # For default that's set to 'yes' as it's almost always a win.
  176. # If you want to save some CPU in the saving child set it to 'no' but
  177. # the dataset will likely be bigger if you have compressible values or keys.
  178. # 快照的时候,是否用LZF压缩,使用压缩会占一定的cpu,但不使用压缩,快照会很大
  179. rdbcompression yes
  180. # Since version 5 of RDB a CRC64 checksum is placed at the end of the file.
  181. # This makes the format more resistant to corruption but there is a performance
  182. # hit to pay (around 10%) when saving and loading RDB files, so you can disable it
  183. # for maximum performances.
  184. #
  185. # RDB files created with checksum disabled have a checksum of zero that will
  186. # tell the loading code to skip the check.
  187. # 数据校验,快照末尾会存放一个校验值,保证数据的准确性
  188. # 但数据校验会使性能下降约10%,默认开启校验
  189. rdbchecksum yes
  190. # The filename where to dump the DB
  191. # 快照的名字
  192. dbfilename dump.rdb
  193. # The working directory.
  194. #
  195. # The DB will be written inside this directory, with the filename specified
  196. # above using the 'dbfilename' configuration directive.
  197. #
  198. # The Append Only File will also be created inside this directory.
  199. #
  200. # Note that you must specify a directory here, not a file name.
  201. #
  202. # 快照存放的目录
  203. # linux root下测试,会发现该进程会在当前目录下创建一个dump.rdb
  204. # 但快照却放在了根目录/下,重启的时候,是不会从快照中恢复数据的
  205. # 当把根目录下的dump.rdb文件拷贝到当前目录的时候,再次启动,就会从快照中恢复数据
  206. # 而且以后的快照也都在当前目录的dump.rdb中做操作
  207. #
  208. # 值得一提的是,快照是异步方式的,如果在还未达到快照的时候,修改了数据,而且redis发生问题crash了
  209. # 那么中间的修改数据是不会被保存到dump.rdb快照中的
  210. # 解决办法就是用Append Only Mode的同步模式(下面将会有该配置项)
  211. # 将会把每个操作写到Append Only File中,该文件也存放于当前配置的目录
  212. # 建议使用绝对路径!!!
  213. #
  214. dir ./
  215. ################################# REPLICATION #################################
  216. # Master-Slave replication. Use slaveof to make a Redis instance a copy of
  217. # another Redis server. Note that the configuration is local to the slave
  218. # so for example it is possible to configure the slave to save the DB with a
  219. # different interval, or to listen to another port, and so on.
  220. #
  221. # 主从复制,类似于双机备份。
  222. # 配置需指定主机的ip 和port
  223. # slaveof <masterip> <masterport>
  224. # If the master is password protected (using the "requirepass" configuration
  225. # directive below) it is possible to tell the slave to authenticate before
  226. # starting the replication synchronization process, otherwise the master will
  227. # refuse the slave request.
  228. #
  229. # 如果主机redis需要密码,则指定密码
  230. # 密码配置在下面安全配置中
  231. # masterauth <master-password>
  232. # When a slave loses its connection with the master, or when the replication
  233. # is still in progress, the slave can act in two different ways:
  234. #
  235. # 1) if slave-serve-stale-data is set to 'yes' (the default) the slave will
  236. # still reply to client requests, possibly with out of date data, or the
  237. # data set may just be empty if this is the first synchronization.
  238. #
  239. # 2) if slave-serve-stale-data is set to 'no' the slave will reply with
  240. # an error "SYNC with master in progress" to all the kind of commands
  241. # but to INFO and SLAVEOF.
  242. #
  243. # 当从机与主机断开时,即同步出现问题的时候,从机有两种处理方式
  244. # yes, 继续响应客户端请求,但可能有脏数据(过期数据、空数据等)
  245. # no,对客户端的请求统一回复为“SYNC with master in progress”,除了INFO和SLAVEOF命令
  246. slave-serve-stale-data yes
  247. # You can configure a slave instance to accept writes or not. Writing against
  248. # a slave instance may be useful to store some ephemeral data (because data
  249. # written on a slave will be easily deleted after resync with the master) but
  250. # may also cause problems if clients are writing to it because of a
  251. # misconfiguration.
  252. #
  253. # Since Redis 2.6 by default slaves are read-only.
  254. #
  255. # Note: read only slaves are not designed to be exposed to untrusted clients
  256. # on the internet. It's just a protection layer against misuse of the instance.
  257. # Still a read only slave exports by default all the administrative commands
  258. # such as CONFIG, DEBUG, and so forth. To a limited extent you can improve
  259. # security of read only slaves using 'rename-command' to shadow all the
  260. # administrative / dangerous commands.
  261. # slave只读选项,设置从机只读(默认)。
  262. # 即使设置可写,当下一次从主机上同步数据,仍然会删除当前从机上写入的数据
  263. # 【待测试】:主机与从机互为slave会出现什么情况?
  264. # 【预期三种结果】:1. 提示报错 2. 主从服务器数据不可控 3. 一切正常
  265. slave-read-only yes
  266. # Slaves send PINGs to server in a predefined interval. It's possible to change
  267. # this interval with the repl_ping_slave_period option. The default value is 10
  268. # seconds.
  269. #
  270. # 从服务器向主服务器发送心跳包,默认10发送一次
  271. # repl-ping-slave-period 10
  272. # The following option sets the replication timeout for:
  273. #
  274. # 1) Bulk transfer I/O during SYNC, from the point of view of slave.
  275. # 2) Master timeout from the point of view of slaves (data, pings).
  276. # 3) Slave timeout from the point of view of masters (REPLCONF ACK pings).
  277. #
  278. # It is important to make sure that this value is greater than the value
  279. # specified for repl-ping-slave-period otherwise a timeout will be detected
  280. # every time there is low traffic between the master and the slave.
  281. #
  282. # 超时响应时间,值必须比repl-ping-slave-period大
  283. # 批量数据传输超时、ping超时
  284. # repl-timeout 60
  285. # Disable TCP_NODELAY on the slave socket after SYNC?
  286. #
  287. # If you select "yes" Redis will use a smaller number of TCP packets and
  288. # less bandwidth to send data to slaves. But this can add a delay for
  289. # the data to appear on the slave side, up to 40 milliseconds with
  290. # Linux kernels using a default configuration.
  291. #
  292. # If you select "no" the delay for data to appear on the slave side will
  293. # be reduced but more bandwidth will be used for replication.
  294. #
  295. # By default we optimize for low latency, but in very high traffic conditions
  296. # or when the master and slaves are many hops away, turning this to "yes" may
  297. # be a good idea.
  298. # 主从同步是否延迟
  299. # yes 有延迟,约40毫秒(linux kernel的默认配置),使用较少的数据包,较小的带宽
  300. # no 无延迟(减少延迟),但需要更大的带宽
  301. repl-disable-tcp-nodelay no
  302. # Set the replication backlog size. The backlog is a buffer that accumulates
  303. # slave data when slaves are disconnected for some time, so that when a slave
  304. # wants to reconnect again, often a full resync is not needed, but a partial
  305. # resync is enough, just passing the portion of data the slave missed while
  306. # disconnected.
  307. #
  308. # The biggest the replication backlog, the longer the time the slave can be
  309. # disconnected and later be able to perform a partial resynchronization.
  310. #
  311. # The backlog is only allocated once there is at least a slave connected.
  312. #
  313. # 默认情况下,当slave重连的时候,会进行全量数据同步
  314. # 但实际上slave只需要部分同步即可,这个选项设置部分同步的大小
  315. # 设置值越大,同步的时间就越长
  316. # repl-backlog-size 1mb
  317. # After a master has no longer connected slaves for some time, the backlog
  318. # will be freed. The following option configures the amount of seconds that
  319. # need to elapse, starting from the time the last slave disconnected, for
  320. # the backlog buffer to be freed.
  321. #
  322. # A value of 0 means to never release the backlog.
  323. #
  324. # 主机的后台日志释放时间,即当没有slave连接时,过多久释放后台日志
  325. # 0表示不释放
  326. # repl-backlog-ttl 3600
  327. # The slave priority is an integer number published by Redis in the INFO output.
  328. # It is used by Redis Sentinel in order to select a slave to promote into a
  329. # master if the master is no longer working correctly.
  330. #
  331. # A slave with a low priority number is considered better for promotion, so
  332. # for instance if there are three slaves with priority 10, 100, 25 Sentinel will
  333. # pick the one with priority 10, that is the lowest.
  334. #
  335. # However a special priority of 0 marks the slave as not able to perform the
  336. # role of master, so a slave with priority of 0 will never be selected by
  337. # Redis Sentinel for promotion.
  338. #
  339. # By default the priority is 100.
  340. # 当主机crash的时候,在从机中选择一台作为主机,数字越小,优先级越高
  341. # 0 表示永远不作为主机,默认值是100
  342. slave-priority 100
  343. # It is possible for a master to stop accepting writes if there are less than
  344. # N slaves connected, having a lag less or equal than M seconds.
  345. #
  346. # The N slaves need to be in "online" state.
  347. #
  348. # The lag in seconds, that must be <= the specified value, is calculated from
  349. # the last ping received from the slave, that is usually sent every second.
  350. #
  351. # This option does not GUARANTEES that N replicas will accept the write, but
  352. # will limit the window of exposure for lost writes in case not enough slaves
  353. # are available, to the specified number of seconds.
  354. #
  355. # For example to require at least 3 slaves with a lag <= 10 seconds use:
  356. #
  357. # 当slave数量小于min-slaves-to-write,且延迟小于等于min-slaves-max-lag时,
  358. # 主机停止写入操作
  359. # 0表示禁用
  360. # 默认min-slaves-to-write为0,即禁用。min-slaves-max-lag为10
  361. # min-slaves-to-write 3
  362. # min-slaves-max-lag 10
  363. #
  364. # Setting one or the other to 0 disables the feature.
  365. #
  366. # By default min-slaves-to-write is set to 0 (feature disabled) and
  367. # min-slaves-max-lag is set to 10.
  368. ################################## SECURITY ###################################
  369. # Require clients to issue AUTH <PASSWORD> before processing any other
  370. # commands. This might be useful in environments in which you do not trust
  371. # others with access to the host running redis-server.
  372. #
  373. # This should stay commented out for backward compatibility and because most
  374. # people do not need auth (e.g. they run their own servers).
  375. #
  376. # Warning: since Redis is pretty fast an outside user can try up to
  377. # 150k passwords per second against a good box. This means that you should
  378. # use a very strong password otherwise it will be very easy to break.
  379. #
  380. # redis密码,默认不配置,即无密码
  381. # 这里注意,如果设置了密码,应该设置一个复杂度比较高的密码
  382. # 因为redis的速度很快,每秒可以尝试150k次的密码测试,很容易对其进行暴力破解(跑码)。
  383. # 疑问:这里为什么不设置一个针对主机的测试次数限制的,例如每10次,则禁止建立连接1个小时!
  384. # requirepass foobared
  385. # Command renaming.
  386. #
  387. # It is possible to change the name of dangerous commands in a shared
  388. # environment. For instance the CONFIG command may be renamed into something
  389. # hard to guess so that it will still be available for internal-use tools
  390. # but not available for general clients.
  391. #
  392. # 命令重命名,将命令重命名为另一个字符串标识
  393. # 如果命令为空串(""),则会彻底禁用该命令
  394. # 命令重命名,会对写AOF(Append of file)文件、slave从机造成一些问题
  395. # Example:
  396. #
  397. # rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52
  398. #
  399. # It is also possible to completely kill a command by renaming it into
  400. # an empty string:
  401. #
  402. # rename-command CONFIG ""
  403. #
  404. # Please note that changing the name of commands that are logged into the
  405. # AOF file or transmitted to slaves may cause problems.
  406. ################################### LIMITS ####################################
  407. # Set the max number of connected clients at the same time. By default
  408. # this limit is set to 10000 clients, however if the Redis server is not
  409. # able to configure the process file limit to allow for the specified limit
  410. # the max number of allowed clients is set to the current file limit
  411. # minus 32 (as Redis reserves a few file descriptors for internal uses).
  412. #
  413. # Once the limit is reached Redis will close all the new connections sending
  414. # an error 'max number of clients reached'.
  415. #
  416. # 这只redis的最大连接数目,默认设置为10000个客户端
  417. # 当超过限制时,将段开新的连接,并响应“max number of clients reached”
  418. # maxclients 10000
  419. # Don't use more memory than the specified amount of bytes.
  420. # When the memory limit is reached Redis will try to remove keys
  421. # according to the eviction policy selected (see maxmemory-policy).
  422. #
  423. # If Redis can't remove keys according to the policy, or if the policy is
  424. # set to 'noeviction', Redis will start to reply with errors to commands
  425. # that would use more memory, like SET, LPUSH, and so on, and will continue
  426. # to reply to read-only commands like GET.
  427. #
  428. # This option is usually useful when using Redis as an LRU cache, or to set
  429. # a hard memory limit for an instance (using the 'noeviction' policy).
  430. #
  431. # WARNING: If you have slaves attached to an instance with maxmemory on,
  432. # the size of the output buffers needed to feed the slaves are subtracted
  433. # from the used memory count, so that network problems / resyncs will
  434. # not trigger a loop where keys are evicted, and in turn the output
  435. # buffer of slaves is full with DELs of keys evicted triggering the deletion
  436. # of more keys, and so forth until the database is completely emptied.
  437. #
  438. # In short... if you have slaves attached it is suggested that you set a lower
  439. # limit for maxmemory so that there is some free RAM on the system for slave
  440. # output buffers (but this is not needed if the policy is 'noeviction').
  441. #
  442. # redis的最大内存限制,如果达到最大内存,会按照下面的maxmemory-policy进行清除
  443. # 如果不能再清除或者maxmemory-policy为noeviction,则对于需要增加空间的操作,将会返回错误
  444. maxmemory <1024*1024*1024>
  445. # MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
  446. # is reached. You can select among five behaviors:
  447. #
  448. # volatile-lru -> remove the key with an expire set using an LRU algorithm
  449. # allkeys-lru -> remove any key accordingly to the LRU algorithm
  450. # volatile-random -> remove a random key with an expire set
  451. # allkeys-random -> remove a random key, any key
  452. # volatile-ttl -> remove the key with the nearest expire time (minor TTL)
  453. # noeviction -> don't expire at all, just return an error on write operations
  454. #
  455. # Note: with any of the above policies, Redis will return an error on write
  456. # operations, when there are not suitable keys for eviction.
  457. #
  458. # At the date of writing this commands are: set setnx setex append
  459. # incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd
  460. # sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby
  461. # zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby
  462. # getset mset msetnx exec sort
  463. #
  464. # The default is:
  465. #
  466. # 内存删除策略,默认volatile-lru,利用LRU算法,删除过期的key
  467. maxmemory-policy volatile-lru
  468. # LRU and minimal TTL algorithms are not precise algorithms but approximated
  469. # algorithms (in order to save memory), so you can select as well the sample
  470. # size to check. For instance for default Redis will check three keys and
  471. # pick the one that was used less recently, you can change the sample size
  472. # using the following configuration directive.
  473. #
  474. # LRU算法与最小TTL算法只是相对精确的算法,并不是绝对精确的算法
  475. # 为了更精确,可以设置样本个数
  476. # 比如设置3个样本,redis会选取三个key,并选择删除那个上次使用时间最远的
  477. # maxmemory-samples 3
  478. ############################## APPEND ONLY MODE ###############################
  479. # By default Redis asynchronously dumps the dataset on disk. This mode is
  480. # good enough in many applications, but an issue with the Redis process or
  481. # a power outage may result into a few minutes of writes lost (depending on
  482. # the configured save points).
  483. #
  484. # The Append Only File is an alternative persistence mode that provides
  485. # much better durability. For instance using the default data fsync policy
  486. # (see later in the config file) Redis can lose just one second of writes in a
  487. # dramatic event like a server power outage, or a single write if something
  488. # wrong with the Redis process itself happens, but the operating system is
  489. # still running correctly.
  490. #
  491. # AOF and RDB persistence can be enabled at the same time without problems.
  492. # If the AOF is enabled on startup Redis will load the AOF, that is the file
  493. # with the better durability guarantees.
  494. #
  495. # Please check http://redis.io/topics/persistence for more information.
  496. # 将对redis所有的操作都保存到AOF文件中
  497. # 因为dump.rdb是异步的,在下次快照到达之前,如果出现crash等问题,会造成数据丢失
  498. # 而AOF文件时同步记录的,所以会完整的恢复数据
  499. appendonly no
  500. # The name of the append only file (default: "appendonly.aof")
  501. # AOF文件的名字
  502. appendfilename "appendonly.aof"
  503. # The fsync() call tells the Operating System to actually write data on disk
  504. # instead to wait for more data in the output buffer. Some OS will really flush
  505. # data on disk, some other OS will just try to do it ASAP.
  506. #
  507. # Redis supports three different modes:
  508. #
  509. # no: don't fsync, just let the OS flush the data when it wants. Faster.
  510. # always: fsync after every write to the append only log . Slow, Safest.
  511. # everysec: fsync only one time every second. Compromise.
  512. #
  513. # The default is "everysec", as that's usually the right compromise between
  514. # speed and data safety. It's up to you to understand if you can relax this to
  515. # "no" that will let the operating system flush the output buffer when
  516. # it wants, for better performances (but if you can live with the idea of
  517. # some data loss consider the default persistence mode that's snapshotting),
  518. # or on the contrary, use "always" that's very slow but a bit safer than
  519. # everysec.
  520. #
  521. # More details please check the following article:
  522. # http://antirez.com/post/redis-persistence-demystified.html
  523. #
  524. # If unsure, use "everysec".
  525. # redis的数据同步方式,三种
  526. # no,redis本身不做同步,由OS来做。redis的速度会很快
  527. # always,在每次写操作之后,redis都进行同步,即写入AOF文件。redis会变慢,但是数据更安全
  528. # everysec,折衷考虑,每秒同步一次数据。【默认】
  529. # appendfsync always
  530. appendfsync everysec
  531. # appendfsync no
  532. # When the AOF fsync policy is set to always or everysec, and a background
  533. # saving process (a background save or AOF log background rewriting) is
  534. # performing a lot of I/O against the disk, in some Linux configurations
  535. # Redis may block too long on the fsync() call. Note that there is no fix for
  536. # this currently, as even performing fsync in a different thread will block
  537. # our synchronous write(2) call.
  538. #
  539. # In order to mitigate this problem it's possible to use the following option
  540. # that will prevent fsync() from being called in the main process while a
  541. # BGSAVE or BGREWRITEAOF is in progress.
  542. #
  543. # This means that while another child is saving, the durability of Redis is
  544. # the same as "appendfsync none". In practical terms, this means that it is
  545. # possible to lose up to 30 seconds of log in the worst scenario (with the
  546. # default Linux settings).
  547. #
  548. # If you have latency problems turn this to "yes". Otherwise leave it as
  549. # "no" that is the safest pick from the point of view of durability.
  550. # redis的同步方式中,always和everysec,快照和写AOF可能会执行大量的硬盘I/O操作,
  551. # 而在一些Linux的配置中,redis会阻塞很久,而redis本身并没有很好的解决这一问题。
  552. # 为了缓和这一问题,redis提供no-appendfsync-on-rewrite选项,
  553. # 即当有另外一个进程在执行保存操作的时候,redis采用no的同步方式。
  554. # 最坏情况下会有延迟30秒的同步延迟。
  555. # 如果你觉得这样做会有潜在危险,则请将该选项改为yes。否则就保持默认值no(基于稳定性考虑)。
  556. no-appendfsync-on-rewrite no
  557. # Automatic rewrite of the append only file.
  558. # Redis is able to automatically rewrite the log file implicitly calling
  559. # BGREWRITEAOF when the AOF log size grows by the specified percentage.
  560. #
  561. # This is how it works: Redis remembers the size of the AOF file after the
  562. # latest rewrite (if no rewrite has happened since the restart, the size of
  563. # the AOF at startup is used).
  564. #
  565. # This base size is compared to the current size. If the current size is
  566. # bigger than the specified percentage, the rewrite is triggered. Also
  567. # you need to specify a minimal size for the AOF file to be rewritten, this
  568. # is useful to avoid rewriting the AOF file even if the percentage increase
  569. # is reached but it is still pretty small.
  570. #
  571. # Specify a percentage of zero in order to disable the automatic AOF
  572. # rewrite feature.
  573. # 自动重写AOF文件
  574. # 当AOF日志文件大小增长到指定百分比时,redis会自动隐式调用BGREWRITEAOF来重写AOF文件
  575. # redis会记录上次重写AOF文件之后的大小,
  576. # 如果当前文件大小增加了auto-aof-rewrite-percentage,则会触发重写AOF日志功能
  577. # 当然如果文件过小,比如小于auto-aof-rewrite-min-size这个大小,是不会触发重写AOF日志功能的
  578. # auto-aof-rewrite-percentage为0时,禁用重写功能
  579. auto-aof-rewrite-percentage 100
  580. auto-aof-rewrite-min-size 64mb
  581. ################################ LUA SCRIPTING ###############################
  582. # Max execution time of a Lua script in milliseconds.
  583. #
  584. # If the maximum execution time is reached Redis will log that a script is
  585. # still in execution after the maximum allowed time and will start to
  586. # reply to queries with an error.
  587. #
  588. # When a long running script exceed the maximum execution time only the
  589. # SCRIPT KILL and SHUTDOWN NOSAVE commands are available. The first can be
  590. # used to stop a script that did not yet called write commands. The second
  591. # is the only way to shut down the server in the case a write commands was
  592. # already issue by the script but the user don't want to wait for the natural
  593. # termination of the script.
  594. #
  595. # Set it to 0 or a negative value for unlimited execution without warnings.
  596. # LUA脚本的最大执行时间(单位是毫秒),默认5000毫秒,即5秒
  597. # 如果LUA脚本执行超过这个限制,可以调用SCRIPT KILL和SHUTDOWN NOSAVE命令。
  598. # SCRIPT KILL可以终止脚本执行
  599. # SHUTDOWN NOSAVE关闭服务,防止LUA脚本的写操作发生
  600. # 该值为0或者负数,表示没有限制时间
  601. lua-time-limit 5000
  602. ################################## SLOW LOG ###################################
  603. # The Redis Slow Log is a system to log queries that exceeded a specified
  604. # execution time. The execution time does not include the I/O operations
  605. # like talking with the client, sending the reply and so forth,
  606. # but just the time needed to actually execute the command (this is the only
  607. # stage of command execution where the thread is blocked and can not serve
  608. # other requests in the meantime).
  609. #
  610. # You can configure the slow log with two parameters: one tells Redis
  611. # what is the execution time, in microseconds, to exceed in order for the
  612. # command to get logged, and the other parameter is the length of the
  613. # slow log. When a new command is logged the oldest one is removed from the
  614. # queue of logged commands.
  615. # 记录执行比较慢的命令
  616. # 执行比较慢仅仅是指命令的执行时间,不包括客户端的链接与响应等时间
  617. # slowlog-log-slower-than 设定这个慢的时间,单位是微妙,即1000000表示1秒,0表示所有命令都记录,负数表示不记录
  618. # slowlog-max-len表示记录的慢命令的个数,超过限制,则最早记录的命令会被移除
  619. # 命令的长度没有限制,但是会消耗内存,用SLOWLOG RESET来收回这些消耗的内存
  620. # The following time is expressed in microseconds, so 1000000 is equivalent
  621. # to one second. Note that a negative number disables the slow log, while
  622. # a value of zero forces the logging of every command.
  623. slowlog-log-slower-than 10000
  624. # There is no limit to this length. Just be aware that it will consume memory.
  625. # You can reclaim memory used by the slow log with SLOWLOG RESET.
  626. slowlog-max-len 128
  627. ################################ LATENCY MONITOR ##############################
  628. # The Redis latency monitoring subsystem samples different operations
  629. # at runtime in order to collect data related to possible sources of
  630. # latency of a Redis instance.
  631. #
  632. # Via the LATENCY command this information is available to the user that can
  633. # print graphs and obtain reports.
  634. #
  635. # The system only logs operations that were performed in a time equal or
  636. # greater than the amount of milliseconds specified via the
  637. # latency-monitor-threshold configuration directive. When its value is set
  638. # to zero, the latency monitor is turned off.
  639. #
  640. # By default latency monitoring is disabled since it is mostly not needed
  641. # if you don't have latency issues, and collecting data has a performance
  642. # impact, that while very small, can be measured under big load. Latency
  643. # monitoring can easily be enalbed at runtime using the command
  644. # "CONFIG SET latency-monitor-threshold <milliseconds>" if needed.
  645. # 延迟监控器
  646. # redis延迟监控子系统在运行时,会抽样检测可能导致延迟的不同操作
  647. # 通过LATENCY命令可以打印相关信息和报告, 命令如下(摘自源文件注释):
  648. # LATENCY SAMPLES: return time-latency samples for the specified event.
  649. # LATENCY LATEST: return the latest latency for all the events classes.
  650. # LATENCY DOCTOR: returns an human readable analysis of instance latency.
  651. # LATENCY GRAPH: provide an ASCII graph of the latency of the specified event.
  652. #
  653. # 系统只记录超过设定值的操作,单位是毫秒,0表示禁用该功能
  654. # 可以通过命令“CONFIG SET latency-monitor-threshold <milliseconds>” 直接设置而不需要重启redis
  655. latency-monitor-threshold 0
  656. ############################# Event notification ##############################
  657. # Redis can notify Pub/Sub clients about events happening in the key space.
  658. # This feature is documented at http://redis.io/topics/keyspace-events
  659. #
  660. # For instance if keyspace events notification is enabled, and a client
  661. # performs a DEL operation on key "foo" stored in the Database 0, two
  662. # messages will be published via Pub/Sub:
  663. #
  664. # PUBLISH __keyspace@0__:foo del
  665. # PUBLISH __keyevent@0__:del foo
  666. #
  667. # It is possible to select the events that Redis will notify among a set
  668. # of classes. Every class is identified by a single character:
  669. #
  670. # K Keyspace events, published with __keyspace@<db>__ prefix.
  671. # E Keyevent events, published with __keyevent@<db>__ prefix.
  672. # g Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ...
  673. # $ String commands
  674. # l List commands
  675. # s Set commands
  676. # h Hash commands
  677. # z Sorted set commands
  678. # x Expired events (events generated every time a key expires)
  679. # e Evicted events (events generated when a key is evicted for maxmemory)
  680. # A Alias for g$lshzxe, so that the "AKE" string means all the events.
  681. #
  682. # The "notify-keyspace-events" takes as argument a string that is composed
  683. # by zero or multiple characters. The empty string means that notifications
  684. # are disabled at all.
  685. #
  686. # Example: to enable list and generic events, from the point of view of the
  687. # event name, use:
  688. #
  689. # notify-keyspace-events Elg
  690. #
  691. # Example 2: to get the stream of the expired keys subscribing to channel
  692. # name __keyevent@0__:expired use:
  693. #
  694. # notify-keyspace-events Ex
  695. #
  696. # By default all notifications are disabled because most users don't need
  697. # this feature and the feature has some overhead. Note that if you don't
  698. # specify at least one of K or E, no events will be delivered.
  699. # 事件通知,当事件发生时,redis可以通知Pub/Sub客户端
  700. # 空串表示禁用事件通知
  701. # 注意:K和E至少要指定一个,否则不会有事件通知
  702. notify-keyspace-events ""
  703. ############################### ADVANCED CONFIG ###############################
  704. # Hashes are encoded using a memory efficient data structure when they have a
  705. # small number of entries, and the biggest entry does not exceed a given
  706. # threshold. These thresholds can be configured using the following directives.
  707. # 当hash数目比较少,并且最大元素没有超过给定值时,Hash使用比较有效的内存数据结构来存储。
  708. # 即ziplist的结构(压缩的双向链表),参考:http://blog.csdn.net/benbendy1984/article/details/7796956
  709. hash-max-ziplist-entries 512
  710. hash-max-ziplist-value 64
  711. # Similarly to hashes, small lists are also encoded in a special way in order
  712. # to save a lot of space. The special representation is only used when
  713. # you are under the following limits:
  714. # List配置同Hash
  715. list-max-ziplist-entries 512
  716. list-max-ziplist-value 64
  717. # Sets have a special encoding in just one case: when a set is composed
  718. # of just strings that happens to be integers in radix 10 in the range
  719. # of 64 bit signed integers.
  720. # The following configuration setting sets the limit in the size of the
  721. # set in order to use this special memory saving encoding.
  722. # Sets的元素如果全部是整数(10进制),且为64位有符号整数,则采用特殊的编码方式。
  723. # 其元素个数限制配置如下:
  724. set-max-intset-entries 512
  725. # Similarly to hashes and lists, sorted sets are also specially encoded in
  726. # order to save a lot of space. This encoding is only used when the length and
  727. # elements of a sorted set are below the following limits:
  728. # sorted set 同Hash和List
  729. zset-max-ziplist-entries 128
  730. zset-max-ziplist-value 64
  731. # HyperLogLog sparse representation bytes limit. The limit includes the
  732. # 16 bytes header. When an HyperLogLog using the sparse representation crosses
  733. # this limit, it is converted into the dense representation.
  734. #
  735. # A value greater than 16000 is totally useless, since at that point the
  736. # dense representation is more memory efficient.
  737. #
  738. # The suggested value is ~ 3000 in order to have the benefits of
  739. # the space efficient encoding without slowing down too much PFADD,
  740. # which is O(N) with the sparse encoding. The value can be raised to
  741. # ~ 10000 when CPU is not a concern, but space is, and the data set is
  742. # composed of many HyperLogLogs with cardinality in the 0 - 15000 range.
  743. # 关于HyperLogLog的介绍:http://www.redis.io/topics/data-types-intro#hyperloglogs
  744. # HyperLogLog稀疏表示限制设置,如果其值大于16000,则仍然采用稠密表示,因为这时稠密表示更能有效使用内存
  745. # 建议值为3000
  746. hll-sparse-max-bytes 3000
  747. # Active rehashing uses 1 millisecond every 100 milliseconds of CPU time in
  748. # order to help rehashing the main Redis hash table (the one mapping top-level
  749. # keys to values). The hash table implementation Redis uses (see dict.c)
  750. # performs a lazy rehashing: the more operation you run into a hash table
  751. # that is rehashing, the more rehashing "steps" are performed, so if the
  752. # server is idle the rehashing is never complete and some more memory is used
  753. # by the hash table.
  754. #
  755. # The default is to use this millisecond 10 times every second in order to
  756. # active rehashing the main dictionaries, freeing memory when possible.
  757. #
  758. # If unsure:
  759. # use "activerehashing no" if you have hard latency requirements and it is
  760. # not a good thing in your environment that Redis can reply form time to time
  761. # to queries with 2 milliseconds delay.
  762. #
  763. # use "activerehashing yes" if you don't have such hard requirements but
  764. # want to free memory asap when possible.
  765. # 每100毫秒,redis将用1毫秒的时间对Hash表进行重新Hash。
  766. # 采用懒惰Hash方式:操作Hash越多,则重新Hash的可能越多,若根本就不操作Hash,则不会重新Hash
  767. # 默认每秒10次重新hash主字典,释放可能释放的内存
  768. # 重新hash会造成延迟,如果对延迟要求较高,则设为no,禁止重新hash。但可能会浪费很多内存
  769. activerehashing yes
  770. # The client output buffer limits can be used to force disconnection of clients
  771. # that are not reading data from the server fast enough for some reason (a
  772. # common reason is that a Pub/Sub client can't consume messages as fast as the
  773. # publisher can produce them).
  774. #
  775. # The limit can be set differently for the three different classes of clients:
  776. #
  777. # normal -> normal clients including MONITOR clients
  778. # slave -> slave clients
  779. # pubsub -> clients subscribed to at least one pubsub channel or pattern
  780. #
  781. # The syntax of every client-output-buffer-limit directive is the following:
  782. #
  783. # 客户端输出缓冲区限制,当客户端从服务端的读取速度不够快时,则强制断开
  784. # 三种不同的客户端类型:normal、salve、pubsub,语法如下:
  785. # client-output-buffer-limit <class> <hard limit> <soft limit> <soft seconds>
  786. #
  787. # A client is immediately disconnected once the hard limit is reached, or if
  788. # the soft limit is reached and remains reached for the specified number of
  789. # seconds (continuously).
  790. # So for instance if the hard limit is 32 megabytes and the soft limit is
  791. # 16 megabytes / 10 seconds, the client will get disconnected immediately
  792. # if the size of the output buffers reach 32 megabytes, but will also get
  793. # disconnected if the client reaches 16 megabytes and continuously overcomes
  794. # the limit for 10 seconds.
  795. #
  796. # By default normal clients are not limited because they don't receive data
  797. # without asking (in a push way), but just after a request, so only
  798. # asynchronous clients may create a scenario where data is requested faster
  799. # than it can read.
  800. #
  801. # Instead there is a default limit for pubsub and slave clients, since
  802. # subscribers and slaves receive data in a push fashion.
  803. #
  804. # Both the hard or the soft limit can be disabled by setting them to zero.
  805. # 当达到硬限制,或者达到软限制且持续了算限制秒数,则立即与客户端断开
  806. # 限制设为0表示禁止该功能
  807. # 普通用户默认不限制
  808. client-output-buffer-limit normal 0 0 0
  809. client-output-buffer-limit slave 256mb 64mb 60
  810. client-output-buffer-limit pubsub 32mb 8mb 60
  811. # Redis calls an internal function to perform many background tasks, like
  812. # closing connections of clients in timeout, purging expired keys that are
  813. # never requested, and so forth.
  814. #
  815. # Not all tasks are performed with the same frequency, but Redis checks for
  816. # tasks to perform accordingly to the specified "hz" value.
  817. #
  818. # By default "hz" is set to 10. Raising the value will use more CPU when
  819. # Redis is idle, but at the same time will make Redis more responsive when
  820. # there are many keys expiring at the same time, and timeouts may be
  821. # handled with more precision.
  822. #
  823. # The range is between 1 and 500, however a value over 100 is usually not
  824. # a good idea. Most users should use the default of 10 and raise this up to
  825. # 100 only in environments where very low latency is required.
  826. # redis调用内部函数执行的后台任务的频率
  827. # 后台任务比如:清除过期数据、客户端超时链接等
  828. # 默认为10,取值范围1~500,
  829. # 对延迟要求很低的可以设置超过100以上
  830. hz 10
  831. # When a child rewrites the AOF file, if the following option is enabled
  832. # the file will be fsync-ed every 32 MB of data generated. This is useful
  833. # in order to commit the file to the disk more incrementally and avoid
  834. # big latency spikes.
  835. # 当修改AOF文件时,该设置为yes,则每生成32MB的数据,就进行同步
  836. aof-rewrite-incremental-fsync yes