Table of Contents
Installation environment
Specific steps to install Redis under Linux
Download Redis
Extract and install Redis
Start redis
redis.conf配置文件
查看Redis是否正在运行
redis-cli
关闭运行中的Redis服务
远程连接不上问题
Home Database Redis How to install Redis under Linux

How to install Redis under Linux

May 27, 2023 pm 07:42 PM
linux redis

Redis is a high-performance key-value database. The emergence of redis has largely compensated for the shortcomings of keyvalue storage such as memcached, and can play a very good supplementary role to relational databases in some situations.

How to install Redis under Linux

Installation environment

  • Redis version 5.0.4

  • Server version Linux CentOS 7.6 64-bit

Specific steps to install Redis under Linux

Download Redis

Enter the official website to find the download address https://redis.io/download

How to install Redis under Linux

Right-click the Download button and select Copy Link. Enter the Xshell console (the default is the root directory), enter wget and paste the download link copied above, as follows:

 wget http:``//download.redis.io/releases/redis-5.0.7.tar.gz
Copy after login

After pressing the Enter key to execute, the following figure is shown:

How to install Redis under Linux

Wait for the download to complete.

Extract and install Redis

Decompression

After the download is complete, you need to decompress the compressed file. Enter the following command to decompress it to the current directory

 tar -zvxf redis-5.0.7.tar.gz
Copy after login

After decompression, enter ls in the root directory. List all directories and you will find that there is an additional redis-5.0.7.tar.gz file and redis-5.0.7 directory before downloading redis.

How to install Redis under Linux

Move the redis directory

Generally, the redis directory will be placed in the /usr/local/redis directory, so enter the following command here Change the directory of the redis-5.0.7 folder currently in the /root directory and change the folder name to redis.

 mv /root/redis-5.0.7 /usr/local/redis
Copy after login

cd Go to the /usr/local directory and enter the ls command to check that there is an additional redis subdirectory in the current directory, and there is no redis-5.0.7 folder in the /root directory

How to install Redis under Linux

Compile

cd to the /usr/local/redis directory, enter the command make to execute the compilation command, and then the console will output various compilation processes the output content.

 make
Copy after login

The final running result is as follows:

How to install Redis under Linux

Installation

Enter the following command

 make PREFIX=/usr/local/redis install
Copy after login

There is an additional keyword PREFIX= The function of this keyword is to specify the path where the program is stored during compilation. For example, we have now specified that redis must be stored in the /usr/local/redis directory. If you do not add this keyword, Linux will store the executable file in the /usr/local/bin directory, and the

library file will be stored in the /usr/local/lib directory. Configuration files will be stored in the /usr/local/etc directory. Other resource files will be stored in the usr/local/share directory. The directory number specified here also facilitates subsequent uninstallation. You can delete redis directly by rm -rf /usr/local/redis.

The execution result is as follows:

How to install Redis under Linux

Start redis

The redis installation has been completed according to the above operations. In the directory /usr/local/redis, enter the following command to start redis

?

 ./bin/redis-server& ./redis.conf
Copy after login

How to install Redis under Linux

The above startup method is to use the background process method, the following is to use the display method Startup mode (if the daemonize attribute is set to yes in the configuration file, it is actually the same as the background process startup).

?

 ./bin/redis-server ./redis.conf
Copy after login

The difference between the two methods is nothing more than the difference between the presence and absence of the signed &. redis-server is followed by a configuration file, the purpose is to start the redis service according to the configuration of the configuration file. The redis.conf configuration file allows you to customize multiple configuration files by specifying which one to read at startup.

redis.conf配置文件

在目录/usr/local/redis下有一个redis.conf的配置文件。我们上面启动方式就是执行了该配置文件的配置运行的。我么可以通过cat、vim、less等Linux内置的读取命令读取该文件。

也可以通过redis-cli命令进入redis控制台后通过CONFIG GET * 的方式读取所有配置项。 如下:

 redis-cli``CONFIG GET *
Copy after login

How to install Redis under Linux

回车确认后会将所有配置项读取出来,如下图

How to install Redis under Linux

这里列举下比较重要的配置项

配置项名称配置项值范围说明
daemonizeyes、noyes表示启用守护进程,默认是no即不以守护进程方式运行。其中Windows系统下不支持启用守护进程方式运行
port
指定 Redis 监听端口,默认端口为 6379
bind
绑定的主机地址,如果需要设置远程访问则直接将这个属性备注下或者改为bind * 即可,这个属性和下面的protected-mode控制了是否可以远程访问 。
protected-modeyes 、no保护模式,该模式控制外部网是否可以连接redis服务,默认是yes,所以默认我们外网是无法访问的,如需外网连接rendis服务则需要将此属性改为no。
timeout300当客户端闲置多长时间后关闭连接,如果指定为 0,表示关闭该功能
logleveldebug、verbose、notice、warning日志级别,默认为 notice
databases16设置数据库的数量,默认的数据库是0。整个通过客户端工具可以看得到
rdbcompressionyes、no指定存储至本地数据库时是否压缩数据,默认为 yes,Redis 采用 LZF 压缩,如果为了节省 CPU 时间,可以关闭该选项,但会导致数据库文件变的巨大。
dbfilenamedump.rdb指定本地数据库文件名,默认值为 dump.rdb
dir
指定本地数据库存放目录
requirepass
设置 Redis 连接密码,如果配置了连接密码,客户端在连接 Redis 时需要通过 AUTH 命令提供密码,默认关闭
maxclients0设置同一时间最大客户端连接数,默认无限制,Redis 可以同时打开的客户端连接数为 Redis 进程可以打开的最大文件描述符数,如果设置 maxclients 0,表示不作限制。当客户端连接数到达限制时,Redis 会关闭新的连接并向客户端返回 max number of clients reached 错误信息。
maxmemoryXXX 指定 Redis 最大内存限制,Redis 在启动时会把数据加载到内存中,达到最大内存后,Redis 会先尝试清除已到期或即将到期的 Key,当此方法处理 后,仍然到达最大内存设置,将无法再进行写入操作,但仍然可以进行读取操作。Redis 新的 vm 机制,会把 Key 存放内存,Value 会存放在 swap 区。配置项值范围列里XXX为数值。

显示详细信息

这里我要将daemonize改为yes,不然我每次启动都得在redis-server命令后面加符号&,不这样操作则只要回到Linux控制台则redis服务会自动关闭,同时也将bind注释,将protected-mode设置为no。 这样启动后我就可以在外网访问了。

更改方式:

 vim /usr/local/redis/redis.conf
Copy after login

通过 /daemonize 查找到属性,默认是no,更改为yes即可。 (通过/关键字查找出现多个结果则使用 n字符切换到下一个即可,查找到结果后输入:noh退回到正常模式)

如下图:

How to install Redis under Linux

其他两个属性也是同样方式查找和编辑即可。

查看Redis是否正在运行

1、采取查看进程方式

 ps -aux | grep redis
Copy after login

结果如下图:

How to install Redis under Linux

2、采取端口监听查看方式

 netstat -lanp | grep 6379
Copy after login

结果如下图:

How to install Redis under Linux

redis-cli

redis-cli是连接本地redis服务的一个命令,通过该命令后可以既然怒redis的脚本控制台。如下图

How to install Redis under Linux

输入exit可以退出redis脚本控制台

关闭运行中的Redis服务

输入redis-cli 进入控制台后输入命令shutdown即可关闭运行中的Redis服务了。如下图:

How to install Redis under Linux

远程连接不上问题

如下图,已经开放了Redis服务的ip不为127.0.0.1,理论上远程客户端应该可以连接了,而且云服务器的端口号也在安全组里开放了。

How to install Redis under Linux

后面发现是启动命令的问题,因为我比较偷懒,启动redis我都是直接输入命令 redis-server 或 redis-server& 这两种方式都是直接读取默认的配置文件启动,无非前者是显示启动后者是作为后台应用启动。我其实也很纳闷,因为我修改的就是默认的配置文件啊,我并没有重新生成新的配置文件,但是确实我输入命令 redis-server /usr/local/redis/etc/redis.conf 就是能成功,而且我输入命令redis-server& /usr/local/redis/etc/redis.conf也是远程登录失败。 关于直接输入redis-server不行的问题我还怀疑是不是Linux缓存问题,我重启服务器尝试下。结果还是一样的。。。哎先不纠结了 后续再去找原因吧

How to install Redis under Linux

The above is the detailed content of How to install Redis under Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Laravel8 optimization points Laravel8 optimization points Apr 18, 2025 pm 12:24 PM

Laravel 8 provides the following options for performance optimization: Cache configuration: Use Redis to cache drivers, cache facades, cache views, and page snippets. Database optimization: establish indexing, use query scope, and use Eloquent relationships. JavaScript and CSS optimization: Use version control, merge and shrink assets, use CDN. Code optimization: Use Composer installation package, use Laravel helper functions, and follow PSR standards. Monitoring and analysis: Use Laravel Scout, use Telescope, monitor application metrics.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

Redis's Role: Exploring the Data Storage and Management Capabilities Redis's Role: Exploring the Data Storage and Management Capabilities Apr 22, 2025 am 12:10 AM

Redis plays a key role in data storage and management, and has become the core of modern applications through its multiple data structures and persistence mechanisms. 1) Redis supports data structures such as strings, lists, collections, ordered collections and hash tables, and is suitable for cache and complex business logic. 2) Through two persistence methods, RDB and AOF, Redis ensures reliable storage and rapid recovery of data.

What should I do if the Redis cache of OAuth2Authorization object fails in Spring Boot? What should I do if the Redis cache of OAuth2Authorization object fails in Spring Boot? Apr 19, 2025 pm 08:03 PM

In SpringBoot, use Redis to cache OAuth2Authorization object. In SpringBoot application, use SpringSecurityOAuth2AuthorizationServer...

How to set important Git configuration global properties How to set important Git configuration global properties Apr 17, 2025 pm 12:21 PM

There are many ways to customize a development environment, but the global Git configuration file is one that is most likely to be used for custom settings such as usernames, emails, preferred text editors, and remote branches. Here are the key things you need to know about global Git configuration files.

See all articles