robin 发表于 2018-11-6 12:38:31

redis 简介与安装

  1、redis简介:
  REmote DIctionary Server (redis)
  是一个基于key-value键值对的持久化数据库存储系统。redis和memcached缓存服务器很像,但是redis支持的数据 存储类型更丰富,包括string(字符串)、list(链表)、set(集合)和zest(有序集合)等。
  这些数据类型都支持push/pop、add/remove及取交集、并集合差集等丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与Memcahced缓存服务一样,为了保证效率,数据都是缓存在内存中提供服务。和Memcache不同的是,redis持久化缓存服务还会周期行的把更新的数据写入到磁盘以及把修改的操作记录追加到文件记录下来,比memcached更有优势的是,redis还支持master-slave(主从)同步,这点很类似关系型数据库MySQL.
  redis的出现, 在一定程度上了memcached这类key-value内存缓存服务的不足,在部分场合可以对关系数据库起到很好的补充作用。redis提供了Python、Ruby、Erlang,PHP客户端,使用方便。
  官网:http://www.redis.io/documentation
  http://www.redis.cn/
  2、redis 的优势:
  性能很高:    Redis 能支持超过100K+每秒的读写频率。(数据库也就几百的并发,上千已经很高了)
  丰富的数据类型:Redis支持二进制的strings、lists、Hashes、Sets及Ordered等数据类型
  原子:Redis的所有操作都是原子性的,同时Redis还支持对几个操作全并后的原子性特点,
  丰富的特性:Redis 还支持pubish、subecirbe,通知,key过期等等特性。
  redis支持异步主从复制
  3、redis 的数据类型(以下是常用的五种):
  String、Hash、List、Set、Sorte set
  4、Redis 的应用场景:
  传统的MYSQL+Memcache 的网站架构遇到的问题:
  mysql数据实际上是适合进行海量数据存储的,加上通过Memcached将热点数据存放到内存cache里,达到加速数据访问的目的,绝大部分公司都曾经使用过这样的架构,但随着业务数据量的不断增加,和访问量的持续增长,很多问题会暴露出来。(比如预热、重启操作)
  1、需要不断的对Mysql进行拆库拆表,Memcached也需要不断跟着扩容,扩容和文件工作占据大量开发运维时间。
  2、Memcached与Mysql数据库数据一致性是一个很大的问题
  3、Memcached数据命中率低或down机,会导致大量访问直接穿透数据库,导致Mysql无法支撑访问。
  4、跨机房cache同步一致性问题;
  redis最佳应用场景:
  1、redis最佳适用场景是全部数据in-memory,
  2、redis更多场景是作为Memcached的替代品来使用。(需开发配合)
  3、当需要出key/value之外的更多数据类型支持时,使用redis更合适。
  4、支持持久化。
  5、需要负载均衡的场景(redis主从同步)
  推荐:http://blog.nosqlfan.com/ 专家博客(乐视工作)
  #使用Redis bitmap进行活跃用户统计:http://blog.nosqlfan.com/html/3501.html
  5、redis的安装部署:
wget  
tar xf redis-2.8.9.tar.gz
  
cd redis-2.8.9
  
less README
  
less INSTALL
  
#查看安装文档
  
make MALLOC=jemallocmake
  
PREFIX=/application/redis-2.8.9 install
  
ln -s /application/redis-2.8.9/ /application/redis
  
LANG=EN
  
tree /application/redis/bin/
  
/application/redis/bin/
  
|-- redis-benchmark
  
#redis命令行操作工具,也可以使用telnet根据其纯文件协议来操作
  
|-- redis-check-aof
  
#redis性能测试工具,测试redis在你的系统及你的配置下的读写性能;
  
|-- redis-check-dump
  
#更新日志检查
  
|-- redis-cli
  
#用于本地数据库检查
  
|-- redis-server
  
#redis服务器的daemon启动程序
  
0 directories, 5 files
  6、配置并启动redis服务:
  a、配置环境变量:
  echo "PATH=/application/redis/bin/:$PATH" >>/etc/profile
  source /etc/profile
  b、创建redis 的配置文件目录:mkdir /application/redis/conf
  c、拷贝配置文件:cp -ap/tools/redis/redis-2.8.9/redis.conf/application/redis/conf/
  d、帮助信息查看:/application/redis/bin/redis-server--help
  Usage: ./redis-server
  ./redis-server - (read config from stdin)
  ./redis-server -v or --version
  ./redis-server -h or --help
  ./redis-server --test-memory
  Examples:
  ./redis-server (run the server with default conf)
  ./redis-server /etc/redis/6379.conf
  ./redis-server --port 7777
  ./redis-server --port 7777 --slaveof 127.0.0.1 8888
  ./redis-server /etc/myredis.conf --loglevel verbose
  Sentinel mode:
  ./redis-server /etc/sentinel.conf --sentinel
  如提示:内存不足时可执行如下语句
  sysctl vm.overcommit_memory=1#命令行执行时当前生效
  #或加入到 /etc/sysctl.conf
  启动: redis-server   /application/redis/conf &
  #指定配置文件
  检查:lsof -i :6379
  关闭:redis-clishutdown
  7、通过客户端测试redis服务
  redis-cli客户端帮助:
        redis-cli--help
Usage: redis-cli ]]  
            -h       Server hostname (default: 127.0.0.1).
  
            -p         Server port (default: 6379).
  
            -s         Server socket (overrides hostname and port).
  
            -a       Password to use when connecting to the server.
#客户端测试方法:  
# redis-cli
  
127.0.0.1:6379> help set
  

  
SET key value
  
summary: Set the string value of a key
  
since: 1.0.0
  
group: string
  

  
127.0.0.1:6379> set num002kong
  
OK
  
127.0.0.1:6379> get num002
  
"kong"
  
127.0.0.1:6379> quit
  
# redis-cli -h 119.10.115.82 -p 6379 set num003 ling
  
OK
  
# redis-cli -h 119.10.115.82 -p 6379 get num003
  
"ling"
  
# redis-cli del num002
  
(integer) 1
  
# redis-cli -h 119.10.115.82 -p 6379 get num002
  
(nil)
  
# telnet 127.0.0.1 6379
  
Trying 127.0.0.1...
  
Connected to 127.0.0.1.
  
Escape character is '^]'.
  
set num003 ling
  
+OK
  
get num003
  
$4
  
ling


页: [1]
查看完整版本: redis 简介与安装