目录

  • 一、Redis 简介
  • 二、Redis 的安装
  • 一、Windows 上 Redis 的安装
  • 二、Linux 上 Redis 的安装


一、Redis 简介



  Remote Dictionary Server(远程字典服务器),是一个用C语言编写的、开源的、基于内存运行并支持持久化的、高性能的NoSQL数据库。

  Redis 是支持网络的、可基于内存亦可持久化的 key-value数据库,其中 key 是字符类型,其值 value 可以使字符串(String)、哈希(Map)、列表(List)、集合(Sets)和有序集合(sorted Sets)等类型,每种数据类型都有自己的专属命令,所以它通常也被称为数据结构服务器。


二、Redis 的安装



  Redis 分为 Linux 版本和 Windows 版本,然而 Windows 版本不是 Redis 官方所提供,它是 Microsoft 的开源部门提供的,这个版本的 Redis 适合我们平时的学习使用,但是在真正的生产环境中使用的是 Linux 上的 Redis。


一、Windows 上 Redis 的安装



 1.下载 Windows 版的 Redis : 点击下载Windows版 Redis.

redis 区分windows和linux redis安装windows和linux区别_linux



 2.解压下载的压缩包,我们可以看到以下目录:

redis 区分windows和linux redis安装windows和linux区别_linux_02



 3.双击打开 redis 服务器,出现以下界面打开成功:

redis 区分windows和linux redis安装windows和linux区别_linux_03


  如果出现打开 Redis 服务器一闪而过时,解决方法如下:链接: 003 - 解决 Windows 打开 Redis 服务器一闪而过问题.


 4.双击打开 redis 客户端,出现以下界面打开成功:

redis 区分windows和linux redis安装windows和linux区别_redis_04


二、Linux 上 Redis 的安装



 1.下载 Linux 版的 Redis : 点击下载Linux 版 Redis.

redis 区分windows和linux redis安装windows和linux区别_服务器_05



 2.使用工具(我这里使用的是 Xftp )上传 redis-5.0.2.tar.gz (我这里使用的是之前下载的版本,上面官网截图的版本是6.2.1的)到 Linux 系统并在当前目录将其解压到 /usr/local 目录下:

[root@localhost home]# tar -zxvf redis-5.0.2.tar.gz -C /usr/local



 3.进入解压目录,执行 make 命令编译 redis:

[root@localhost home]# cd /usr/local
[root@localhost local]# cd redis-5.0.2/
[root@localhost redis-5.0.2]# make

redis 区分windows和linux redis安装windows和linux区别_linux_06



 4.执行 make install,该操作则将 src下的许多可执行文件复制到/usr/local/bin 目录下,这样做可以在任意目录执行redis的软件的命令(例如启动,停止,客户端连接服务器等),因此 make install 可以不用执行;

[root@localhost redis-5.0.2]# make install



 5.编译成功后,我们可以进入 src 目录下看编译结果:

redis 区分windows和linux redis安装windows和linux区别_服务器_07



 6.启动 redis 服务器,redis服务器 的启动方式有三种,如下:

① 前台启动 redis-server,此时启动 redis 保持在前台运行,我们无法进行其他操作
[root@localhost src]# redis-server 
4508:C 09 Apr 2021 10:46:26.169 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4508:C 09 Apr 2021 10:46:26.169 # Redis version=5.0.2, bits=64, commit=00000000, modified=0, pid=4508, just started
4508:C 09 Apr 2021 10:46:26.169 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
4508:M 09 Apr 2021 10:46:26.171 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.2 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 4508
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

4508:M 09 Apr 2021 10:46:26.174 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
4508:M 09 Apr 2021 10:46:26.174 # Server initialized
4508:M 09 Apr 2021 10:46:26.174 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
4508:M 09 Apr 2021 10:46:26.174 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
4508:M 09 Apr 2021 10:46:26.178 * DB loaded from disk: 0.004 seconds
4508:M 09 Apr 2021 10:46:26.178 * Ready to accept connections
②后台启动 redis-server &
[root@localhost src]# redis-server  &
[1] 4529
[root@localhost src]# 4529:C 09 Apr 2021 10:48:29.550 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4529:C 09 Apr 2021 10:48:29.550 # Redis version=5.0.2, bits=64, commit=00000000, modified=0, pid=4529, just started
4529:C 09 Apr 2021 10:48:29.550 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
4529:M 09 Apr 2021 10:48:29.552 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.2 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 4529
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

4529:M 09 Apr 2021 10:48:29.553 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
4529:M 09 Apr 2021 10:48:29.553 # Server initialized
4529:M 09 Apr 2021 10:48:29.553 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
4529:M 09 Apr 2021 10:48:29.553 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
4529:M 09 Apr 2021 10:48:29.553 * DB loaded from disk: 0.000 seconds
4529:M 09 Apr 2021 10:48:29.553 * Ready to accept connections


此时 redis 正在后台运行,我们可以进行其他操作,可以通过查看进程号查看正在运行的 redis  
[root@localhost src]# ^C
[root@localhost src]# ps -ef | grep redis
root       4529   2680  0 10:48 pts/0    00:00:00 redis-server *:6379
root       4561   2680  0 10:50 pts/0    00:00:00 grep --color=auto redis
[root@localhost src]#
②根据配置文件启动 启动命令 配置文件 & 
[root@localhost src]# redis-server ./redis.conf  &
[1] 4602
[root@localhost src]# 4602:C 09 Apr 2021 10:52:41.796 # Fatal error, can't open config file './redis.conf'

[1]+  Exit 1                  redis-server ./redis.conf
[root@localhost src]# cd ..
[root@localhost redis-5.0.2]# ls
00-RELEASENOTES  CONTRIBUTING  INSTALL    README.md   runtest-cluster   src
bak_redis.conf   COPYING       Makefile   redis.conf  runtest-sentinel  tests
BUGS             deps          MANIFESTO  runtest     sentinel.conf     utils
[root@localhost redis-5.0.2]# redis-server redis.conf  &
[1] 4604
[root@localhost redis-5.0.2]# 4604:C 09 Apr 2021 10:53:06.918 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4604:C 09 Apr 2021 10:53:06.918 # Redis version=5.0.2, bits=64, commit=00000000, modified=0, pid=4604, just started
4604:C 09 Apr 2021 10:53:06.918 # Configuration loaded
4604:M 09 Apr 2021 10:53:06.919 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.2 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 4604
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

4604:M 09 Apr 2021 10:53:06.919 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
4604:M 09 Apr 2021 10:53:06.919 # Server initialized
4604:M 09 Apr 2021 10:53:06.919 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
4604:M 09 Apr 2021 10:53:06.919 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
4604:M 09 Apr 2021 10:53:06.919 * Ready to accept connections

注意:如果修改了redis的配置文件redis.conf,必须在每次启动时指定配置文件,否则修改无效!



 6.启动 redis 客户端,:

1)	直接连接redis (默认ip127.0.0.1,端口6379)
[root@localhost redis-5.0.2]# redis-cli
127.0.0.1:6379> 


2)	指定IP和端口连接redis:redis-cli –h 主机ip  -p 端口号
	-h redis主机IP(可以指定任意的redis服务器)
	-p端口号(不同的端口表示不同的redis应用)
[root@localhost redis-5.0.2]# redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379>



 7.关闭 redis 客户端,:

1)	直接连接 redis-cli shutdown 推荐使用这种方式,这种方式 redis 先完成数据操作,然后再关闭。
[root@localhost redis-5.0.2]# redis-cli shutdow

2)	使用 ps -ef | grep redis 查出进程号,再使用 kill pid 或者 kill -9 pid 杀死进程
	-h redis主机IP(可以指定任意的redis服务器)
	-p端口号(不同的端口表示不同的redis应用)
[root@localhost redis-5.0.2]# ps -ef | grep redis
root       4673   2680  0 10:57 pts/0    00:00:01 redis-server *:6379
root       4884   2680  0 11:12 pts/0    00:00:00 grep --color=auto redis
[root@localhost redis-5.0.2]# kill -9 4673



 8.关闭 redis 客户端,:

1)	exit 指令:
[root@localhost redis-5.0.2]# redis-cli
127.0.0.1:6379> exit


2)	quit指令 :
[root@localhost redis-5.0.2]# redis-cli
127.0.0.1:6379>	quit