redis: source installation and configuration

1. Source code compilation and installation

Get the source code and make: 

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

tar xzf redis-6.2.0.tar.gz

cd redis-6.2.0/

make

make test

sudo make install

During the execution of make test, pkg-config and ttl packages may be missing, and the installation can continue.

After executing the make command, the compiled redis service program redis-server and the client program redis-cli for testing will appear in the src directory of redis-6.0.8:

Start the redis service below:

cd src

./redis-server

Note that starting redis in this way uses the default configuration. You can also tell redis to use the specified configuration file to start with the following command through the startup parameters.

cd src
./redis-server ../redis.conf

After starting the redis service process, you can use the test client program redis-cli to interact with the redis service. such as:

At this point, the installation of redis and a simple test have been completed.

If you need to execute commands on the remote redis service, you also need to use the redis-cli command

redis-cli -h host -p port -a password

2. Redis configuration

The Redis configuration file is located in the Redis installation directory and the file name is redis.conf.

The above picture is a small part of the screenshot of the default configuration of redis. The configuration file of redis is also in the form of key-value.

We can view or set configuration items through the config command.

The redis.conf configuration items are described as follows:

Serial number Configuration item Description
1
daemonize no
Redis does not run as a daemon by default. You can modify this configuration item to enable the daemon with yes (Windows does not support the configuration of daemon threads as no)
2
pidfile /var/run/redis.pid
When Redis is running as a daemon, Redis will write the pid to the /var/run/redis.pid file by default, which can be specified by pidfile
3
port 6379
Specify the Redis listening port. The default port is 6379. The author explained in a blog post why 6379 was selected as the default port, because 6379 is the number corresponding to MERZ on the phone button, and MERZ is taken from the name of the Italian singer Alessia Merz
4
bind 127.0.0.1
Bound host address
5
timeout 300
How long to close the connection after the client is idle, if it is specified as 0, it means to close the function
6
loglevel notice
Specify the logging level, Redis supports a total of four levels: debug, verbose, notice, warning, the default is notice
7
logfile stdout
The logging mode is standard output by default. If Redis is configured to run as a daemon and the logging mode is configured as standard output here, the log will be sent to /dev/null
8
databases 16
Set the number of databases, the default database is 0, you can use the SELECT command to specify the database id on the connection
9
save <seconds> <changes>

Three conditions are provided in the Redis default configuration file:

save 900 1

save 300 10

save 60 10000

It means that there are 1 change in 900 seconds (15 minutes), 10 changes in 300 seconds (5 minutes), and 10,000 changes in 60 seconds.

Specify how many update operations within a period of time, the data will be synchronized to the data file, multiple conditions can be matched
10
rdbcompression yes
Specify whether to compress data when storing to a local database. The default is yes. Redis uses LZF compression. If you want to save CPU time, you can turn off this option, but the database file will become huge
11
dbfilename dump.rdb
Specify the local database file name, the default value is dump.rdb
12
dir ./
Specify the local database storage directory
13
slaveof <masterip> <masterport>
Set when this machine is the slave service, set the IP address and port of the master service, when Redis starts, it will automatically synchronize data from the master
14
masterauth <master-password>
When the master service is password protected, the slav service connects to the master password
15
requirepass foobared
Set the Redis connection password. If the connection password is configured, the client needs to provide the password through the AUTH <password> command when connecting to Redis, which is closed by default
16
 maxclients 128
Set the maximum number of client connections at the same time. The default is unlimited. The number of client connections that Redis can open at the same time is the maximum number of file descriptors that can be opened by the Redis process. If you set maxclients 0, it means that there is no limit. When the number of client connections reaches the limit, Redis will close the new connection and return a max number of clients reached error message to the client
17
maxmemory <bytes>
指定 Redis 最大内存限制,Redis 在启动时会把数据加载到内存中,达到最大内存后,Redis 会先尝试清除已到期或即将到期的 Key,当此方法处理 后,仍然到达最大内存设置,将无法再进行写入操作,但仍然可以进行读取操作。Redis 新的 vm 机制,会把 Key 存放内存,Value 会存放在 swap 区
18
appendonly no
指定是否在每次更新操作后进行日志记录,Redis 在默认情况下是异步的把数据写入磁盘,如果不开启,可能会在断电时导致一段时间内的数据丢失。因为 redis 本身同步数据文件是按上面 save 条件来同步的,所以有的数据会在一段时间内只存在于内存中。默认为 no
19
appendfilename appendonly.aof
指定更新日志文件名,默认为 appendonly.aof
20
appendfsync everysec

指定更新日志条件,共有 3 个可选值:

  • no:表示等操作系统进行数据缓存同步到磁盘(快)
  • always:表示每次更新操作后手动调用 fsync() 将数据写到磁盘(慢,安全)
  • everysec:表示每秒同步一次(折中,默认值)
21
vm-enabled no
指定是否启用虚拟内存机制,默认值为 no,简单的介绍一下,VM 机制将数据分页存放,由 Redis 将访问量较少的页即冷数据 swap 到磁盘上,访问多的页面由磁盘自动换出到内存中(在后面的文章我会仔细分析 Redis 的 VM 机制)
22
vm-swap-file /tmp/redis.swap
虚拟内存文件路径,默认值为 /tmp/redis.swap,不可多个 Redis 实例共享
23
vm-max-memory 0
将所有大于 vm-max-memory 的数据存入虚拟内存,无论 vm-max-memory 设置多小,所有索引数据都是内存存储的(Redis 的索引数据 就是 keys),也就是说,当 vm-max-memory 设置为 0 的时候,其实是所有 value 都存在于磁盘。默认值为 0
24
vm-page-size 32
Redis swap 文件分成了很多的 page,一个对象可以保存在多个 page 上面,但一个 page 上不能被多个对象共享,vm-page-size 是要根据存储的 数据大小来设定的,作者建议如果存储很多小对象,page 大小最好设置为 32 或者 64bytes;如果存储很大大对象,则可以使用更大的 page,如果不确定,就使用默认值
25
vm-pages 134217728
设置 swap 文件中的 page 数量,由于页表(一种表示页面空闲或使用的 bitmap)是在放在内存中的,,在磁盘上每 8 个 pages 将消耗 1byte 的内存。
26
vm-max-threads 4
设置访问swap文件的线程数,最好不要超过机器的核数,如果设置为0,那么所有对swap文件的操作都是串行的,可能会造成比较长时间的延迟。默认值为4
27
glueoutputbuf yes
设置在向客户端应答时,是否把较小的包合并为一个包发送,默认为开启
28
hash-max-zipmap-entries 64
hash-max-zipmap-value 512
指定在超过一定的数量或者最大的元素超过某一临界值时,采用一种特殊的哈希算法
29
activerehashing yes
指定是否激活重置哈希,默认为开启(后面在介绍 Redis 的哈希算法时具体介绍)
30
include /path/to/local.conf
指定包含其它的配置文件,可以在同一主机上多个Redis实例之间使用同一份配置文件,而同时各个实例又拥有自己的特定配置文件

Guess you like

Origin blog.csdn.net/weixin_40179091/article/details/114866607