• Redis4.0之持久化存储(下)


    十,Redis的RDB存储方式

     

    10.1 redis的运行方式说明

    redis如果提供缓存服务,可以关闭所有持久化存储,如此一来redis重启后所有数据会丢失 
    开启rdb或aof持久化存储,能把redis中的数据持久化到磁盘中。 
    rdb和aof对性能都有影响,所以建议持久化的操作在从库上进行

     

    10.2 redis rdb存储方式,使用save配置开启rdb存储或者关闭rdb存储

     
    1. #与rdb相关的配置文件信息
    2. dir /data/redis/ #dir为rdb存储的路径
    3. dbfilename dump.rdb #rdb存储文件的名字
    4. save 60 10000 #60s改变10000key,触发rdb存储
    5. save 300 10 #300s改变10个key,触发rdb存储
    6. save 900 1 #900s改变1个key触发rdb存储
    7. rdbcompression no #rdb压缩最好关闭,影响cpu
     

    10.3 设置开启或者关闭rdb存储

    提示:默认情况下rdb持久化存储是开启的

    redis-cli config set save "" #关闭rdb存储

    redis-cli config rewrite #配置保存

    redis-cli config set save "180 1 120 10 60 10000" #开启rdb

    redis-cli config rewrite #配置保存

    image_1d01k0lqck84gt01p89e7615em9.png-23.1kB

     

    10.4 进行数据写入,观察rdb存储日志

     

    输入1万条数据

    for line in `seq -w 10000`;do redis-cli set key_${line} value1_${line};done

    image_1d01k1u0m1rldmt61nu13iq3o6m.png-9.9kB

     

    查看日志信息

    cat /data/redis/redis.log

     
    1. 1224:M 31 Dec 23:51:32.345 # CONFIG REWRITE executed with success.
    2. 1224:M 31 Dec 23:52:04.342 # CONFIG REWRITE executed with success.
    3. 1224:M 31 Dec 23:53:23.603 * 1 changes in 180 seconds. Saving...
    4. #发生了1万条数据改变180秒内,触发rdb存储
    5. 1224:M 31 Dec 23:53:23.613 * Background saving started by pid 1246
    6. 1246:C 31 Dec 23:53:23.636 * DB saved on disk
    7. 1246:C 31 Dec 23:53:23.637 * RDB: 6 MB of memory used by copy-on-write
    8. 1224:M 31 Dec 23:53:23.713 * Background saving terminated with success

    image_1d01k3hgiuj61m1q1pkn1o6c17oo13.png-40.5kB

     

    查看redis里有多少个键值(keys)

    redis-cli info

    image_1d01k5vgb3e41hp29mj15ri140o1g.png-76kB

     

    redis占用了多少内存

    redis-cli info memory

     
    1. used_memory:848480 --->数据占用内存大小
    2. used_memory_human:828.59K --->人性化的方式显示数据占用内存大小
    3. used_memory_rss:2179072 --->数据和进程占用大小
    4. used_memory_rss_human:2.08M --->人性化的方式显示数据和进程占用大小
    5. used_memory_peak:849456
    6. used_memory_peak_human:829.55K
    7. used_memory_peak_perc:99.89%
    8. used_memory_overhead:836222
    9. used_memory_startup:786592
    10. used_memory_dataset:12258
    11. used_memory_dataset_perc:19.81%
    12. total_system_memory:1021906944
    13. total_system_memory_human:974.57M
    14. used_memory_lua:37888
    15. used_memory_lua_human:37.00K
    16. maxmemory:0
    17. maxmemory_human:0B
    18. maxmemory_policy:noeviction
    19. mem_fragmentation_ratio:2.57 --->redis碎片率(频繁的写入删除,就会产生碎片,删的越多碎片就会越高),1-2之间代表没有内存碎片,小于1说明已经占用虚拟缓存,分配的内存不够用了
    20. mem_allocator:jemalloc-4.0.3
    21. active_defrag_running:0
    22. lazyfree_pending_objects:0

    image_1d01k6mg5s8nctp1rkq1tmji5s1t.png-58kB

     

    10.5 redis提供的bgsave命令能够立刻触发rdb存储,观察存储日志

    redis-cli save #会阻塞前端客户数据输入

    redis-cli bgsave #后台启动新进程进行rdb存储(不影响前端输入)

    image_1d01k9trg11p36nf4f01td81ic72q.png-13.7kB

     

    查看日志

    cat /data/redis/redis.log

     
    1. 1224:M 31 Dec 23:57:31.841 * DB saved on disk #save触发的日志信息
    2. 1224:M 31 Dec 23:57:39.821 * Background saving started by pid 11243 #bgsave触发的信息
    3. 11243:C 31 Dec 23:57:39.920 * DB saved on disk #bgsave触发的信息
    4. 11243:C 31 Dec 23:57:39.920 * RDB: 8 MB of memory used by copy-on-write #bgsave触发的信息
    5. 1224:M 31 Dec 23:57:39.985 * Background saving terminated with success #bgsave触发的信息

    image_1d01kb0mf5nu10sn8d3aht8v537.png-31.4kB

     

    十一,Redis的AOF存储方式

    redis的appendonly(aof)持久化存储会把用户每次的操作都记录到文件中(类似mysqlbinlog)

     

    11.1 动态开启或者关闭aof

    redis-cli config set appendonly yes #开启

    redis-cli config rewrite

    redis-cli config get appendonly #查询状态

    redis-cli config set appendonly no #关闭

    redis-cli config rewrite

    image_1d01m3r3np1t336kr8igo1gtg41.png-34.1kB

     

    11.2 写入数据,观察aof。多次运行,aof文件不断增大,rdb文件大小不变

     

    查看aof和rdb文件大小

    du -sh /data/redis/appendonly.aof

    du -sh /data/redis/dump.rdb

    image_1d01m4s291hms1ping45lk517754e.png-17.8kB

     

    写入数据

    for line in `seq -w 100`;do redis-cli set key_${line} value_${line};done

    image_1d01m6tb2q7emfq8ia44d1d055e.png-26kB

     

    查看aof和rdb文件大小

    redis-cli config set appendonly yes #开启

    redis-cli config rewrite

    du -sh /data/redis/appendonly.aof

    du -sh /data/redis/dump.rdb

    image_1d01mb67a16oit91efir9p1vgj6b.png-28kB

     

    11.3 重写aof文件,整理相同的key,写入最后的有效值

    BGREWRITEAOF 
    执行一个AOF文件重写操作。重写会创建一个当前AOF文件的体积优化版本 
    即使BGREWRITEAOF执行失败,也不会有任何数据丢失,因为旧的AOF文件在BGREWRITEAOF成功之前不会被修改。 
    重写操作只会在没有其他持久化工作在后台执行时被触发。 
    从Redis2.4开始,AOF重写由Redis自行触发,BGREWRITEAOF仅仅用于手动触发重写操作。

     

    清空aof文件

    > /data/redis/appendonly.aof

    du -sh /data/redis/appendonly.aof

    redis-cli bgrewriteaof #手动触发AOF重写

    du -sh /data/redis/appendonly.aof #redis里所有数据被重写入aof

    image_1d01mg8h2rk44t114slsuq132678.png-27.4kB

     

    清空aof文件

    > /data/redis/appendonly.aof

    du -sh /data/redis/appendonly.aof

    redis-cli set yunjisuan benet

    du -sh /data/redis/appendonly.aof

    cat /data/redis/appendonly.aof

     
    1. *2
    2. $6
    3. SELECT #select 0 表示切换到db0
    4. $1
    5. 0
    6. *3
    7. $3
    8. set #执行set yunjisuan benet
    9. $9
    10. yunjisuan
    11. $5
    12. benet

    image_1d01mrbsrqal1kmme7e1h24gqf7o.png-35.8kB


    redis-cli del yunjisuan benet

    cat /data/redis/appendonly.aof

     
    1. *2
    2. $6
    3. SELECT
    4. $1
    5. 0
    6. *3
    7. $3
    8. set
    9. $9
    10. yunjisuan
    11. $5
    12. benet
    13. *3
    14. $3
    15. del #执行del yunjisuan benet
    16. $9
    17. yunjisuan
    18. $5
    19. benet

    image_1d01msgkgach176rks911g42p18o.png-30.7kB

     

    重要提示

    我们发现虽然我们向redis添加了一个key,又删除了这个key。redis数据库从本质上来说并没有新增任何数据。但是aof文件仍旧把操作都给记录了。这样就会导致aof文件最终会非常大。所以aof文件的优化,就是让aof文件进行重写,只记录数据的增量部分。如此aof文件就小很多了。

     

    11.4 aof配置自动rewrite机制

     

    在默认配置文件里,默认存在

    redis-cli config get auto-aof-rewrite*

     
    1. auto-aof-rewrite-percentage 100 #默认100%,也就是aof增加一倍后考虑rewrite,两个条件要同时满足
    2. auto-aof-rewrite-min-size 64mb #默认64mb,也就是aof达到64M后考虑rewirte,两个条件要同时满足

    image_1d01n1icc1shv1e1h1jdi11slvoa19.png-16.6kB

     

    获取aof-rewrite配置

    redis-cli config get auto-aof-rewrite*

     
    1. 1) "auto-aof-rewrite-percentage"
    2. 2) "100"
    3. 3) "auto-aof-rewrite-min-size"
    4. 4) "67108864" #64MB(单位字节)

    image_1d01n1icc1shv1e1h1jdi11slvoa19.png-16.6kB

     

    进行aof自动重写测试

    redis-cli config set auto-aof-rewrite-min-size 100000

    redis-cli config get auto-aof-rewrite*

    redis-cli config rewrite

    > /data/redisappendonly.aof

    du -sh /data/redis/appendonly.aof

    for line in `seq -w 1000`;do redis-cli set key2_${line} value2_${line};done

    du -sh /data/redis/appendonly.aof

    for line in `seq -w 1000`;do redis-cli del key2_${line} value2_${line};done

    du -sh appendonly.aof

    du -sh appendonly.aof

    du -sh appendonly.aof

     
    1. [root@redis01 ~]# du -sh /data/redis/appendonly.aof
    2. 128K /data/redis/appendonly.aof
    3. [root@redis01 ~]# du -sh /data/redis/appendonly.aof
    4. 128K /data/redis/appendonly.aof
    5. [root@redis01 ~]# du -sh /data/redis/appendonly.aof
    6. 128K /data/redis/appendonly.aof
    7. [root@redis01 ~]# du -sh /data/redis/appendonly.aof
    8. 92K /data/redis/appendonly.aof #自动触发了aof重写机制

    image_1d01n96dmrdnnnba991vtq3tc2p.png-55.9kB


    image_1d01nqq1j1fgs1daotkb8su18k136.png-56.3kB


    image_1d01nrjlk1aenfm9pc6l6pbcq3j.png-68.4kB

     

    十二,Redis最大内存设置和删除算法

    redis-cli flushall #手动清空redis里所有数据

    image_1d01pqcaf1e5d1fj6b8g1sa11gm04g.png-14.7kB

     

    12.1 redis的键设置有效期,过期自动删除

    redis-cli set name yunjisuan

    redis-cli ttl name 
    (integer) -1 #-1代表key永久有效

    redis-cli expire name 10 #设定key 10s有效 
    (integer) 1

    redis-cli ttl name #查看key存活剩余时间 
    (integer) 8

    redis-cli ttl name 
    (integer) 6

    redis-cli ttl name 
    (integer) 2

    redis-cli ttl name 
    (integer) -2

    redis-cli get name #key已经被过期清除了 
    (nil)

    image_1d01ps1s2ltdd7o1alefu4b9d4t.png-34.5kB

     

    12.2 查看设置最大内存

     

    查看和设定最大内存限制

    redis-cli config get maxmemory

     
    1. 1) "maxmemory"
    2. 2) "0" #默认对内存无限制

    redis-cli config set maxmemory 1M #限制1M

    redis-cli config get maxmemory

     
    1. 1) "maxmemory"
    2. 2) "1000000"

    image_1d01q1kn81g1rgbj1hicl8n43g5a.png-21.6kB

     

    12.3 可选择的删除算法

     
    1. volatile-lru:
    2. 使用LRU算法删除键(key需要设置过期时间)
    3. volatile-random:
    4. 随机删除键(key需要设置过期时间)
    5. volatile-ttl:
    6. 删除ttl最小的键(key需要设置过期时间)
    7. allkeys-lru:
    8. 使用LRU算法删除键(所有key
    9. allkeys-random:
    10. 随机删除键(所有key
    11. noeviction:
    12. 不进行任何的操作,只返回错误,默认

    redis-cli config get maxmemory-policy #内存清理算法

     
    1. 1) "maxmemory-policy"
    2. 2) "noeviction" #默认noeviction

    image_1d01q5s371ebf12hthpq177kf3g5n.png-12.8kB

     

    12.4 模拟超过内存

    for line in `seq -w 2000`;do redis-cli set key_${line} value_${line};done

     
    1. 测试会发现报错
    2. (error) OOM command not allowed when used memory > 'maxmemory'.

    image_1d01qgeug1gr3c971vk2euf14nn64.png-86.5kB

     

    12.5 设置删除算法

     

    将删除算法设置为volatile-lru

    redis-cli config get maxmemory-policy

    redis-cli config set maxmemory-policy volatile-lru

    redis-cli config get maxmemory-policy

    redis-cli config rewrite

    image_1d01qkldcbppkrmt101f5l1uh56h.png-29.9kB

     

    算法测试

    redis-cli get key_0011

    redis-cli expire key_0011 3600

    redis-cli ttl key_0011

    redis-cli get key_0011

     
    1. 说明:由上述测试可以发现
    2. volatile-lru算法
    3. 当内存到了最大值以后,会优先删除有过期时间的key

    image_1d01qqav71r0m1li5to7ifk1pg46u.png-22.9kB

     

    十三,Redis禁用屏蔽危险命令

     

    13.1 redis禁用的命令

    FLUSHALL和FLUSHDB会清除redis的数据,比较危险 
    KEYS在键过多的时候使用会阻塞业务请求

     

    13.2 redis禁用危险命令配置代码如下(写入配置文件即可,此配置无法平滑更新)

    rename-command FLUSHALL "" #将命令改名成空 
    rename-command FLUSHDB "" #将命令改名成空 
    rename-command KEYS "" #将命令改名成空

     

    将配置加入redis.conf配置文件

    echo 'rename-command FLUSHALL ""' >> /usr/local/redis/conf/redis.conf

    echo 'rename-command FLUSHDB ""' >> /usr/local/redis/conf/redis.conf

    echo 'rename-command KEYS ""' >> /usr/local/redis/conf/redis.conf

    tail -3 /usr/local/redis/conf/redis.conf

    image_1d01qu2u81huour01g5884ej3j7b.png-32.1kB

     

    13.3 登陆redis,运行禁止命令进行测试

     

    重启redis-server

    redis-cli shutdown

    redis-server /usr/local/redis/conf/redis.conf

    netstat -antup | grep redis

    image_1d01qvi0pr906uhduk1v5u1orc7o.png-21.6kB

     

    测试被屏蔽的危险命令

    redis-cli flushall

    redis-cli flushdb

    redis-cli

     
    1. 127.0.0.1:6379> keys *
    2. (error) ERR unknown command `keys`, with args beginning with: `*`,

    image_1d01r134u1hv27o1b271eb6t785.png-27.3kB

     

    十四,Redis主从服务器环境的搭建

    在工作中redis主库不开启任何持久化,rdb和aof肯定是全关闭的,任何影响主库性能全都关闭。 
    持久化都是在从上做。

    主机名IP用途
    redis01 192.168.200.70 redis-master
    redis02 192.168.200.102 redis-slaveA
    redis03 192.168.200.103 redis-slaveB
     

    14.1 环境要求与redis基础编译部署调优

     

    操作系统环境要求

    cat /etc/redhat-release

    uname -r

    systemctl stop firewalld

    systemctl disable firewalld

    setenforce 0

    sestatus

    image_1d01tsag3u8qgqiukci8r1rag8i.png-20.9kB


    image_1d01tsseu1gup1vh65531dflub18v.png-20.2kB

     

    三台redis都进行如下编译过程(安装过程略)

    yum -y install wget gcc gcc-c++ make tar openssl openssl-devel cmake

    tar xf redis-4.0.11.tar.gz -C /usr/src/

    cd /usr/src/redis-4.0.11/

    make

    make MALLOC=jemalloc

    make PREFIX=/usr/local/redis install

    cd /usr/local/redis/

    ls

    mkdir -p /usr/local/redis/conf

    cp /usr/src/redis-4.0.11/redis.conf /usr/local/redis/conf/

    cp /usr/src/redis-4.0.11/sentinel.conf /usr/local/redis/conf/

    ln -s /usr/local/redis/bin/* /usr/local/bin/

    which redis-server

     

    三台都进行配置文件优化和简单的基础调优

    cd /usr/local/redis

    cp conf/redis.conf{,.bak}

    egrep -v "^$|^#" conf/redis.conf.bak > conf/redis.conf

    mkdir -p /data/redis/ #创建redis数据目录

    image_1d01u5lii1rpudsumtles01s589c.png-20.8kB

     

    修改配置文件

    cat -n conf/redis.conf | sed -n '1p;3p;4p;7p;9p;11p;21p'

     
    1. 1 bind 127.0.0.1
    2. 3 port 6379
    3. 4 tcp-backlog 511
    4. 7 daemonize no
    5. 9 pidfile /var/run/redis_6379.pid
    6. 11 logfile ""
    7. 21 dir ./

    image_1d01u6e6ql9p1fopk4u33n1uqfa6.png-23.7kB

     

    修改成以下设置

    vim conf/redis.conf

    cat -n conf/redis.conf | sed -n '1p;3p;4p;7p;9p;11p'

     
    1. 1 bind 0.0.0.0 #监听地址
    2. 3 port 6379 #监听端口
    3. 4 tcp-backlog 1024 #tcp连接数
    4. 7 daemonize yes #是否后台启动
    5. 9 pidfile /data/redis/redis.pid #pid存放目录
    6. 11 logfile "/data/redis/redis.log" #日志存放目录
    7. 21 dir /data/redis/ #工作目录

    image_1d01uhe8u1thktuk1e6k110e2qbb0.png-45.3kB

     

    进行基础调优设置

    echo "* - nofile 10240" >> /etc/security/limits.conf

    echo "net.core.somaxconn = 10240" >> /etc/sysctl.conf

    echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf

    sysctl -p

    echo never > /sys/kernel/mm/transparent_hugepage/enabled

    echo never > /sys/kernel/mm/transparent_hugepage/defrag

    echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local

    echo 'echo never > /sys/kernel/mm/transparent_hugepage/defrag' >> /etc/rc.local

    image_1d01um5uj14ag15l613dru1p1av99.png-50.1kB

     

    三台的redis-server都启动(上文实验已经启动过redis-master)

    redis-server /usr/local/redis/conf/redis.conf

    netstat -antup | grep redis

    image_1d01une5qu9k17bq9u41mca11lam.png-19.6kB


    image_1d01uo5v6n8qln6l3hhjm18fe13.png-18.5kB

     

    14.2 redis主从同步服务器搭建

    redis的主从同步,不用修改master任何配置 
    只需要在redis-slave上指定master的IP地址即可

     

    先启动redis-master,然后再在两个redis-slave上都进行如下操作

    redis-cli shutdown

    echo "SLAVEOF 192.168.200.70 6379" >> /usr/local/redis/conf/redis.conf

    > /data/redis/redis.log

    redis-server /usr/local/redis/conf/redis.conf

    netstat -antup | grep redis

    image_1d01usnco1k0j1mfbp561nq71f501g.png-37.5kB


    image_1d01uu9uv1ed6jq9106urt1pof1t.png-37.1kB

     

    14.3 主从同步日志分析(全量同步)

     

    查看redis-slave同步日志

    cat /data/redis/redis.log

     
    1. 18341:C 11 Aug 13:56:54.895 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    2. 18341:C 11 Aug 13:56:54.895 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=18341, just started
    3. 18341:C 11 Aug 13:56:54.895 # Configuration loaded
    4. _._
    5. _.-``__ ''-._
    6. _.-`` `. `_. ''-._ Redis 4.0.11 (00000000/0) 64 bit
    7. .-`` .-```. ```/ _.,_ ''-._
    8. ( ' , .-` | `, ) Running in standalone mode
    9. |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
    10. | `-._ `._ / _.-' | PID: 18342
    11. `-._ `-._ `-./ _.-' _.-'
    12. |`-._`-._ `-.__.-' _.-'_.-'|
    13. | `-._`-._ _.-'_.-' | http://redis.io
    14. `-._ `-._`-.__.-'_.-' _.-'
    15. |`-._`-._ `-.__.-' _.-'_.-'|
    16. | `-._`-._ _.-'_.-' |
    17. `-._ `-._`-.__.-'_.-' _.-'
    18. `-._ `-.__.-' _.-'
    19. `-._ _.-'
    20. `-.__.-'
    21. 18342:S 11 Aug 13:56:54.897 # Server initialized #服务器初始化
    22. 18342:S 11 Aug 13:56:54.897 * DB loaded from disk: 0.000 seconds #数据从磁盘加载0秒
    23. 18342:S 11 Aug 13:56:54.897 * Ready to accept connections #准备接受连接
    24. 18342:S 11 Aug 13:56:54.897 * Connecting to MASTER 192.168.200.165:6379 #链接到主192.168.200.165:6379
    25. 18342:S 11 Aug 13:56:54.897 * MASTER <-> SLAVE sync started #主从同步开始
    26. 18342:S 11 Aug 13:56:54.897 * Non blocking connect for SYNC fired the event. #非阻塞同步连接触发事件
    27. 18342:S 11 Aug 13:56:54.898 * Master replied to PING, replication can continue... #主应答,复制可以继续
    28. 18342:S 11 Aug 13:56:54.898 * Partial resynchronization not possible (no cached master) #部分同步不能(本机无缓存的主文件)
    29. 18342:S 11 Aug 13:56:54.899 * Full resync from master: e3adc85bd644e66bd1ee17b49c25e5e0491084d5:0 #进行全同步
    30. 18342:S 11 Aug 13:56:54.917 * MASTER <-> SLAVE sync: receiving 43606 bytes from master #从主接收43606字节
    31. 18342:S 11 Aug 13:56:54.917 * MASTER <-> SLAVE sync: Flushing old data #刷新旧数据
    32. 18342:S 11 Aug 13:56:54.917 * MASTER <-> SLAVE sync: Loading DB in memory #加载数据到内存
    33. 18342:S 11 Aug 13:56:54.918 * MASTER <-> SLAVE sync: Finished with success #同步完成
     

    查看redis-master同步日志

    cat /data/redis/redis.log

     
    1. #从192.168.200.163:6379请求同步
    2. 26003:M 11 Aug 14:14:55.342 * Slave 192.168.200.163:6379 asks for synchronization
    3. #从192.168.200.163:6379请求完整的重新同步
    4. 26003:M 11 Aug 14:14:55.342 * Full resync requested by slave 192.168.200.163:6379
    5. #master启动bgsave与目标的磁盘进行同步
    6. 26003:M 11 Aug 14:14:55.342 * Starting BGSAVE for SYNC with target: disk
    7. #后台保存rdb的进程的pid号为26128
    8. 26003:M 11 Aug 14:14:55.342 * Background saving started by pid 26128
    9. #rdb文件已经保存到了磁盘
    10. 26128:C 11 Aug 14:14:55.344 * DB saved on disk
    11. #rdb写时复制使用了0MB的内存
    12. 26128:C 11 Aug 14:14:55.344 * RDB: 0 MB of memory used by copy-on-write
    13. #后台保存成功
    14. 26003:M 11 Aug 14:14:55.414 * Background saving terminated with success
    15. #与从192.168.200.163:6379同步成功
    16. 26003:M 11 Aug 14:14:55.415 * Synchronization with slave 192.168.200.163:6379 succeeded

    image_1d01v04g67iqbg71ear1mhhmgc2a.png-118.7kB

     

    14.4 主从同步日志分析(部分同步)

     

    清空master日志

    > /data/redis/redis.log

    image_1d01vb9066vq13451hmn19ro1v232n.png-8.4kB

     

    清空slave日志,并shutdown在启动slave

    > /data/redis/redis.log

    redis-cli shutdown

    redis-server /usr/local/redis/conf/redis.conf

    netstat -antup | grep redis

    image_1d01vfc7gh451uic1bfemsdmmu34.png-31.9kB

     

    查看redis-slave日志

    cat /data/redis/redis.log

     
    1. 18342:S 11 Aug 14:23:27.678 # User requested shutdown...
    2. 18342:S 11 Aug 14:23:27.678 * Saving the final RDB snapshot before exiting.
    3. 18342:S 11 Aug 14:23:27.681 * DB saved on disk
    4. 18342:S 11 Aug 14:23:27.681 * Removing the pid file.
    5. 18342:S 11 Aug 14:23:27.681 # Redis is now ready to exit, bye bye...
    6. 18417:C 11 Aug 14:24:41.432 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    7. 18417:C 11 Aug 14:24:41.432 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=18417, just started
    8. 18417:C 11 Aug 14:24:41.432 # Configuration loaded
    9. _._
    10. _.-``__ ''-._
    11. _.-`` `. `_. ''-._ Redis 4.0.11 (00000000/0) 64 bit
    12. .-`` .-```. ```/ _.,_ ''-._
    13. ( ' , .-` | `, ) Running in standalone mode
    14. |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
    15. | `-._ `._ / _.-' | PID: 18418
    16. `-._ `-._ `-./ _.-' _.-'
    17. |`-._`-._ `-.__.-' _.-'_.-'|
    18. | `-._`-._ _.-'_.-' | http://redis.io
    19. `-._ `-._`-.__.-'_.-' _.-'
    20. |`-._`-._ `-.__.-' _.-'_.-'|
    21. | `-._`-._ _.-'_.-' |
    22. `-._ `-._`-.__.-'_.-' _.-'
    23. `-._ `-.__.-' _.-'
    24. `-._ _.-'
    25. `-.__.-'
    26. #服务器初始化
    27. 18418:S 11 Aug 14:24:41.434 # Server initialized
    28. #从磁盘加载旧数据用时0.001秒
    29. 18418:S 11 Aug 14:24:41.434 * DB loaded from disk: 0.001 seconds
    30. #由于之前是一个从库,利用主的参数合成一个主的缓存,这样就可以和主仅仅进行一部分的数据同步
    31. 18418:S 11 Aug 14:24:41.434 * Before turning into a slave, using my master parameters to synthesize a cached master: I may be able to synchronize with the new master with just a partial transfer.
    32. #准备接受连接
    33. 18418:S 11 Aug 14:24:41.434 * Ready to accept connections
    34. #连接到master192.168.200.165:6379
    35. 18418:S 11 Aug 14:24:41.435 * Connecting to MASTER 192.168.200.165:6379
    36. #主从同步开始
    37. 18418:S 11 Aug 14:24:41.435 * MASTER <-> SLAVE sync started
    38. #非阻塞同步连接触发事件
    39. 18418:S 11 Aug 14:24:41.435 * Non blocking connect for SYNC fired the event.
    40. #master应答,复制可以继续
    41. 18418:S 11 Aug 14:24:41.435 * Master replied to PING, replication can continue...
    42. #尝试进行部分同步(要求646f44ea31d2b057e919e08493f94e97e92007f2:911)
    43. 18418:S 11 Aug 14:24:41.435 * Trying a partial resynchronization (request 646f44ea31d2b057e919e08493f94e97e92007f2:911).
    44. #成功进行部分同步
    45. 18418:S 11 Aug 14:24:41.436 * Successful partial resynchronization with master.
    46. #master应答接受一个部分同步的请求
    47. 18418:S 11 Aug 14:24:41.436 * MASTER <-> SLAVE sync: Master accepted a Partial Resynchronization.
     

    查看redis-master日志

    cat /data/redis/redis.log

     
    1. #一个从库192.168.200.164:6379请求进行同步
    2. 26003:M 11 Aug 14:24:41.424 * Slave 192.168.200.164:6379 asks for synchronization
    3. #一个部分同步的请求来自192.168.200.164:6379,master已经接受请求,从偏移量为911处开始发送98字节的剩余数据
    4. 26003:M 11 Aug 14:24:41.424 * Partial resynchronization request from 192.168.200.164:6379 accepted. Sending 98 bytes of backlog starting from offset 911.

    image_1d01viobnqts1m15ofm1ncoa5h3h.png-141kB

     

    14.5 主从同步的停止

     

    清空从库日志并停止从库主从同步(只能在从库上执行)

    > /data/redis/redis.log

    redis-cli slaveof no one

    image_1d01vnh95hbscciiupgaf1fmg3u.png-13.2kB

     

    在次查看日志

    cat /data/redis/redis.log

     
    1. #将第二次复制的ID设置为646f44ea31d2b057e919e08493f94e97e92007f2,有效偏移量:3571。新的复制ID为efb9d2d393bb0bb4570a2f3144e7b2fbfc60b892
    2. 18418:M 11 Aug 14:55:15.723 # Setting secondary replication ID to 646f44ea31d2b057e919e08493f94e97e92007f2, valid up to offset: 3571. New replication ID is efb9d2d393bb0bb4570a2f3144e7b2fbfc60b892
    3. #与主库失去联系
    4. 18418:M 11 Aug 14:55:15.723 # Connection with master lost.
    5. #主从状态缓存断开
    6. 18418:M 11 Aug 14:55:15.723 * Caching the disconnected master state.
    7. #丢弃之前缓存的主的状态
    8. 18418:M 11 Aug 14:55:15.723 * Discarding previously cached master state.
    9. #主模式启用
    10. 18418:M 11 Aug 14:55:15.723 * MASTER MODE enabled (user request from 'id=4 addr=127.0.0.1:39500 fd=8 name= age=0 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=slaveof')

    image_1d01vpj7t26q15ht118i12bv1spd4b.png-52.1kB

     

    清空redis日志并恢复从库主从同步

    > /data/redis/redis.log

    redis-cli slaveof 192.168.200.70 6379

    image_1d01vs9k21e1l1cn11mu815sk1lgf4o.png-13.9kB

     

    查看slave日志

    cat /data/redis/redis.log

     
    1. 18418:S 11 Aug 15:02:40.330 * Before turning into a slave, using my master parameters to synthesize a cached master: I may be able to synchronize with the new master with just a partial transfer.
    2. 18418:S 11 Aug 15:02:40.330 * SLAVE OF 192.168.200.165:6379 enabled (user request from 'id=5 addr=127.0.0.1:39502 fd=7 name= age=0 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=slaveof')
    3. 18418:S 11 Aug 15:02:40.928 * Connecting to MASTER 192.168.200.165:6379
    4. 18418:S 11 Aug 15:02:40.928 * MASTER <-> SLAVE sync started
    5. 18418:S 11 Aug 15:02:40.928 * Non blocking connect for SYNC fired the event.
    6. 18418:S 11 Aug 15:02:40.929 * Master replied to PING, replication can continue...
    7. 18418:S 11 Aug 15:02:40.929 * Trying a partial resynchronization (request efb9d2d393bb0bb4570a2f3144e7b2fbfc60b892:3571).
    8. 18418:S 11 Aug 15:02:40.929 * Full resync from master: 646f44ea31d2b057e919e08493f94e97e92007f2:4186
    9. 18418:S 11 Aug 15:02:40.929 * Discarding previously cached master state.
    10. 18418:S 11 Aug 15:02:40.967 * MASTER <-> SLAVE sync: receiving 43607 bytes from master
    11. 18418:S 11 Aug 15:02:40.968 * MASTER <-> SLAVE sync: Flushing old data
    12. 18418:S 11 Aug 15:02:40.968 * MASTER <-> SLAVE sync: Loading DB in memory
    13. 18418:S 11 Aug 15:02:40.970 * MASTER <-> SLAVE sync: Finished with success

    image_1d01vso9b1m5b1vjs1gqgm6g11c955.png-102.6kB

     

    14.6 加密的主从同步

     

    (1)为redis-master平滑设置连接密码

    redis-cli config get requirepass

    redis-cli config set requirepass 'yunjisuan'

    redis-cli config get requirepass

    redis-cli -a yunjisuan config get requirepass

    redis-cli -a yunjisuan config rewrite

    image_1d020032t1p86o2s1jk915ee1qql5i.png-48.9kB

     

    查看从库日志信息

    cat /data/redis/redis.log

     
    1. #主从同步需要进行端口验证请求
    2. 18418:S 11 Aug 15:08:40.440 * (Non critical) Master does not understand REPLCONF listening-port: -NOAUTH Authentication required.
    3. 18418:S 11 Aug 15:08:40.440 * (Non critical) Master does not
    4. understand REPLCONF capa: -NOAUTH Authentication required.
    5. #部分同步不能,没有主的缓存
    6. 18418:S 11 Aug 15:08:40.440 * Partial resynchronization not possible (no cached master)
    7. #从主发来的意外回复:需要身份验证
    8. 18418:S 11 Aug 15:08:40.441 # Unexpected reply to PSYNC from master: -NOAUTH Authentication required.
    9. #进行同步重试
    10. 18418:S 11 Aug 15:08:40.441 * Retrying with SYNC...
    11. #主从复制终止:需要身份验证
    12. 18418:S 11 Aug 15:08:40.441 # MASTER aborted replication with an error: NOAUTH Authentication required.

    image_1d0202eu31o8k1e831ld31lqtu995v.png-242.9kB

     

    (2)为从库提供主从同步密码验证

     

    从服务器需要设置主从同步的认证密码

    redis-cli config get masterauth

    redis-cli config set masterauth "yunjisuan"

    redis-cli config get masterauth

    redis-cli config rewrite

    tail -1 /usr/local/redis/conf/redis.conf

    image_1d0207fn41ai1520oin47k6v66c.png-33.7kB

     

    查看从服务器日志

    cat /data/redis/redis.log

     
    1. 18418:S 11 Aug 15:14:07.854 * Connecting to MASTER 192.168.200.165:6379
    2. 18418:S 11 Aug 15:14:07.854 * MASTER <-> SLAVE sync started
    3. 18418:S 11 Aug 15:14:07.855 * Non blocking connect for SYNC fired the event.
    4. 18418:S 11 Aug 15:14:07.856 * Master replied to PING, replication can continue...
    5. 18418:S 11 Aug 15:14:07.858 * Partial resynchronization not possible (no cached master)
    6. 18418:S 11 Aug 15:14:07.860 * Full resync from master: 646f44ea31d2b057e919e08493f94e97e92007f2:4648
    7. 18418:S 11 Aug 15:14:07.886 * MASTER <-> SLAVE sync: receiving 43607 bytes from master
    8. 18418:S 11 Aug 15:14:07.888 * MASTER <-> SLAVE sync: Flushing old data
    9. 18418:S 11 Aug 15:14:07.889 * MASTER <-> SLAVE sync: Loading DB in memory
    10. 18418:S 11 Aug 15:14:07.893 * MASTER <-> SLAVE sync: Finished with success
    11. 18418:S 11 Aug 15:14:18.778 # CONFIG REWRITE executed with success.

    image_1d0208a9v18o1ob814k3ilrpl96p.png-226.2kB

     

    十五,使用Python操作Redis单例

     

    15.1 Python安装redis扩展

    yum -y install epel-release

    yum -y install python2-pip

    pip install redis

    image_1d020a22a11951gc81gru1ruf89876.png-95.2kB


    image_1d020bbm15tqc166mruao10d17j.png-156kB


    image_1d020fmqm841nfb19fb19mf1eo480.png-39.9kB

     

    15.2 利用python进行redis数据的读写

    python

     
    1. Python 2.7.5 (default, Apr 11 2018, 07:36:10)
    2. [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
    3. Type "help", "copyright", "credits" or "license" for more information.
    4. >>> import redis
    5. >>> r = redis.Redis(host='127.0.0.1',port=6379,password='yunjisuan',db=0)
    6. >>> r.set('key_test','value_test')
    7. True
    8. >>> value = r.get('key_test')
    9. >>> print (value)
    10. value_test
    11. >>> exit()

    image_1d020oik9mag1ia61vojifn162e8d.png-36kB


    redis-cli -a yunjisuan get key_test

     
    1. Warning: Using a password with '-a' option on the command line interface may not be safe.
    2. "value_test"

    image_1d020p06h16rbtna11dr1vru19qs8q.png-16kB

  • 相关阅读:
    LintCode2016年8月22日算法比赛----骰子求和
    LintCode2016年8月22日算法比赛----平面列表
    LintCode2016年8月22日算法比赛----将数组重新排序以构造最小值
    LintCode2016年8月22日算法比赛----克隆二叉树
    Leetcode算法比赛----Longest Absolute File Path
    Leetcode算法比赛----First Unique Character in a String
    vue运行报错Error: listen EADDRNOTAVAIL 192.168.1.105:8080
    vue使用lrz插件压缩图片
    <input type="file">原型难看
    vue创建全局变量以及全局方法
  • 原文地址:https://www.cnblogs.com/linyaonie/p/11238225.html
Copyright © 2020-2023  润新知