安装Redis 4.0单实例

一、Redis简单介绍

转载于网络

Redis是一个开源(BSD许可)的内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。由于Redis采用运行在内存中的数据集工作方式,其性能卓越,能支持超过100K+每秒的读写频率。它支持多种类型的数据结构,如字符串(strings), 散列(hashes),列表(lists),集合(sets),有序集合(sorted sets)与范围查询和地理空间(geospatial)索引半径查询。Redis内置了复制(replication), LUA脚本(Lua scripting),LRU淘汰机制,事务实现(transactions),发布订阅(publish/subscribe)和不同级别的磁盘持久化(persistence)等能力, 并通过Redis哨兵(Sentinel)和自动分区(Cluster)提供高可用性(high availability)。

Redis的主要功能都是基于单线程网络模型实现,也就是说Redis使用一个线程来服务所有的客户端请求,同时Redis采用了非阻塞式IO,并精细地优化各种命令的算法和时间复杂度,大部分命令的算法都是O(1)的,详细的命令具体可以看 Redis命令参考

另外Redis的大部分操作都是原子性的(简单的单线程模型),同时Redis还支持对几个操作全并后的原子性执行。列如:字符串(strings)的append命令;散列(hashes)的hincrby命令;列表(lists)的lpush命令;集合(sets)计算交集sinter命令,计算并集union命令和计算差集sdiff命令;或者在有序集合(sorted sets)里面获取成员的最高排名zrangebyscore命令等。

官方站点:http://redis.io

二、安装Redis 4.0单实例

1、安装依赖包

  1. [root@VM_2_13_centos redis]# yum install gcc* 

2、获取安装文件

  1. [root@VM_2_13_centos redis]# wget http://download.redis.io/releases/redis-4.0.9.tar.gz

3、解压文件

  1. [root@VM_2_13_centos redis]# tar zxvf redis-4.0.9.tar.gz
  2. [root@VM_2_13_centos redis]# ll
  3. total 1708
  4. drwxrwxr-x 6 root root    4096 Mar 27 00:04 redis-4.0.9
  5. -rw-r--r-- 1 root root 1737022 Mar 27 00:04 redis-4.0.9.tar.gz

4、编译安装

  1. [root@VM_2_13_centos redis-4.0.9]# make
  2. [root@VM_2_13_centos redis-4.0.9]# make PREFIX=/usr/local/redis install
  3. cd src && make install
  4. make[1]: Entering directory `/usr/local/redis/redis-4.0.9/src'
  5.     CC Makefile.dep
  6. make[1]: Leaving directory `/usr/local/redis/redis-4.0.9/src'
  7. make[1]: Entering directory `/usr/local/redis/redis-4.0.9/src'
  8. Hint: It's a good idea to run 'make test'
  9.     INSTALL install
  10.     INSTALL install
  11.     INSTALL install
  12.     INSTALL install
  13.     INSTALL install

5、查看redis的版本

  1. [root@VM_2_13_centos ~]# redis-server --version
  2. Redis server v=4.0.9 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=c97ec2b5e9b86914

6、启动redis服务

  1. [root@VM_2_13_centos redis]# /usr/local/redis/bin/redis-server /etc/redis/redis.conf
  2.  
  3. [root@VM_2_13_centos redis]# netstat -tuplan | grep 6379
  4. tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      5305/redis-server
  5.  
  6. [root@VM_2_13_centos redis]# ps -ef | grep redis
  7. root      5305     1  0 21:38 ?        00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:6379
  8. root      5356 30807  0 21:39 pts/1    00:00:00 grep --color=auto redis

7、通过客户端登录

  1. [root@VM_2_13_centos ~]# redis-cli
  2. 127.0.0.1:6379>

备注:如果要卸载redis,把/usr/local/redis/bin/目录下的redis删除即可。为了卸载干净,你还可以把解压和编译的redis包及配置的redis.conf也删除。

三、安全配置

1、设置密码
redis的默认安装是不设置密码的,可以在redis.conf中进行配置

  1. [root@VM_2_13_centos ~]# vim /etc/redis/redis.conf
  2. requirepass qcloud@2018

或者通过命令配置

  1. 127.0.0.1:6379>CONFIG set requirepass qcloud@2018

由于Redis的性能极高,并且输入错误密码后Redis并不会进行主动延迟(考虑到Redis的单线程模型),所以攻击者可以通过穷举法破解Redis的密码(1秒内能够尝试十几万个密码),因此在设置时一定要选择复杂的密码,可以用随机密码生成器生成。

注意:配置Redis复制的时候如果主数据库设置了密码,需要在从数据库的配置文件中通过masterauth参数设置主数据库的密码,以使从数据库连接主数据库时自动使用AUTH命令认证。

验证密码是否有效,是否需要认证

  1. [root@VM_2_13_centos ~]# redis-cli
  2. 127.0.0.1:6379>
  3. 127.0.0.1:6379> keys *
  4. (error) NOAUTH Authentication required.
  5.  
  6. 127.0.0.1:6379> auth qcloud@2018
  7. OK
  8.  
  9. 127.0.0.1:6379> keys *
  10. (empty list or set)

2、禁用高危命令
目前该命令可以正常使用

  1. 127.0.0.1:6379> flushall
  2. OK

关闭redis,但是由于上面设置了密码,必须要认证成功后才能关闭      

  1. [root@VM_2_13_centos ~]# redis-cli shutdown
  2. (error) NOAUTH Authentication required.
  3. [root@VM_2_13_centos ~]# redis-cli -a qcloud@2018 shutdown
  4. [root@VM_2_13_centos ~]#
  5. [root@VM_2_13_centos ~]# ps -ef | grep redis
  6. root      6144  5406  0 21:54 pts/0    00:00:00 grep --color=auto redis

修改配置文件redis.conf,增加如下行:

  1. [root@VM_2_13_centos ~]# vim /etc/redis/redis.conf
  2. rename-command FLUSHALL ""
  3. rename-command CONFIG   ""
  4. rename-command EVAL     ""

重新启动redis

  1. [root@VM_2_13_centos ~]# redis-server /etc/redis/redis.conf
  2. [root@VM_2_13_centos ~]#
  3. [root@VM_2_13_centos ~]# redis-cli
  4. 127.0.0.1:6379>
  5. 127.0.0.1:6379> keys *
  6. (error) NOAUTH Authentication required.
  7. 127.0.0.1:6379>
  8. 127.0.0.1:6379> auth qcloud@2018
  9. OK
  10. 127.0.0.1:6379>
  11. 127.0.0.1:6379> flushall
  12. (error) ERR unknown command 'flushall'
  13. 127.0.0.1:6379>
  14. 127.0.0.1:6379> config
  15. (error) ERR unknown command 'config'
  16. 127.0.0.1:6379>
  17. 127.0.0.1:6379> eval
  18. (error) ERR unknown command 'eval'

通过上面的报错可以发现,在配置文件禁用的三个命令无法使用

3、绑定只能本机访问

  1. [root@VM_2_13_centos ~]# vim /etc/redis/redis.conf
  2. bind 127.0.0.1

4、设置redis开启自启动

    1. [root@VM_2_13_centos ~]# vim /etc/rc.d/rc.local
    2. /usr/local/redis/bin/redis-server /etc/redis/redis.conf &

猜你喜欢

转载自www.cnblogs.com/cheyunhua/p/10435362.html