Home  >  Article  >  Database  >  How to install Redis under Linux

How to install Redis under Linux

WBOY
WBOYforward
2023-05-27 19:42:421382browse

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

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

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

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

The final running result is as follows:

How to install Redis under Linux

Installation

Enter the following command

 make PREFIX=/usr/local/redis install

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

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

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 *

How to install Redis under Linux

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

How to install Redis under Linux

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

配置项名称 配置项值范围 说明
daemonize yes、no yes表示启用守护进程,默认是no即不以守护进程方式运行。其中Windows系统下不支持启用守护进程方式运行
port
指定 Redis 监听端口,默认端口为 6379
bind
绑定的主机地址,如果需要设置远程访问则直接将这个属性备注下或者改为bind * 即可,这个属性和下面的protected-mode控制了是否可以远程访问 。
protected-mode yes 、no 保护模式,该模式控制外部网是否可以连接redis服务,默认是yes,所以默认我们外网是无法访问的,如需外网连接rendis服务则需要将此属性改为no。
timeout 300 当客户端闲置多长时间后关闭连接,如果指定为 0,表示关闭该功能
loglevel debug、verbose、notice、warning 日志级别,默认为 notice
databases 16 设置数据库的数量,默认的数据库是0。整个通过客户端工具可以看得到
rdbcompression yes、no 指定存储至本地数据库时是否压缩数据,默认为 yes,Redis 采用 LZF 压缩,如果为了节省 CPU 时间,可以关闭该选项,但会导致数据库文件变的巨大。
dbfilename dump.rdb 指定本地数据库文件名,默认值为 dump.rdb
dir
指定本地数据库存放目录
requirepass
设置 Redis 连接密码,如果配置了连接密码,客户端在连接 Redis 时需要通过 AUTH  命令提供密码,默认关闭
maxclients 0 设置同一时间最大客户端连接数,默认无限制,Redis 可以同时打开的客户端连接数为 Redis 进程可以打开的最大文件描述符数,如果设置 maxclients 0,表示不作限制。当客户端连接数到达限制时,Redis 会关闭新的连接并向客户端返回 max number of clients reached 错误信息。
maxmemory XXX  指定 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

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

如下图:

How to install Redis under Linux

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

查看Redis是否正在运行

1、采取查看进程方式

 ps -aux | grep redis

结果如下图:

How to install Redis under Linux

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

 netstat -lanp | grep 6379

结果如下图:

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:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete