学习Redis第二课(Redis入门介绍)

第一部分:入门概述

        1.1 Redis是什么?

            Redis:Remote Dictionary Server(远程字典服务器),是完全开源免费的,用C语言编写的,遵守BSD协议,是一个高性能的(key/value)分布式内存数据库,基于内存运行并支持持久化NoSQL数据库,是当前最热门的NoSQL数据库之一,也被人们称为数据结构服务器

            Redis 与其他 key - value 缓存产品有以下三个特点,这也是它逐渐取代Memcache的原因。

           1)Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用

           2)Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存储

           3)Redis支持数据的备份,即master-slave模式的数据备份

         1.2 Redis能干什么

              1)内存存储和持久化:redis支持异步将内存中的数据写到硬盘上,同时不影响继续服务

              2)取最新N个数据的操作,如:可以将最新的10条评论的ID放在Redis的List集合里面

              3)模拟类似于HttpSessioin这种需要设定过期时间的功能

              4)发布订阅消息系统

              5)定时器、计数器

           1.3 去哪下

                 1.3.1 英文网站:http://redis.io/

                 1.3.2 中文网站:http://www.redis.cn/

            1.4 怎么玩

                 1.4.1 数据类型、基本操作和配置

                 1.4.2 持久化和复制,RDB/AOF

                 1.4.3 事务的控制(Redis部分支持事务)

                 1.4.4 复制

                 ........................

第二部分:Redis安装

        2.1 下载Redis安装包,这里我用的是redis-3.0.4.tar.gz,大家可以去:http://download.csdn.net/detail/u012453843/9696273这个地址进行下载

        2.2 将下载好的redis-3.0.4.tar.gz上传到我们的虚拟机上,虚拟机我用的是CentOS6.8,上传我用的工具是FileZilla,如下图所示。

 

        2.3 解压

        [root@itcast01 opt]# tar -zxvf redis-3.0.4.tar.gz

        2.4 make安装

        [root@itcast01 redis-3.0.4]# make

        [root@itcast01 redis-3.0.4]# make install

        安装过程中可能会碰到gcc命令未找到的问题,解决方法:yum install gcc-c++

        安装完gcc之后再执行make操作的话,会报Jemalloc/jemalloc.h:没有那个文件或目录,这时我们可以运行make distclean之后再make

        安装完之后,使用命令:gcc -v来检查是否安装成功

[root@itcast08 ~]# gcc -v
Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) 
[root@itcast08 ~]# 

        刚安装完Redis我们可以不执行Redis Test,如果要执行的话,可能会报TCL不存在。

        安装TCL的步骤:

        1)wget http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz

         2)sudo tar -zxvf tcl8.6.1-src.tar.gz -C /usr/local/

         3)cd /usr/local/tcl8.6.1/unix

         4)sudo ./configure

         5)sudo make

         6)make install

        2.5 安装完之后Redis所在的目录在哪儿呢?我们首先有必要看一张图,是说各个目录的作用的,如下所示。


         CentOS6.8安装完Redis之后,redis-server和redis-cli被放到了src目录下,如下所示

[root@itcast08 redis-3.0.4]# cd src/
[root@itcast08 src]# ls
adlist.c     anet.o       cluster.h  debug.c        intset.c   Makefile         object.o  redisassert.h       redis-cli.c     rio.c           sha1.c          syncio.c    t_zset.o      zmalloc.c
adlist.h     aof.c        cluster.o  debug.o        intset.h   Makefile.dep     pqsort.c  redis-benchmark     redis-cli.o     rio.h           sha1.h          syncio.o    util.c        zmalloc.h
adlist.o     aof.o        config.c   dict.c         intset.o   memtest.c        pqsort.h  redis-benchmark.c   redis.h         rio.o           sha1.o          testhelp.h  util.h        zmalloc.o
ae.c         asciilogo.h  config.h   dict.h         latency.c  memtest.o        pqsort.o  redis-benchmark.o   redis.o         scripting.c     slowlog.c       t_hash.c    util.o
ae_epoll.c   bio.c        config.o   dict.o         latency.h  mkreleasehdr.sh  pubsub.c  redis.c             redis-sentinel  scripting.o     slowlog.h       t_hash.o    valgrind.sup
ae_evport.c  bio.h        crc16.c    endianconv.c   latency.o  multi.c          pubsub.o  redis-check-aof     redis-server    sds.c           slowlog.o       t_list.c    version.h
ae.h         bio.o        crc16.o    endianconv.h   lzf_c.c    multi.o          rand.c    redis-check-aof.c   redis-trib.rb   sds.h           solarisfixes.h  t_list.o    ziplist.c
ae_kqueue.c  bitops.c     crc64.c    endianconv.o   lzf_c.o    networking.c     rand.h    redis-check-aof.o   release.c       sds.o           sort.c          t_set.c     ziplist.h
ae.o         bitops.o     crc64.h    fmacros.h      lzf_d.c    networking.o     rand.o    redis-check-dump    release.h       sentinel.c      sort.o          t_set.o     ziplist.o
ae_select.c  blocked.c    crc64.o    help.h         lzf_d.o    notify.c         rdb.c     redis-check-dump.c  release.o       sentinel.o      sparkline.c     t_string.c  zipmap.c
anet.c       blocked.o    db.c       hyperloglog.c  lzf.h      notify.o         rdb.h     redis-check-dump.o  replication.c   setproctitle.c  sparkline.h     t_string.o  zipmap.h
anet.h       cluster.c    db.o       hyperloglog.o  lzfP.h     object.c         rdb.o     redis-cli           replication.o   setproctitle.o  sparkline.o     t_zset.c    zipmap.o
       我们要启动Redis的话,需要修改redis-conf这个文件的参数

[root@itcast08 redis-3.0.4]# ls
00-RELEASENOTES  BUGS  CONTRIBUTING  COPYING  deps  INSTALL  Makefile  MANIFESTO  README  redis.conf  runtest  runtest-cluster  runtest-sentinel  sentinel.conf  src  tests  utils
[root@itcast08 redis-3.0.4]# vim redis.conf 
# Redis configuration file example


# Note on units: when memory size is needed, it is possible to specify
# it in the usual form of 1k 5GB 4M and so forth:
#
# 1k => 1000 bytes
# 1kb => 1024 bytes
# 1m => 1000000 bytes
# 1mb => 1024*1024 bytes
# 1g => 1000000000 bytes
# 1gb => 1024*1024*1024 bytes
#
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes


# When running daemonized, Redis writes a pid file in /var/run/redis.pid by
# default. You can specify a custom pid file location here.
pidfile /var/run/redis.pid


# Accept connections on the specified port, default is 6379.
# If port 0 is specified Redis will not listen on a TCP socket.
port 6379

       下面我们来启动redis-server

[root@itcast08 src]# ./redis-server /opt/redis-3.0.4/redis.conf 

       接着启动客户端
[root@itcast08 src]# ./redis-cli -p 6379

       设置一个变量k1,值是hello
127.0.0.1:6379> set k1 hello
OK

       获取我们刚才设置的k1的值
127.0.0.1:6379> get k1
"hello"
127.0.0.1:6379>

       如果我们想查看redis服务有没有启动的话,可以使用如下命令:      

[root@itcast08 src]# ps -ef|grep redis
root       3300      1  0 15:26 ?        00:00:02 ./redis-server *:6379
                    
root       3402   3008  0 15:52 pts/2    00:00:00 grep redis
[root@itcast08 src]# 

       如果想退出redis服务,可以进行如下操作:

127.0.0.1:6379> SHUTDOWN
not connected> exit
[root@itcast08 src]# ps -ef|grep redis
root       3406   3008  0 15:56 pts/2    00:00:00 grep redis
[root@itcast08 src]#

        好,本小节我们便一起学习到这里

        

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值