Redis(二):初始篇-Linux安装Redis

“纸上得来终觉浅,绝知此事要躬行”      --陆游《冬夜读书示子聿》

版本规则

  Redis约定次版本号(即第一个小数点后的数字)为偶数的版本是稳定版(如2.8版,3.0版),奇数版本是非稳定版(如2.7版,2.9版),生产环境下一般需要使用稳定版本。


一、安装环境

Redis是C语言编写的,所以在编译前要安装编译环境gcc

[root@VM-0-17-centos ~]# yum install -y gcc
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
epel                                                                                                                                                                                             | 4.7 kB  00:00:00     
extras                                                                                                                                                                                           | 2.9 kB  00:00:00     
os                                                                                                                                                                                               | 3.6 kB  00:00:00     
updates                                                                                                                                                                                          | 2.9 kB  00:00:00     
Resolving Dependencies
...

二、下载Redis安装包

一是,在官网下载后,上传到linux中
二是,直接在linux通过wget下载

Redis官网下载:https://redis.io/download

安装wget

[root@VM-0-17-centos ~]# yum install -y wget
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Package wget-1.14-18.el7_6.1.x86_64 already installed and latest version
Nothing to do

下载redis-6.2.4.tar.gz

[root@VM-0-17-centos ~]# wget https://download.redis.io/releases/redis-6.2.4.tar.gz
--2021-06-22 09:26:34--  https://download.redis.io/releases/redis-6.2.4.tar.gz
Resolving download.redis.io (download.redis.io)... 45.60.125.1
Connecting to download.redis.io (download.redis.io)|45.60.125.1|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2457940 (2.3M) [application/octet-stream]
Saving to: ‘redis-6.2.4.tar.gz’

100%[==============================================================================================================================================================================>] 2,457,940   5.30MB/s   in 0.4s   

2021-06-22 09:26:34 (5.30 MB/s) - ‘redis-6.2.4.tar.gz’ saved [2457940/2457940]
[root@VM-0-17-centos ~]# ll
total 2404
-rw-r--r-- 1 root root 2457940 Jun  1 22:16 redis-6.2.4.tar.gz

三、解压

解压不查看日志

[root@VM-0-17-centos ~]# tar xzf redis-6.2.4.tar.gz
[root@VM-0-17-centos ~]# ll
total 2408
drwxrwxr-x 7 root root    4096 Jun  1 22:03 redis-6.2.4
-rw-r--r-- 1 root root 2457940 Jun  1 22:16 redis-6.2.4.tar.gz

四、编译

[root@VM-0-17-centos ~]# cd redis-6.2.4/
[root@VM-0-17-centos redis-6.2.4]# make
...
...
Hint: It's a good idea to run 'make test' ;)

make[1]: Leaving directory `/root/redis-6.2.4/src'

五、安装

5.1 查看redis-6.2.4/README.md中的“Installing Redis”

在这里插入图片描述
我这里新建目录,安装到指定目录/opt/su/redis6

[root@VM-0-17-centos redis-6.2.4]# mkdir -p /opt/su/redis6
[root@VM-0-17-centos redis-6.2.4]# make install PREFIX=/opt/su/redis6
cd src && make install
make[1]: Entering directory `/root/redis-6.2.4/src'
    CC Makefile.dep
make[1]: Leaving directory `/root/redis-6.2.4/src'
make[1]: Entering directory `/root/redis-6.2.4/src'

Hint: It's a good idea to run 'make test' ;)

    INSTALL redis-server
    INSTALL redis-benchmark
    INSTALL redis-cli
make[1]: Leaving directory `/root/redis-6.2.4/src'

5.2 将Redis安装成服务,随系统启动

首先配置环境变量

[root@VM-0-17-centos redis-6.2.4]# vim /etc/profile
在末尾添加
export REDIS_HOME=/opt/su/redis6
export PATH=$PATH:$REDIS_HOME/bin

生效配置文件

[root@VM-0-17-centos redis-6.2.4]# source /etc/profile

进入redis安装目录,执行install_server.sh脚本

[root@VM-0-17-centos redis-6.2.4]# cd utils/
[root@VM-0-17-centos utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server

This systems seems to use systemd.
Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!

出现以上错误,需要修改install_server.sh脚本,若没有出现错误则跳过

[root@VM-0-17-centos utils]# vi ./install_server.sh
注释文件中以下部分
#bail if this system is managed by systemd
#_pid_1_exe="$(readlink -f /proc/1/exe)"
#if [ "${_pid_1_exe##*/}" = systemd ]
#then
#        echo "This systems seems to use systemd."
#        echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!"
#        exit 1
#fi

重新执行安装脚本,选择默认直接按Enter键即可

[root@VM-0-17-centos utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] 
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] 
Selected default - /var/lib/redis/6379
Please select the redis executable path [/opt/su/redis6/bin/redis-server] 
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /opt/su/redis6/bin/redis-server
Cli Executable : /opt/su/redis6/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!

默认安装时已经启动redis,查看redis运行状态

[root@VM-0-17-centos utils]# service redis_6379 status
Redis is running (22952)

注意:

  1. 一个物理机中可以有多个erdis实例(进程),通过port区分;执行./install_server.sh
  2. 可执行程序就一份在指定目录,但是内存中未来的多个实例需要自己的配置文件,持久化目录等资源
  3. 启动停止命令:service redis_port start/stop/status 文件所在 /etc/init.d/***

六、测试

通过redis-cli连接redis

[root@VM-0-17-centos ~]# redis-cli 
127.0.0.1:6379> set key1 hello
OK
127.0.0.1:6379> get key1
"hello"
127.0.0.1:6379> set num 1
OK
127.0.0.1:6379> incr num
(integer) 2
127.0.0.1:6379> get num
"2"
127.0.0.1:6379> keys *
1) "key1"
2) "num"
127.0.0.1:6379> 

七、了解可执行文件

文件名说明
redis-serverRedis服务器
redis-cliRedis命令行客户端
redis-benchmarkRedis性能测试工具
redis-check-aofAOF文件检查工具
redis-check-rdbRDB文件检查工具
redis-sentinelSentinel服务器

  我们最常用的两个程序时redis-server和redis-cli,其中redis-server是Redis的服务器,启动Redis即运行redis-server;而redis-cli是Redis自带的Redis命令行客户端,是学习Redis的重要工具。


八、启动和停止Redis

8.1 直接启动

[root@VM-0-17-centos ~]# redis-server

Redis服务器默认会使用6379端口,通过 --port参数可以自定义端口号:

[root@VM-0-17-centos ~]# redis-server --port 6380

6379是手机键盘上MERZ对应的数字,MERZ是一名意大利歌女的名字。

8.2 以服务启动

[root@VM-0-17-centos ~]# service redis_6379 start 
Starting Redis server...

8.3 停止Redis

  考虑到Redis有可能正在将内存中的数据同步到硬盘中,强行终止Redis进程可能会导致数据丢失。正确停止Redis的方式应该是想Redis发送SHUTDOWN命令

[root@VM-0-17-centos ~]# redis-cli SHUTDOWN

  当Redis收到SHUTDOWN命令后,会先断开所有客户端连接,然后根据配置执行持久化,最后完成瑞出。

7393:M 22 Jun 2021 21:54:25.047 # User requested shutdown...
7393:M 22 Jun 2021 21:54:25.047 * Saving the final RDB snapshot before exiting.
7393:M 22 Jun 2021 21:54:25.051 * DB saved on disk
7393:M 22 Jun 2021 21:54:25.051 # Redis is now ready to exit, bye bye...

九、Redi命令行客户端

  redis-cli(Redis Command Line Interface)是Redis自带的基于命令行的Redis客户端。

9.1 发送命令

第一种方式是将命令作为redis-cli的参数执行

  redis-cli执行时会自动按照默认配置(服务器地址为127.0.0.1,端口号为6379)连接Redis,通过-h和-p参数可以自定义地址和端口:

[root@VM-0-17-centos ~]# redis-cli 127.0.0.1 -p 6379

  Redis提供了PING命令来测试客户端与Redis的连接是否正常,如果连接正常会收到PONG:

[root@VM-0-17-centos ~]# redis-cli PING
PONG

第二种方式是不附带参数运行redis-cli,这样会进入交互模式,可以自由输入命令:

[root@VM-0-17-centos ~]# redis-cli
127.0.0.1:6379> PING
PONG
127.0.0.1:6379> ECHO hi
"hi"
127.0.0.1:6379> 

9.2 命令返回值

  在大多数情况下,执行一条命令后我们往往会关心命令的返回值。命令的返回值有5种类型,对于每种类型redis-cli的展现结果不同,分别说明:

9.2.1 状态回复

  状态回复(status reply)是最简单的一种回复,比如向Redis发送SET命令设置某个键的值时,Redis会回复状态OK表示设置成功。另外之前演示的对PING命令的回复PONG也是状态回复。状态回复直接显示状态信息。

9.2.2 错误回复

  当出现命令不存在或命令格式有错误等情况时,Redis会返回错误回复(error reply)。错误回复以(error)开头,并在后面跟上错误信息,如执行一条不存在的命令:

127.0.0.1:6379> ewqeq
(error) ERR unknown command `ewqeq`, with args beginning with: 
127.0.0.1:6379> LPUSH key 1
(integer) 1
127.0.0.1:6379> get key
(error) WRONGTYPE Operation against a key holding the wrong kind of value

9.2.3 整数回复

  Redis虽然没有整数类型,但是却提供了一些用于整数操作的命令,如递增键值的INCR命令会以整数形式返回递增后的键值。整数回复(integer reply)以(integer)开头,并在后面跟上整数数据:

127.0.0.1:6379> set num 1
OK
127.0.0.1:6379> incr num
(integer) 2

9.2.4 字符串回复

  字符串回复(bulk reply)是最常见的一种回复类型,当请求一个字符串类型的键的键值或一个其他类型键中的某个元素时,就会得到一个字符串回复。字符串回复以双引号包裹:

127.0.0.1:6379> get num
"2"

  特殊情况是当请求的键值不存在时会得到一个空结果,显示为(nil)。如:

127.0.0.1:6379> get nnn
(nil)

9.2.5 多行字符串回复

  多行字符串回复(multi-bulk reply)同样很常见,如当请求一个非字符串类型键的元素列表时,就会收到多行字符串回复。多行字符串回复中的每行字符串都以一个序号开头,如:

127.0.0.1:6379> keys *
1) "num"
2) "key"
3) "su"

十、配置

  前面提到可以通过redis-server的启动参数port设置Redis的端口号,除此之外Redis还支持其他配置选项,如是否开启持久化、日志级别等。由于可以配置的选项较多,通过启动参数设置这些选项并不方便,所以Redis支持通过配置文件来设置这些选项。启用配置文件的方法是在启动时,将配置文件的路径作为启动参数传递给redis-server,如:

[root@VM-0-17-centos ~]# redis-server /path/to/redis.conf

  通过启动参数传递同名的配置选项会覆盖文件中相应的参数,如:

[root@VM-0-17-centos ~]# redis-server /path/to/redis.conf --loglevel warning

  Redis提供了一个配置文件的模板redis.conf,位于源代码目录的根目录中。
  除此之外,还可以在Redis运行时通过CONFIG SET命令在不重启Redis的情况下动态修改部分Redis配置,如:

127.0.0.1:6379> CONFIG SET loglevel warning
OK

  并不是所有的配置都可以使用CONFIG SET命令修改。同样在运行时,可以使用CONFIG GET命令获取Redis当前的配置情况,如:

127.0.0.1:6379> CONFIG GET loglevel
1) "loglevel"
2) "warning"

其中第一行字符串回复表示的是选项名,第二行即是选项值。


十一、多数据库

  一个Redis实例提供了多个用来存储数据的字典,客户端可以指定将数据存储在哪个字典中,这与我们熟知的在一个关系型数据库实例中可以创建多个数据库类似,所以可以将其中的每个字典都理解成一个独立的数据库。
  每个数据库对外都是以一个从0开始的递增数字命名,Redis默认支持16个数据库,可以通过配置参数databases来修改这一数字。客户端与Redis建立连接后会自动选择0号数据库,不过可以使用SELECT 命令更改数据库,如要选择1号数据库:

127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> get key
(nil)


author:su1573
鄙人记录生活点滴,学习并分享,请多指教!!!
如需交流,请联系 sph1573@163.com,鄙人看到会及时回复

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ssy03092919

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值