Redis介绍,Redis安装,redis持久化

Redis介绍

Redis和Memcached类似,也属于k-v数据存储
Redis官网redis.io, 当前最新稳定版4.0.1
支持更多value类型,除了和string外,还支持hash、lists(链表)、sets(集合)和sorted sets(有序集合)
redis使用了两种文件格式:全量数据(RDB)和增量请求(aof)。全量数据格式是把内存中的数据写入磁盘,便于下次读取文件进行加载。增量请求文件则是把内存中的数据序列化为操作请求,用于读取文件进行replay得到数据,这种类似于mysql binlog。
redis的存储分为内存存储、磁盘存储和log文件三部分

Redis安装

先进入到/usr/local/src目录下,下载Redis

[root@shuai-01 ~]# cd /usr/local/src
下载的是Redis-4.0.2版本
[root@shuai-01 src]# wget http://download.redis.io/releases/redis-4.0.2.tar.gz

解压,进入目录安装

[root@shuai-01 redis-4.0.2]# tar zxvf redis-4.0.2.tar.gz

[root@shuai-01 src]# cd redis-4.0.2/

Redis安装不和其他的一样,只需要make和make install就行。

[root@shuai-01 redis-4.0.2]# make

Redis默认安装的位置

[root@shuai-01 redis-4.0.2]# which redis-cli
/usr/local/bin/redis-cli

Redis几个相关命令:

[root@shuai-01 redis-4.0.2]# ls /usr/local/bin/
redis-benchmark  redis-check-rdb  redis-sentinel
redis-check-aof  redis-cli        redis-server

Redis官网:redis.io

配置

将Redis配置文件拷贝到/etc目录下

[root@shuai-01 redis-4.0.2]# cp /usr/local/src/redis-4.0.2/redis.conf /etc/

编辑配置文件

[root@shuai-01 ~]# vim /etc/redis.conf 

一般配置Redis的几个常用的配置

daemonize yes          #yes表示后台启动;no表示前台启动
pidfile /var/run/redis_6379.pid  #pid存放位置
loglevel notice        
#指定日志级别:debug(调试,排错)、verbose(冗长的)、notice、warning
#debug适合排查错误时使用,错误排查完毕后要换到notice,避免产生大量日志浪费系统空间  
logfile "/tmp/logs/redis.log"  #定义日志存放路径  
databases 16   
#Redis有库的概念,默认在0库
dir /data/redis     #定义rdb、aof文件的存放位置
appendonly yes          #开启aof日志,开启后会在dir定义的目录生成appendonly.aof文件  
appendfsync everysec      #指定记录日志的规则:always(只要有变动就记录)、everysec(每秒记录一次)、no(不记录)

启动redis

[root@shuai-01 ~]# redis-server /etc/redis.conf 

*** FATAL CONFIG FILE ERROR ***
Reading the configuration file, at line 171
>>> 'logfile "/tmp/logs/redis.log"'
Can't open the log file: No such file or directory

没有logfile文件,创建一个文件

[root@shuai-01 ~]# touch /tmp/logs/redis.log

[root@shuai-01 ~]# ps aux |grep redis-server
root       7922  1.0  0.7 145256  7516 ?        Ssl  16:50   0:00 redis-server 127.0.0.1:6379
root       7927  0.0  0.0 112680   980 pts/0    S+   16:51   0:00 grep --color=auto redis-server

查看日志:

7921:C 18 Apr 16:50:06.212 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7921:C 18 Apr 16:50:06.212 # Redis version=4.0.2, bits=64, commit=00000000
, modified=0, pid=7921, just started
7921:C 18 Apr 16:50:06.212 # Configuration loaded
7922:M 18 Apr 16:50:08.288 * Increased maximum number of open files to 100
32 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 4.0.2 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 7922
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

将redis内核参数配置加入到开机自启动:

sysctl vm.overcommit_memory=1
echo never > /sys/kernel/mm/transparent_hugepage/enabled


[root@shuai-01 ~]# vim /etc/rc.local

sysctl vm.overcommit_memory=1
echo never > /sys/kernel/mm/transparent_hugepage/enabled

关于redis中监听的IP及端口号,都可以在配置文件中做相应的修改

redis持久化

Redis提供了两种持久化的方式,分别是RDB(Redis DataBase)和AOF(Append Only File)
RDB,简而言之,就是在不同的时间点,将redis存储的数据生成快照并存储到磁盘等介质上。
AOF,则是换了一个角度来实现持久化,那就是将redis执行过的所有写指令记录下来,在下次redis重新启动时,只要把这些写指令从前到后再重复执行一遍,就可以实现数据恢复了。
其实RDB和AOF两种方式也可以同时使用,在这种情况下,如果redis重启的话,则会优先采用AOF方式来进行数据恢复,这是因为AOF方式的数据恢复完整度更高。
如果你没有数据持久化的需求,也完全可以关闭RDB和AOF方式,这样的话,redis将变成一个纯内存数据库,就像memcache一样。

持久化就是将数据从内存中复制一份到硬盘上

持久化的规则:
在redis配置文件中,

这一项是关闭持久化
#   save ""
这三项是持久化的规则
save 900 1
save 300 10
save 60 10000

aof有三种方式,aof是记录redis执行过的所有指令。

#每次都写进去
# appendfsync always
#每一秒写进去,默认是这种方式
appendfsync everysec
#不规定,根据linux系统规则来
# appendfsync no

猜你喜欢

转载自blog.csdn.net/chen112826473/article/details/80339135