博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Redis 5.0
阅读量:6846 次
发布时间:2019-06-26

本文共 7455 字,大约阅读时间需要 24 分钟。

Redis 使用场景

1、性能:  不经常变动的数据放入缓存

我们在碰到需要执行耗时特别久,且结果不频繁变动的SQL,就特别适合将运行结果放入缓存。这样,后面的请求就去缓存中读取,使得请求能够迅速响应。 

 

2、迸发: 在高迸发的情况下使用缓存

在大并发的情况下,所有的请求直接访问数据库,数据库会出现连接异常。这个时候,就需要使用redis做一个缓冲操作,让请求先访问到redis,而不是直接访问数据库。

 

 

install Redis

wget http://download.redis.io/releases/redis-5.0.5.tar.gztar xzf redis-5.0.5.tar.gzcd redis-5.0.5make MALLOC=libcmake PREFIX=/usr/local/redis/ install

tree /usr/local/redis/bin/

/usr/local/redis/bin/
├── redis-benchmark                     // Redis性能测试工具
├── redis-check-aof                     // 对更新日志appendonly.aof 检查, 是否可用
├── redis-check-rdb
├── redis-cli                           // Redis命令行客户端操作工具
├── redis-sentinel -> redis-server
└── redis-server                        //Redis服务器的daemon启动程序

 

0 directories, 6 files

 

配置并启动

echo 'PATH=/usr/local/redis/bin/:$PATH' >> /etc/profile source /etc/profilewhich redis-server cp /usr/local/lib/redis-5.0.5/redis.conf /usr/local/redis/conf/ redis-server /usr/local/redis/conf/redis.conf  &               -- 启动 redis-cli shutdown                    -- 关闭

 

在生产环境中,需要将redis作为一个deamon进程去启动,每次系统启动的时候,redis服务会跟着启动。

进入redis目录下面,然后在进入utils目录,可以看到,有一个redis_init_script文件。

将这个文件拷贝到/etc/init.d/redis_6379下面

cp /usr/local/lib/redis-5.0.5/utils/redis_init_script /etc/init.d/redis.6379 vim /usr/local/redis/conf/redis.6379.conf   daemonize yes                 # 让redis以daemon进程运行   dir /usr/local/redis/var/      # 设置持久化文件的位置

 

让redis跟随系统启动

redis_6379脚本中,最上面,加入两行注释

# chkconfig:   2345 90 10# description:  Redis is a persistent key-value database

 

 

认证

修改配置文件并重启 vim redis.conf   requirepass password123 redis-cli shutdown redis-server /usr/local/redis/conf/redis.conf  & [root@caoc-1 conf]# redis-cli 127.0.0.1:6379> set k v                  # 设置k v 提示需要认证(error) NOAUTH Authentication required.127.0.0.1:6379> auth password123          # 认证OK127.0.0.1:6379> set k v            # 设置成功OK127.0.0.1:6379> get k"v" or  登录方式 redis-cli -a password

 

改名和禁用命令

rename-command CONFIG ""   # 禁用命令 rename-command set sets    # 改名命令

 

 

python 操作redis

pip install redis

# python

Python 2.7.5 (default, Oct 30 2018, 23:45:53)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import redis
>>> r = redis.Redis(host='127.0.0.1', port=6379, password='luciencao', db=0)
>>> pipe = r.pipeline()
>>> pipe.set('name','orange')
>>> pipe.execute()
[True]
>>> print(r.get('name'))
orange
>>>

 

 

 

Redis 主从复制

bind 0.0.0.0port 6379 appendonly yes ...

 

bind 0.0.0.0 port 6379 appendonly yes replicaof 192.168.1.19 6379masterauth password ...

 

 

Redis-clusters 配置

集群采用6个节点,  3个master 和分别对应的1个 salve ,  创建对应的7000-7005目录 

mkdir -p /home/redis-clusters/{
7000,7001,7002,7003,7004,7005}/{conf,var,run,log}

[root@redis redis-clusters]# tree .

.
├── 7000
│   ├── conf
│   │   └── redis.7000.conf
│   ├── log
│   │   └── redis.7000.log
│   ├── run
│   └── var
├── 7001
│   ├── conf
│   │   └── redis.7001.conf
│   ├── log
│   │   └── redis.7000.log
│   ├── run
│   └── var

...

 

集群配置文件  redis.7000.conf

需要区别的配置

port 7000pidfile /home/redis-clusters/7000/run/redis_7000.pidlogfile /home/redis-clusters/7000/log/redis.7000.logdir /home/redis-clusters/7000/var/masterauth luciencaorequirepass luciencaoappendonly yes

 

完整配置:

[root@caoc-1 redis-clusters]# egrep -v "^$|^#" 7000/conf/redis.7000.conf bind 0.0.0.0protected-mode yesport 7000tcp-backlog 511timeout 0tcp-keepalive 300daemonize yessupervised nopidfile /home/redis-clusters/7000/run/redis_7000.pidloglevel noticelogfile /home/redis-clusters/7000/log/redis.7000.logdatabases 16always-show-logo yessave 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump.rdbdir /home/redis-clusters/7000/var/masterauth luciencaoreplica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noreplica-priority 100requirepass luciencaolazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush noappendonly yesappendfilename "appendonly.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yeslua-time-limit 5000cluster-enabled yescluster-config-file nodes-7000.confcluster-node-timeout 15000slowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yes
View Code

 

启动服务

redis-server 7000/conf/redis.7000.conf redis-server 7001/conf/redis.7001.conf redis-server 7002/conf/redis.7002.conf redis-server 7003/conf/redis.7003.conf redis-server 7004/conf/redis.7004.conf redis-server 7005/conf/redis.7005.conf

启动集群

redis-cli --cluster  create 0.0.0.0:7000 0.0.0.0:7001 0.0.0.0:7002 0.0.0.0:7003 0.0.0.0:7004 0.0.0.0:7005 --cluster-replicas 1 -a luciencao

输入  yes 打印如下  

[OK] All 16384 slots covered   这表示集群中的 16384 个槽都有至少一个主节点在处理, 集群运作正常。
>>> Performing hash slots allocation on 6 nodes...Master[0] -> Slots 0 - 5460Master[1] -> Slots 5461 - 10922Master[2] -> Slots 10923 - 16383Adding replica 0.0.0.0:7004 to 0.0.0.0:7000Adding replica 0.0.0.0:7005 to 0.0.0.0:7001Adding replica 0.0.0.0:7003 to 0.0.0.0:7002>>> Trying to optimize slaves allocation for anti-affinity[WARNING] Some slaves are in the same host as their masterM: 814ff7a5c1e1affa45ff741018d38cc504114650 0.0.0.0:7000   slots:[0-5460] (5461 slots) masterM: 9a46b15737ba55d8c09cb776d6f82796aca35296 0.0.0.0:7001   slots:[5461-10922] (5462 slots) masterM: 9042f31ec13b6ace52833e2a5c83aa0178f7019e 0.0.0.0:7002   slots:[10923-16383] (5461 slots) masterS: 59d4a5f73ffb82d294e684ac07700eb5e0cd8b0a 0.0.0.0:7003   replicates 9a46b15737ba55d8c09cb776d6f82796aca35296S: 03bfaa6794ef1157b6f6b8e071d0575646105abc 0.0.0.0:7004   replicates 9042f31ec13b6ace52833e2a5c83aa0178f7019eS: db800ec138a395f05d68895ee3301835a412c314 0.0.0.0:7005   replicates 814ff7a5c1e1affa45ff741018d38cc504114650Can I set the above configuration? (type 'yes' to accept): yes>>> Nodes configuration updated>>> Assign a different config epoch to each node>>> Sending CLUSTER MEET messages to join the clusterWaiting for the cluster to join..>>> Performing Cluster Check (using node 0.0.0.0:7000)M: 814ff7a5c1e1affa45ff741018d38cc504114650 0.0.0.0:7000   slots:[0-5460] (5461 slots) master   1 additional replica(s)M: 9a46b15737ba55d8c09cb776d6f82796aca35296 127.0.0.1:7001   slots:[5461-10922] (5462 slots) master   1 additional replica(s)S: 59d4a5f73ffb82d294e684ac07700eb5e0cd8b0a 127.0.0.1:7003   slots: (0 slots) slave   replicates 9a46b15737ba55d8c09cb776d6f82796aca35296S: db800ec138a395f05d68895ee3301835a412c314 127.0.0.1:7005   slots: (0 slots) slave   replicates 814ff7a5c1e1affa45ff741018d38cc504114650S: 03bfaa6794ef1157b6f6b8e071d0575646105abc 127.0.0.1:7004   slots: (0 slots) slave   replicates 9042f31ec13b6ace52833e2a5c83aa0178f7019eM: 9042f31ec13b6ace52833e2a5c83aa0178f7019e 127.0.0.1:7002   slots:[10923-16383] (5461 slots) master   1 additional replica(s)[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.
View Code

 

 

 

转载于:https://www.cnblogs.com/blogscc/p/11023459.html

你可能感兴趣的文章
Java8新特性
查看>>
Swoole 实例三(Timer定时器)
查看>>
Windows下安装Redis
查看>>
Hyper-V Server 2008 R2安装、配置
查看>>
install_haproxy
查看>>
在PHP里通过工厂模式提高效率
查看>>
http://www.qingtin.com/
查看>>
MYSQL-字符校对规则探究
查看>>
堡垒机teleport之旅
查看>>
面向对象思想所遵循的五大设计原则
查看>>
警惕公司的破窗效应!
查看>>
oracle注册监听器,改变端口
查看>>
Java Serializable,序列化,串行化
查看>>
12306曝光sql注入漏洞,我试着发布解决方案
查看>>
策略模式
查看>>
CentOS 6.1 安装Nodejs及npm
查看>>
Vmware vSphere(Esxi)常见问题汇总
查看>>
HTTPClient模拟登陆21CN
查看>>
(转) Twisted :第六部分 抽象地利用Twisted
查看>>
php+mysql+html页面编码解决方案
查看>>