redis的安装配置

模板网 2021-04-14

先介绍mac的安装方法

一、安装(首先你要先安装brew工具)

brew install redis

然后在终端的末尾你会发现如下:

if you don't want/need a background service you can just run:
  redis-server /usr/local/etc/redis.conf

貌似redis不会自动加载配置文件,这句话就是提示你,你可以指定redis读取的配置文件 redis默认是不会后台运行的,所以当你指定这个配置文件时候,就要去里面找一下daemonize将其改为yes

daemonize yes

然后就可以运行

redis-server /usr/local/etc/redis.conf

这样你的redis就完美的运行起来了。

二、redis的配置

上面只介绍了redis的一个后台运行的配置,下面将详细分析redis的配置文件

################################## INCLUDES ###################################

# Include one or more other config files here.  This is useful if you
# have a standard template that goes to all Redis servers but also need
# to customize a few per-server settings.  Include files can include
# other files, so use this wisely.
#
# Notice option "include" won't be rewritten by command "CONFIG REWRITE"
# from admin or Redis Sentinel. Since Redis always uses the last processed
# line as value of a configuration directive, you'd better put includes
# at the beginning of this file to avoid overwriting config change at runtime.
#
# If instead you are interested in using includes to override configuration
# options, it is better to use include as the last line.
#
# include /path/to/local.conf
# include /path/to/other.conf
#有一个标准的模板,适用于所有Redis服务器,但也需要为每个服务器定制一些设置。可以包含配置文件

################################## MODULES #####################################

# Load modules at startup. If the server is not able to load modules
# it will abort. It is possible to use multiple loadmodule directives.
#
# loadmodule /path/to/my_module.so
# loadmodule /path/to/other_module.so

################################## NETWORK #####################################

# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ 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 into
# the IPv4 loopback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# bind 127.0.0.1
bind 127.0.0.1 ::1    #绑定的主机地址,你可以绑定单一接口,如果没有绑定,所有接口都会监听到来的连接

# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
#    "bind" directive.
# 2) No password is configured.
#
# The server only accepts connections from clients connecting from the
# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
# sockets.
#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode yes    #保护模式,只允许本机连接

# 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    #指定Redis监听端口,默认端口为6379,如果指定0端口,表示Redis不监听TCP连接

# TCP listen() backlog.
#
# In high requests-per-second environments you need an high backlog in order
# to avoid slow clients connections issues. Note that the Linux kernel
# will silently truncate it to the value of /proc/sys/net/core/somaxconn so
# make sure to raise both the value of somaxconn and tcp_max_syn_backlog
# in order to get the desired effect.
tcp-backlog 511

# Unix socket.
#
# Specify the path for the Unix socket that will be used to listen for
# incoming connections. There is no default, so Redis will not listen
# on a unix socket when not specified.
#
# unixsocket /tmp/redis.sock
# unixsocketperm 700

# Close the connection after a client is idle for N seconds (0 to disable)
timeout 0    #当客户端闲置多长时间后关闭连接,如果指定为0,表示关闭该功能

# TCP keepalive.
#
# If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence
# of communication. This is useful for two reasons:
#
# 1) Detect dead peers.
# 2) Take the connection alive from the point of view of network
#    equipment in the middle.
#
# On Linux, the specified value (in seconds) is the period used to send ACKs.
# Note that to close the connection the double of the time is needed.
# On other kernels the period depends on the kernel configuration.
#
# A reasonable value for this option is 300 seconds, which is the new
# Redis default starting with Redis 3.2.1.
tcp-keepalive 300

################################# GENERAL #####################################

# 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.

daemonize yes    #Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程
                 #启用守护进程后,Redis会把pid写到一个pidfile中

# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
#   supervised no      - no supervision interaction
#   supervised upstart - signal upstart by putting Redis into SIGSTOP mode
#   supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
#   supervised auto    - detect upstart or systemd method based on
#                        UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
#       They do not enable continuous liveness pings back to your supervisor.
supervised no

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

# Specify the server verbosity level.
# This can be one of:
# debug (a lot of information, useful for development/testing)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably)
# warning (only very important / critical messages are logged)
loglevel notice    #指定日志记录级别,Redis总共支持四个级别:debug、verbose、notice、warning

# Specify the log file name. Also the empty string can be used to force
# Redis to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile ""    #日志记录方式,默认为标准输出,如果配置为redis为守护进程方式运行,而这里又配置为标准输出,则日志将会发送给/dev/null

# To enable logging to the system logger, just set 'syslog-enabled' to yes,
# and optionally update the other syslog parameters to suit your needs.
# syslog-enabled no

# Specify the syslog identity.
# syslog-ident redis

# Specify the syslog facility. Must be USER or between LOCAL0-LOCAL7.
# syslog-facility local0

# 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    #设置数据库的数量

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

################################ SNAPSHOTTING  ################################
#
# Save the DB on disk:
#
#   save <seconds> <changes>
#
#   Will save the DB if both the given number of seconds and the given
#   number of write operations against the DB occurred.
#
#   In the example below the behaviour will be to save:
#   after 900 sec (15 min) if at least 1 key changed
#   after 300 sec (5 min) if at least 10 keys changed
#   after 60 sec if at least 10000 keys changed
#
#   Note: you can disable saving completely by commenting out all "save" lines.
#
#   It is also possible to remove all the previously configured save
#   points by adding a save directive with a single empty string argument
#   like in the following example:
#
#   save ""

save 900 1    #900秒(15分钟)内有1个更改
save 300 10    #300秒(5分钟)内有10个更改
save 60 10000    #60秒内有10000个更改
#指定在多长时间内,有多少次更新操作,就将数据同步到数据文件,可以多个条件配合
#满足以上条件将会同步数据:

# By default Redis will stop accepting writes if RDB snapshots are enabled
# (at least one save point) and the latest background save failed.
# This will make the user aware (in a hard way) that data is not persisting
# on disk properly, otherwise chances are that no one will notice and some
# disaster will happen.
#
# If the background saving process will start working again Redis will
# automatically allow writes again.
#
# However if you have setup your proper monitoring of the Redis server
# and persistence, you may want to disable this feature so that Redis will
# continue to work as usual even if there are problems with disk,
# permissions, and so forth.
stop-writes-on-bgsave-error yes

# Compress string objects using LZF when dump .rdb databases?
# For default that's set to 'yes' 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    #指定存储至本地数据库时是否压缩数据,默认为yes,Redis采用LZF压缩,
                      #如果为了节省CPU时间,可以关闭该选项,但会导致数据库文件变的巨大

# Since version 5 of RDB a CRC64 checksum is placed at the end of the file.
# This makes the format more resistant to corruption but there is a performance
# hit to pay (around 10%) when saving and loading RDB files, so you can disable it
# for maximum performances.
#
# RDB files created with checksum disabled have a checksum of zero that will
# tell the loading code to skip the check.
rdbchecksum yes

# The filename where to dump the DB
dbfilename dump.rdb    #指定本地数据库文件名,默认值为dump.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 ./
dir /usr/local/var/db/redis/    #指定本地数据库存放目录,文件名由上一个dbfilename配置项指定
                                #注意,这里只能指定一个目录,不能指定文件名

################################# REPLICATION #################################
主从复制。使用slaveof从 Redis服务器复制一个Redis实例。注意,该配置仅限于当前slave有效
设置当本机为slav服务时,设置master服务的ip地址及端口,在Redis启动时,它会自动从master进行数据同步
# slaveof <masterip> <masterport>

# 当master服务设置了密码保护时,slav服务连接master的密码
# masterauth <master-password>

slave-serve-stale-data yes

slave-read-only yes    #从节点只读

repl-diskless-sync no

repl-diskless-sync-delay 5

# repl-ping-slave-period 10

# repl-timeout 60

repl-disable-tcp-nodelay no

# repl-backlog-size 1mb

# repl-backlog-ttl 3600

slave-priority 100

# min-slaves-to-write 3
# min-slaves-max-lag 10

################################## SECURITY ###################################
设置Redis连接密码,如果配置了连接密码,客户端在连接Redis时需要通过auth <password>命令提供密码,默认关闭

# requirepass foobared



################################### CLIENTS ####################################
# 设置同一时间最大客户端连接数,默认无限制,Redis可以同时打开的客户端连接数为Redis进程可以打开的最大文件描述符数,
# 如果设置maxclients 0,表示不作限制。当客户端连接数到达限制时,Redis会关闭新的连接并向客户端返回max Number of clients reached错误信息
# maxclients 10000



############################## MEMORY MANAGEMENT ################################
# 指定Redis最大内存限制,Redis在启动时会把数据加载到内存中,达到最大内存后,Redis会先尝试清除已到期或即将到期的Key
# 当此方法处理后,仍然到达最大内存设置,将无法再进行写入操作,但仍然可以进行读取操作。
# Redis新的vm机制,会把Key存放内存,Value会存放在swap区
# maxmemory <bytes>
# maxmemory-policy noeviction
# maxmemory-samples 5



############################# LAZY FREEING ####################################
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no



############################## APPEND ONLY MODE ###############################
# 指定是否在每次更新操作后进行日志记录,Redis在默认情况下是异步的把数据写入磁盘,如果不开启,可能会在断电时导致一段时间内的数据丢失。
# 因为redis本身同步数据文件是按上面save条件来同步的,所以有的数据会在一段时间内只存在于内存中。
appendonly no    #是否开启
appendfilename "appendonly.aof"    #指定更新日志文件名
# appendfsync always    # 指定更新日志条件共有3个可选值
                        # no:表示等操作系统进行数据缓存同步到磁盘(快)
                        # always:表示每次更新操作后手动调用fsync()将数据写到磁盘(慢,安全)
                        # everysec:表示每秒同步一次(折衷,默认值)
appendfsync everysec
# appendfsync no
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no




################################ LUA SCRIPTING  ###############################
lua-time-limit 5000




################################ REDIS CLUSTER  ###############################
# cluster-enabled yes
# cluster-config-file nodes-6379.conf
# cluster-node-timeout 15000
# cluster-slave-validity-factor 10
# cluster-migration-barrier 1
# cluster-require-full-coverage yes
# cluster-slave-no-failover no



########################## CLUSTER DOCKER/NAT support  ########################
# cluster-announce-ip 10.1.1.5
# cluster-announce-port 6379
# cluster-announce-bus-port 6380



################################## SLOW LOG ###################################
slowlog-log-slower-than 10000
slowlog-max-len 128




################################ LATENCY MONITOR ##############################
latency-monitor-threshold 0





############################# EVENT NOTIFICATION ##############################
notify-keyspace-events ""





############################### ADVANCED CONFIG ###############################
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
# client-query-buffer-limit 1gb
# proto-max-bulk-len 512mb
hz 10
aof-rewrite-incremental-fsync yes

相关文章

  1. rdb快照持久化

    一、持久化的方式 什么是持久化:redis所有数据保存在内存中,对数据的更新将异步保存到磁盘上。 持久化:即把数据存储于断电后不会丢失的设备中,通常是硬盘。 常见的持久化方式:主从:通过从服务器保存

  2. 常见排序列表

    常见排序列表 中文名称 英文名称 平均时间复杂度 最坏时间复杂度 最好时间复杂度 空间复杂度 稳定性 选择排序 Selection n^2 n^2 n^2 1 不稳 冒泡排序 Bub

  3. Sublime Text 快捷键汇总

    [TOC] 快捷键 作用 command+q 退出sublime command+w 退出当前正在编辑的文件 command+e 使用所选内容查找 command+r 跳转定

  4. php配置加载器

    配置加载器 当我们使用App::cfg系列方法获取配置时,wulaphp是通过配置加载器先加载配置然后再返回配置项对应值的(当然可以返回整个配置数组)。配置加载器ConfigurationLoade

  5. CentOS编译安装PHP7.1.21环境

    0.安装一大堆必备的东西 先执行 yum install -y epel-release yum install -y libmcrypt-devel 再执行 yum -y install gcc

随机推荐

  1. rdb快照持久化

    一、持久化的方式 什么是持久化:redis所有数据保存在内存中,对数据的更新将异步保存到磁盘上。 持久化:即把数据存储于断电后不会丢失的设备中,通常是硬盘。 常见的持久化方式:主从:通过从服务器保存

  2. 常见排序列表

    常见排序列表 中文名称 英文名称 平均时间复杂度 最坏时间复杂度 最好时间复杂度 空间复杂度 稳定性 选择排序 Selection n^2 n^2 n^2 1 不稳 冒泡排序 Bub

  3. Sublime Text 快捷键汇总

    [TOC] 快捷键 作用 command+q 退出sublime command+w 退出当前正在编辑的文件 command+e 使用所选内容查找 command+r 跳转定

  4. php配置加载器

    配置加载器 当我们使用App::cfg系列方法获取配置时,wulaphp是通过配置加载器先加载配置然后再返回配置项对应值的(当然可以返回整个配置数组)。配置加载器ConfigurationLoade

  5. CentOS编译安装PHP7.1.21环境

    0.安装一大堆必备的东西 先执行 yum install -y epel-release yum install -y libmcrypt-devel 再执行 yum -y install gcc