21.9 redis介绍

21.9 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文件三部分mysql

21.10 redis安装

下载最新稳定版
cd /usr/local/src/
wget http://download.redis.io/releases/redis-4.0.1.tar.gz
cd redis-4.0.1
make && make install
cp redis.conf /etc/redis.conf
vim /etc/redis.conf //修改以下配置
daemonize yes
logfile "/var/log/redis.log"
dir /data/redis_data/
appendonly yes
mkdir /data/redis_data
sysctl vm.overcommit_memory=1
echo never > /sys/kernel/mm/transparent_hugepage/enabled
redis-server /etc/redis.confredis

安装redis:sql

[root@Dasoncheng ~]# cd /usr/local/src/
[root@Dasoncheng src]# wget http://download.redis.io/releases/redis-4.0.1.tar.gz
[root@Dasoncheng src]# ll
-rw-r--r--  1 root root   1711660 Jul 24 21:59 redis-4.0.1.tar.gz
[root@Dasoncheng src]# tar -zxf redis-4.0.1.tar.gz
[root@Dasoncheng src]# ll
drwxrwxr-x  6 root root       309 Jul 24 21:58 redis-4.0.1
[root@Dasoncheng redis-4.0.1]# make
cd src && make all
make[1]: Entering directory `/usr/local/src/redis-4.0.1/src'
    CC adlist.o
/bin/sh: cc: command not found
make[1]: *** [adlist.o] Error 127
make[1]: Leaving directory `/usr/local/src/redis-4.0.1/src'
make: *** [all] Error 2
[root@Dasoncheng redis-4.0.1]# yum install -y gcc    ##缺乏gcc库,安装:
[root@Dasoncheng redis-4.0.1]# make
cd src && make all
make[1]: Entering directory `/usr/local/src/redis-4.0.1/src'
    CC adlist.o
In file included from adlist.c:34:0:
zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory
 #include <jemalloc/jemalloc.h>
                               ^
compilation terminated.
make[1]: *** [adlist.o] Error 1
make[1]: Leaving directory `/usr/local/src/redis-4.0.1/src'
make: *** [all] Error 2
[root@Dasoncheng redis-4.0.1]# less README.md     ##搜索关键词jemalloc,参考说明 安装:
[root@Dasoncheng redis-4.0.1]# make MALLOC=libc
[root@Dasoncheng redis-4.0.1]# echo $?
0
[root@Dasoncheng redis-4.0.1]# make install

修改配置文件/etc/redis.conf:数据库

[root@Dasoncheng redis-4.0.1]# cp redis.conf /etc/
[root@Dasoncheng redis-4.0.1]# vim /etc/redis.conf
# bind 192.168.1.100 10.0.0.1   设置监听ip,多个用空格分开;
protected-mode yes      打开保护模式;
port 6379    监听的端口;
daemonize yes    ##修改成yes,后台启动;no的话就是前台启动;(daemonize中文:守护进程;)
pidfile /var/run/redis_6379.pid    指定运行的pid文件;
loglevel notice    指定日志级别;
logfile "/var/log/redis.log"    ##定义日志文件;(这里咱们定义一下)
databases 16    redis的库数量默认总共16个;默认在0 库里面;
save 900 1
save 300 10
save 60 10000    这三个都是用来设置rdb的持久化的;
rdbcompression yes    是否压缩rdb文件;
dbfilename dump.rdb    定义rdb文件名;
dir /data/redis    ##定义rdb文件存放的目录(包括aof文件也会放到这里);
slave-serve-stale-data yes    配置主从的参数;
appendonly yes    ##开启aof的日志;
appendfilename "appendonly.aof"    定义文件名;
# appendfsync always
appendfsync everysec
# appendfsync no    这三个是何时记录日志;一、老是;二、每second秒一次;三、隔一段时间记录(是Linux系统默认定时同步内存到磁盘);

启动redis:vim

[root@Dasoncheng redis-4.0.1]# mkdir /data/redis
[root@Dasoncheng redis-4.0.1]# redis-server /etc/redis.conf 
[root@Dasoncheng redis-4.0.1]# ps aux |grep redis
root       5395  0.8  0.1 141760  1972 ?        Ssl  16:17   0:00 redis-server 127.0.0.1:6379
root       5400  0.0  0.0 112660   968 pts/1    S+   16:17   0:00 grep --color=auto redis
[root@Dasoncheng redis-4.0.1]# tail /var/log/redis.log 
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

5395:M 12 Oct 16:17:40.761 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
5395:M 12 Oct 16:17:40.761 # Server initialized
5395:M 12 Oct 16:17:40.762 # 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.
5395:M 12 Oct 16:17:40.765 # 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.
5395:M 12 Oct 16:17:40.766 * Ready to accept connections
##有两条内存报错问题;经过下面两条命令解决 不解决也没什么问题;
[root@Dasoncheng redis-4.0.1]# sysctl vm.overcommit_memory=1
vm.overcommit_memory = 1
[root@Dasoncheng redis-4.0.1]#  echo never > /sys/kernel/mm/transparent_hugepage/enabled
[root@Dasoncheng redis-4.0.1]# vim /etc/rc.local    ##将这两条命令添加到启动脚本里面;
sysctl vm.overcommit_memory=1
echo never > /sys/kernel/mm/transparent_hugepage/enabled

提示:安装redis并非那么顺利;
一、没有gcc工具,没法编译;
yum install -y gcc
二、安装报错: error: jemalloc/jemalloc.h: No such file or directory;
less README.md 搜索关键词‘jemalloc’ 找到答案;
https://my.oschina.net/Sheamus/blog/712431
http://blog.csdn.net/libraryhu/article/details/64920124安全

21.11 redis持久化

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

相关参数:less

save 900 1 #表示每15分钟且至少有1个key改变,就触发一次持久化 
save 300 10 #表示每5分钟且至少有10个key改变,就触发一次持久化
save 60 10000 #表示每60秒至少有10000个key改变,就触发一次持久
save “” #这样能够禁用rdb持久化
appendonly yes #若是是yes,则开启aof持久化
appendfilename “appendonly.aof” # 指定aof文件名字
appendfsync everysec #指定fsync()调用模式,有三种no(不调用fsync),always(每次写都会调用fsync),everysec(每秒钟调用一次fsync)。第一种最快,第二种数据最安全,但性能会差一些,第三种为这种方案,默认为第三种。工具

相关文章
相关标签/搜索