Redis常用配置解析

默认情况下,Redis的配置文件存放在redis主目录下,文件名为redis.conf,通过此文件,可窥探一二

Redis常用配置解析

1、绑定IP—bind

默认为本机:127.0.0.1,配置如下:

# Examples:
#
# bind 192.168.1.100 10.0.0.1     # listens on two specific IPv4 addresses
# bind 127.0.0.1 ::1              # listens on loopback IPv4 and IPv6
# bind * -::*                     # like the default, all available interfaces
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only on the
# IPv4 and IPv6 (if available) loopback interface addresses (this means Redis
# will only be able to accept client connections from the same host that it is
# running on).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT OUT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1 -::1

2、端口号—port

默认端口号为:6379,配置如下:

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

3、连接密码—requirepass

默认情况下,redis连接不需要密码,此配置项是注释状态,可解除注释以开启连接密码验证,配置如下:

# IMPORTANT NOTE: starting with Redis 6 "requirepass" is just a compatibility
# layer on top of the new ACL system. The option effect will be just setting
# the password for the default user. Clients will still authenticate using
# AUTH <password> as usually, or more explicitly with AUTH default <password>
# if they follow the new protocol: both will work.
#
# The requirepass is not compatable with aclfile option and the ACL LOAD
# command, these will cause requirepass to be ignored.
#
# requirepass foobared
requirepass 配置上连接密码

使用命令redis-server 配置文件 启动redis服务后,发现redis-cli可以正常连接服务,但是进行操作时将报错:(error) NOAUTH Authentication required.。此时,须使用命令 auth 密码

Redis常用配置解析

使用RDM则需要配置密码方可连接

Redis常用配置解析

同样使用Jedis时,需要先通过如下代码,指定连接密码:

// 直接连接时
jedis.auth(密码);
// 使用连接池时
public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, int port, int timeout, final String password)

4、守护进程—daemonize

Redis默认不是以守护进程的方式运行的,当需要开启时,将此配置注释或设置为yes即可,配置如下:

# By default Redis does not run as a daemon. Use ‘yes‘ if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
# When Redis is supervised by upstart or systemd, this parameter has no impact.
daemonize no

注:Windows下,不支持此配置为 no

5、pid文件—pidfile

通过配置文件中# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.的说明可知,当redis以守护进程方式运行时,会生成一个记录pid的文件/var/run/redis.pid,可通过pidfile自定义:

# Note that on modern Linux systems "/run/redis.pid" is more conforming
# and should be used instead.
pidfile /var/run/redis_6379.pid

6、超时时间—timeout

当一个客户端连接闲置多少秒之后将其关闭,默认配置为0,配置为0时表示不启用:

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

7、数据库数量—databases

配置数据库数量,默认是16个,编号为0~15:

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

客户端连接上之后,可以使用select n命令来指定使用n号库。

Redis常用配置解析

8、持久化—rdb

8.1、持久化策略—save

在多长的时间内有多少key改变时,将内存中的数据保存到磁盘数据文件中,默认关闭,可同时配置多个。配置如下:

# Unless specified otherwise, by default Redis will save the DB:
#   * After 3600 seconds (an hour) if at least 1 key changed
#   * After 300 seconds (5 minutes) if at least 100 keys changed
#   * After 60 seconds if at least 10000 keys changed
#
# You can set these explicitly by uncommenting the three following lines.
#

# save 3600 1	# 3600秒(1小时)内有一个key改变
# save 300 100
# save 60 10000

8.2、rdb数据文件名—dbfilename

指定本地数据文件名称,默认为dump.rdb,这是一个二进制文件。配置如下:

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

8.3、是否压缩数据—rdbcompression

指定数据保存到本地文件时是否压缩,Redis 采用 LZF 压缩,如果为了节省 CPU 时间,可以关闭该选项,但会导致数据库文件变大。默认压缩,配置如下:

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

8.4、持久化文件存储路径—dir

指定rdb本地文件的存储路径,默认为当前目录,配置如下:

# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the ‘dbfilename‘ configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir ./

9、持久化—aof

9.1、是否开启—appendonly

指定是否开启aof持久化,默认关闭的(no),但是官方推介开启(配置为yes)。配置如下:

# By default Redis asynchronously dumps the dataset on disk. This mode is
# good enough in many applications, but an issue with the Redis process or
# a power outage may result into a few minutes of writes lost (depending on
# the configured save points).
#
# The Append Only File is an alternative persistence mode that provides
# much better durability. For instance using the default data fsync policy
# (see later in the config file) Redis can lose just one second of writes in a
# dramatic event like a server power outage, or a single write if something
# wrong with the Redis process itself happens, but the operating system is
# still running correctly.
#
# AOF and RDB persistence can be enabled at the same time without problems.
# If the AOF is enabled on startup Redis will load the AOF, that is the file
# with the better durability guarantees.
#
# Please check https://redis.io/topics/persistence for more information.

appendonly no

9.2、持久化策略—appendfsync

与rdb不同的是,aof一共有三个选项共选择:

  • no:表示等操作系统进行数据缓存同步到磁盘(快)
  • always:表示每次更新操作后手动调用 fsync() 将数据写到磁盘(慢,安全)
  • everysec:表示每秒同步一次(折中,默认值)

配置如下:

# If unsure, use "everysec".

# appendfsync always
appendfsync everysec
# appendfsync no

9.3、aof持久化文件名—appendfilename

指定aof的文件名,默认值为appendonly.aof,这是一个文本文件,配置如下:

# The name of the append only file (default: "appendonly.aof")

appendfilename "appendonly.aof"

10、最大连接数—maxclients

设置同一时间最大客户端连接数,默认无限制,Redis 可以同时打开的客户端连接数为 Redis 进程可以打开的最大文件描述符数,如果设置 maxclients 0,表示不作限制。当客户端连接数到达限制时,Redis 会关闭新的连接并向客户端返回 max number of clients reached 错误信息。Once the limit is reached Redis will close all the new connections sending an error ‘max number of clients reached‘

# Set the max number of connected clients at the same time. By default
# this limit is set to 10000 clients, however if the Redis server is not
# able to configure the process file limit to allow for the specified limit
# the max number of allowed clients is set to the current file limit
# minus 32 (as Redis reserves a few file descriptors for internal uses).
#
# Once the limit is reached Redis will close all the new connections sending
# an error ‘max number of clients reached‘.
#
# IMPORTANT: When Redis Cluster is used, the max number of connections is also
# shared with the cluster bus: every node in the cluster will use two
# connections, one incoming and another outgoing. It is important to size the
# limit accordingly in case of very large clusters.
#
# maxclients 10000

11、最大内存容量—maxmemory

指定 Redis 最大内存限制,默认不指定(即系统内存剩余),Redis 在启动时会把数据加载到内存中,达到最大内存后,Redis 会先尝试清除已到期或即将到期的 Key,当此方法处理 后,仍然到达最大内存设置,将无法再进行写入操作,但仍然可以进行读取操作。Redis 新的 vm 机制,会把 Key 存放内存,Value 会存放在 swap 区。

# Set a memory usage limit to the specified amount of bytes.
# When the memory limit is reached Redis will try to remove keys
# according to the eviction policy selected (see maxmemory-policy).
#
# If Redis can‘t remove keys according to the policy, or if the policy is
# set to ‘noeviction‘, Redis will start to reply with errors to commands
# that would use more memory, like SET, LPUSH, and so on, and will continue
# to reply to read-only commands like GET.
#
# This option is usually useful when using Redis as an LRU or LFU cache, or to
# set a hard memory limit for an instance (using the ‘noeviction‘ policy).
#
# WARNING: If you have replicas attached to an instance with maxmemory on,
# the size of the output buffers needed to feed the replicas are subtracted
# from the used memory count, so that network problems / resyncs will
# not trigger a loop where keys are evicted, and in turn the output
# buffer of replicas is full with DELs of keys evicted triggering the deletion
# of more keys, and so forth until the database is completely emptied.
#
# In short... if you have replicas attached it is suggested that you set a lower
# limit for maxmemory so that there is some free RAM on the system for replica
# output buffers (but this is not needed if the policy is ‘noeviction‘).
#
# maxmemory <bytes>

Redis常用配置解析

上一篇:Application 简介 常用方式


下一篇:在Springboot 中使用 flyway