Redis series (1)-Redis environment construction under Linux system

Summary of blog posts

First, Redis first acquaintance

Two, Redis installation

Three, Redis configuration

Four, Redis uninstall

Five, Redis commonly used commands

Six, Redis configuration parameter attachment


At the beginning of this chapter, the author introduces a series of articles about redis technology. First, I will briefly introduce the installation, configuration, deletion, and application of redis under the linux system.

Reference documents

1. Redis Chinese official website

2. W3CSchool Chinese document


First, Redis first acquaintance

Redis is an open source log-based, Key-Value database written in ANSI C language, supporting the network, memory-based or persistent, and providing APIs in multiple languages. It supports rich data types, and high-speed memory read and write. Is gradually replacing the status of memcached. Redis is an open source (BSD licensed), in-memory data structure storage system, which can be used as a database, cache, and messaging middleware.

The following is a demonstration of the redis5.x installation and configuration process under centos7.x64.


Two, Redis installation

1. Create the software source file path

# 切换到根目录
cd /

# 创建目录
mkdir software
cd softwaer

2. Redis source file download

wget https://download.redis.io/releases/redis-5.0.7.tar.gz

3. Unzip the file

tar xzf redis-5.0.7.tar.gz

4. Install gcc (if it exists in the system, it can be omitted)

yum install gcc-c++

5. Redis compile, compile source code according to Makefile, link, generate target file, executable file.

make

6, redis installation

# 根据PREFIX,设置安装路径 
make install PREFIX=/software/redis

7, redis test

# 切换目录
/software/redis/bin

# 启动redis服务
./redis-server 

After starting the redis-server service, the following interface is displayed, indicating that the redis installation is successful

As shown in the figure:


Three, Redis configuration

Simply install redis, but according to the above one pass operation, the default operation is extremely inconvenient in future use, you can do the configuration

1. Modify the system configuration file profile

# 打开系统编辑文件
vim /etc/profile

# 在/etc/profile文件中,添加配置信息
# -----------------------------------------------------------------
# redis  environment setting
# -----------------------------------------------------------------
export  REDIS_HOME=/software/redis
export PATH=$PATH:$REDIS_HOME/bin

# 配置信息添加后,重新读取并执行系统配置文件
source /etc/profile

As shown in the figure: 

 2. Redis background startup

In the redis.conf configuration file. Make the following changes:

daemonize no
# 修改为
daemonize yes

3. Configure service to start redis

Usually when starting the redis service, you need to switch to the redis installation bin directory./redis-server mode, it is extremely inconvenient to start, you can switch to the service redis xxx mode

3.1. Set according to the above [redis background start]

3.2. Copy the redis configuration file (the startup script needs to use the configuration file content)

# 1、在/etc下新建redis配置文件夹
mkdir /etc/redis

# 2、把安装redis目录里面的redis.conf文件复制到/etc/redis/6379.conf里面,6379.conf是取的文件名称,启动脚本里面的变量会读取这个名称
# redis的端口号改了,这里也要修改
cp /software/redis/redis.conf /etc/redis/6379.conf

 3.3, copy the redis startup script

# 1.redis启动脚本一般在redis根目录的utils,如果不知道路径,可以先查看路径
find / -name redis_init_script # 结果 /software/redis/utils/redis_init_script

# 2.复制启动脚本到/etc/init.d/redis文件中
cp /software/redis/utils/redis_init_script /etc/init.d/redis

3.4, modify the startup script parameters

# 修改启动配置文件
vim /etc/init.d/redis

#在/etc/init.d/redis文件的头部添加下面两行注释代码,也就是在文件中#!/bin/sh的下方添加
# chkconfig: 2345 10 90  
# description: Start and Stop redis 

 As shown in the figure:

# 修改参数,指定redis的安装路径
REDISPORT=6379
EXEC=/software/redis/src/redis-server
CLIEXEC=/software/redis/src/redis-cli

 As shown in the figure:

3.5, empower redis

# 777为最高权限,读、写、执行等
chmod 777 /etc/init.d/redis

Four, Redis uninstall

To be perfected


Five, Redis commonly used commands

After the configuration is complete (execute [3, Redis configuration]), you can use service to perform redis operations. Common commands are as follows

# 打开redis命令
service redis start

# 关闭redis命令
service redis stop

# 设为开机启动
chkconfig redis on

# 设为开机关闭
chkconfig redis off

Redis startup example, as shown in the figure:


Six, Redis configuration parameter attachment

Redis.conf main configuration instructions:

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
means 1 change within 900 seconds (15 minutes), 10 changes within 300 seconds (5 minutes), and 60 seconds. 10,000 changes.

 

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 to you ./ 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实例之间使用同一份配置文件,而同时各个实例又拥有自己的特定配置文件

综合上述,redis基本操作已经完成,如有不足的地方,欢迎大家留言指正!

 

--------------------------------------------------------------------------------------
作者:超级字节码
来源:CSDN
原文:https://blog.csdn.net/dgxin_605/article/details/114134999
版权声明:本文为博主原创文章,转载请附上博文链接!

--------------------------------------------------------------------------------------
 

 

Guess you like

Origin blog.csdn.net/dgxin_605/article/details/114134999