Redis4.0安装启动

版权说明:本文为博主原创,如果转载请注明来源。作为学习笔记,不能保证所有知识点是完全正确以及表达无误,用于生产环境配置时请斟酌。如有错误或建议请联系。侵删联系:linuxops@foxmail.com。感谢各位!

一、前言

REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统。

Redis是一个开源的使用ANSI C语言编写、遵守BSD协议、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。

它通常被称为数据结构服务器,因为值(value)可以是 字符串(String), 哈希(Map), 列表(list), 集合(sets) 和 有序集合(sorted sets)等类型。

Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库。

Redis 有以下特点:

  • Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。
  • Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存储。
  • Redis支持数据的备份,即master-slave模式的数据备份。
  • 性能极高 – Redis能读的速度是110000次/s,写的速度是81000次/s 。
  • 丰富的数据类型 – Redis支持二进制案例的 Strings, Lists, Hashes, Sets 及 Ordered Sets 数据类型操作。
  • 原子 – Redis的所有操作都是原子性的,意思就是要么成功执行要么失败完全不执行。单个操作是原子性的。多个操作也支持事务,即原子性,通过MULTI和EXEC指令包起来。
  • 丰富的特性 – Redis还支持 publish/subscribe, 通知, key 过期等等特性。
  • Redis有着更为复杂的数据结构并且提供对他们的原子性操作,这是一个不同于其他数据库的进化路径。Redis的数据类型都是基于基本数据结构的同时对程序员透明,无需进行额外的抽象。
  • Redis运行在内存中但是可以持久化到磁盘,所以在对不同数据集进行高速读写时需要权衡内存,因为数据量不能大于硬件内存。在内存数据库方面的另一个优点是,相比在磁盘上相同的复杂的数据结构,在内存中操作起来非常简单,这样Redis可以做很多内部复杂性很强的事情。同时,在磁盘格式方面他们是紧凑的以追加的方式产生的,因为他们并不需要进行随机访问。

​本篇介绍如在LINUX上安装Redis并且启动。

用到的环境如下:

云平台:华为云

操作系统:centos 7.4

redis版本:4.0.9

下面开始安装吧。


二、安装redis

下载redis

[root@Redis ~]# wget http://download.redis.io/releases/redis-4.0.9.tar.gz
--2018-05-10 14:13:04--  http://download.redis.io/releases/redis-4.0.9.tar.gz
Resolving download.redis.io (download.redis.io)... 109.74.203.151
Connecting to download.redis.io (download.redis.io)|109.74.203.151|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1737022 (1.7M) [application/x-gzip]
Saving to: ‘redis-4.0.9.tar.gz’

100%[=============================================================>] 1,737,022    784KB/s   in 2.2s   

2018-05-10 14:13:07 (784 KB/s) - ‘redis-4.0.9.tar.gz’ saved [1737022/1737022]

[root@Redis ~]# 

解压缩

[root@Redis ~]# tar -zxvf redis-4.0.9.tar.gz 
redis-4.0.9/
redis-4.0.9/.gitignore
redis-4.0.9/00-RELEASENOTES
redis-4.0.9/BUGS
redis-4.0.9/CONTRIBUTING
.
.
.

编译安装

[root@Redis ~]# cd redis-4.0.9
[root@Redis redis-4.0.9]# make #先make
[root@Redis redis-4.0.9]# make PREFIX=/usr/local/redis install #make成功之后install,通过PREFIX指定安装目录
cd src && make install
make[1]: Entering directory `/root/redis-4.0.9/src'

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

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
make[1]: Leaving directory `/root/redis-4.0.9/src'

复制配置文件

安装成功以后,我们来看一下安装目录里面有什么:

[root@Redis redis-4.0.9]# ls /usr/local/redis/
bin

非常简洁,只有一个bin目录,并没有看到配置文件,需要我们安装包中复制配置文件到安装目录。

[root@Redis redis-4.0.9]# cp redis.conf /usr/local/redis/redis.conf

三、启动redis

源码包里面的配置文件是官方默认的,可以直接使用,通过bin/redis-server命令就可以启动rendis

[root@Redis redis-4.0.9]# cd /usr/local/redis/bin/
[root@Redis bin]# ./redis-server /usr/local/redis/redis.conf 
4914:C 10 May 14:22:46.772 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4914:C 10 May 14:22:46.772 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=4914, just started
4914:C 10 May 14:22:46.772 # Configuration loaded
4914:M 10 May 14:22:46.773 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 4.0.9 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 4914
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

4914:M 10 May 14:22:46.774 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
4914:M 10 May 14:22:46.774 # Server initialized
4914:M 10 May 14:22:46.774 # 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.
4914:M 10 May 14:22:46.774 # 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.
4914:M 10 May 14:22:46.774 * Ready to accept connections

如上图,redis已经启动成功了,并且打印出了redis的吉祥物。

官方默认配置文件启动redis并不是以daemon方式启动的,在生产环境中这并不是我们想要的。

我们需要修改配置文件中的daemonizeyes,redis将会daemon方式启动。

[root@Redis bin]# ./redis-server /usr/local/redis/redis.conf 
4923:C 10 May 14:28:48.127 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4923:C 10 May 14:28:48.127 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=4923, just started
4923:C 10 May 14:28:48.127 # Configuration loaded
[root@Redis bin]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      4924/./redis-server 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1101/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1058/master         
tcp        0      0 127.0.0.1:32000         0.0.0.0:*               LISTEN      1401/java           
tcp6       0      0 :::22                   :::*                    LISTEN      1101/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1058/master         
[root@Redis bin]# 

如上,redis以daemon方式启动了。

四、redis客户端

redis官方自带了一个CLI的客户端工具redis-cli,用这个工具可以连接redis服务器。

[root@Redis bin]# ./redis-cli -p 6379
127.0.0.1:6379> ping #发送ping命令到redis服务器
PONG                 #返回PONG说明redis服务可用
127.0.0.1:6379> 

可以通过redis-cli查看redis服务器的各种信息,例如发送info可以查看redis详细的信息,这边先不展开说明。