zoukankan      html  css  js  c++  java
  • redis安装使用

    1. 源码安装部署Redis缓存服务
    2. 常用Redis数据库操作指令
    3. 配置Redis主从服务器

    1 源码安装部署Redis缓存服务

    1.1 问题

    本案例要求先快速搭建好一台Redis服务器,并测试该缓存服务器:

    1. 设置变量test,值为123
    2. 查看变量test的值
    3. 设置计数器mycounter
    4. 对计数器mycounter进行增量加1操作

    1.2 方案

    使用源码包安装Redis服务器,使用redis-cli客户端工具连接Redis服务器并测试缓存数据库。

    使用redis-cli命令测试Redis服务时可以使用的命令列表如表-1所示。

    表-1 Redis命令列表

    1.3 步骤

    实现此案例需要按照如下步骤进行。

    步骤一:安装Redis服务器

    1)源码安装Redis软件

     
    1. [root@svr5 ~]# tar -xzf redis-3.0.6.tar.gz
    2. [root@svr5 ~]# cd redis-3.0.6
    3. [root@svr5 ~]# make
    4. [root@svr5 ~]# make install
    5. [root@svr5 ~]# ./utils/install_server.sh            //初始化
    6. Welcome to the redis service installer
    7. This script will help you easily set up a running redis server
    8. Please select the redis port for this instance: [6379]    //设置端口号,默认即可
    9. Selecting default: 6379
    10. Please select the redis config file name [/etc/redis/6379.conf] //配置文件
    11. Selected default - /etc/redis/6379.conf
    12. Please select the redis log file name [/var/log/redis_6379.log] //日志文件
    13. Selected default - /var/log/redis_6379.log
    14. Please select the data directory for this instance [/var/lib/redis/6379]
    15. //数据目录
    16. Selected default - /var/lib/redis/6379
    17. Please select the redis executable path [/usr/local/bin/redis-server]
    18. //Redis服务器软件存储路径
    19. Selected config:
    20. Port : 6379
    21. Config file : /etc/redis/6379.conf
    22. Log file : /var/log/redis_6379.log
    23. Data dir : /var/lib/redis/6379
    24. Executable : /usr/local/bin/redis-server
    25. Cli Executable : /usr/local/bin/redis-cli
    26. Is this ok? Then press ENTER to go on or Ctrl-C to abort.
    27. //确认信息是否正确,回车确认即可
    28. Copied /tmp/6379.conf => /etc/init.d/redis_6379
    29. Installing service...
    30. Successfully added to chkconfig!
    31. Successfully added to runlevels 345!
    32. Starting Redis server...
    33. Installation successful!
    34. [root@svr5 ~]# ls /etc/init.d/redis_6379         //查看启动脚本

    2)启用Redis服务并查看监听端口状态

     
    1. [root@svr5 ~]# /etc/init.d/redis_6379 restart
    2. [root@srv5 ~]# netstat -nutlp |grep redis
    3. tcp 0 0 0.0.0.0:6379    0.0.0.0:*    LISTEN 5749/redis-server *
    4. tcp 0 0 :::6379            :::*            LISTEN 5749/redis-server *

    步骤二:测试缓存数据库

    1)使用redis-cli测试数据库

     
    1. [root@srv5 ~]# redis-cli
    2. 127.0.0.1:6379> ping                        //测试服务器
    3. PONG
    4. 127.0.0.1:6379> set test 123                //设置变量
    5. OK
    6. 127.0.0.1:6379> get test                    //查看test值
    7. "123"
    8. 127.0.0.1:6379> INCR mycounter                //设置计数器mycounter
    9. (integer) 1
    10. 127.0.0.1:6379> INCR mycounter                //对计数器mycounter进行自增运算
    11. (integer) 2

    2 常用Redis数据库操作指令

    2.1 问题

    1. 沿用练习一,通过redis-cli工具,对Redis数据库各数据类型进行增删改查等操作,要求如下:
    2. 分别对Strings、Hash表、List列表三种数据类型进行增删改查等常见操作
    3. 设置数据缓存时间
    4. 清空所有数据
    5. 对数据库操作

    2.2 方案

    使用redis-cli客户端工具连接Redis服务器并测试缓存数据库。

    使用redis-cli命令测试Redis服务是可以使用的命令列表如表-2所示。

    表-2 Redis命令列表

    2.3 步骤

    实现此案例需要按照如下步骤进行。

    步骤一:常用Redis数据操作指令

    1)使用redis-cli测试数据库(字符串常见操作指南)
    
     
    [root@srv5 ~]# redis-cli
    127.0.0.1:6379> ping
    PONG
    127.0.0.1:6379> set string1 "hello the word"    //设置字符串变量
    OK
    127.0.0.1:6379> get string1                    //查看字符串变量
    "hello the word"
    127.0.0.1:6379> set string2 "hello" ex 5    //设置字符串变量,并设置过期时间为5秒
    OK
    127.0.0.1:6379> get string2                //查看字符串变量
    "hello"
    127.0.0.1:6379> get string2                //字符串过期后,查看该值为空
    (nil)
    127.0.0.1:6379> get string1
    "hello the word"
    127.0.0.1:6379> set string1 hello nx        //因为变量已经存在,则不执行set指令
    (nil)
    127.0.0.1:6379> set string1 hello xx        //变量存在,则执行set指令
    OK
    127.0.0.1:6379> get string1                //查看修改后string1的值
    "hello"
    127.0.0.1:6379> set string1 "hello the world"    //修改string1的值
    OK
    127.0.0.1:6379> get string1
    "hello the world"
    127.0.0.1:6379> setrange string1 6 "Redis"        //从第6个字符开始替换string1的值
    (integer) 15
    127.0.0.1:6379> get string1
    "hello Redisorld"
    127.0.0.1:6379> strlen string1                    //计算string1的长度
    (integer) 15
    127.0.0.1:6379> append string1 xxx                //对string1进行追加操作
    (integer) 18
    127.0.0.1:6379> get string1
    "hello Redisorldxxx"
    127.0.0.1:6379> append string1 " xxx"
    (integer) 22
    127.0.0.1:6379> get string1
    "hello Redisorldxxx xxx"
    127.0.0.1:6379> setbit string2 0 1                //按位设置string2的值,0位为1
    (integer) 0
    127.0.0.1:6379> setbit string2 1 1                //按位设置string2的值,1位为1
    (integer) 0
    127.0.0.1:6379> setbit string2 2 1
    (integer) 0
    127.0.0.1:6379> setbit string2 3 0
    (integer) 0
    127.0.0.1:6379> get string2                    //不可以查看所有的值
    "xe0"
    127.0.0.1:6379> bitcount string2                 //统计string2中1的个数
    (integer) 3
    127.0.0.1:6379> getbit string2 0                //查看string2第0位的值
    (integer) 1
    127.0.0.1:6379> getbit string2 1                //查看string2第1位的值
    (integer) 1
    127.0.0.1:6379> setbit bit1 0 1
    (integer) 0
    127.0.0.1:6379> setbit bit1 1 1                //设置bit1为11
    (integer) 0
    127.0.0.1:6379> setbit bit2 0 0
    (integer) 0
    127.0.0.1:6379> setbit bit2 1 1                //设置bit2为01
    (integer) 0
    127.0.0.1:6379> getbit bit1 0
    (integer) 1
    127.0.0.1:6379> getbit bit1 1
    (integer) 1
    127.0.0.1:6379> getbit bit2 1
    (integer) 1
    127.0.0.1:6379> getbit bit2 0
    (integer) 0
    127.0.0.1:6379> bitop and result bit1 bit2         //对bit1和bit2进行与运算
    (integer) 1
    127.0.0.1:6379> getbit result 0
    (integer) 0
    127.0.0.1:6379> getbit result 1                //运算后结果为01
    (integer) 1
    127.0.0.1:6379> decr string3                    //递减运算,初始值为0
    (integer) -1
    127.0.0.1:6379> decr string3
    (integer) -2
    127.0.0.1:6379> decr string3
    (integer) -3
    127.0.0.1:6379> decr string3
    (integer) -4
    127.0.0.1:6379> decr string3
    (integer) -5
    127.0.0.1:6379> set string4 10                    //自定义变量初始值为10
    OK
    127.0.0.1:6379> decr string4                    //对自定义变量进行递减运算
    (integer) 9
    127.0.0.1:6379> decr string4
    (integer) 8
    127.0.0.1:6379> decr string4
    (integer) 7
    127.0.0.1:6379> decrby string4 2                //对变量进行递减2运算
    (integer) 5
    127.0.0.1:6379> decrby string4 2
    (integer) 3
    127.0.0.1:6379> get string4
    "3"
    127.0.0.1:6379> set string5 "hello the world"        //设置字符串变量
    OK
    127.0.0.1:6379> getrange string5 0 4            //查看字串的第0至第4位
    "hello"
    127.0.0.1:6379> getrange string5 -3 -1            //查看字串的倒数第3位至倒数第1位
    "rld"
    127.0.0.1:6379> getset db mongodb                //获取变量的值,并赋新值
    (nil)                                        //没有改变量所有获取的值为空
    127.0.0.1:6379> get db                        //赋新值后,该变量db的值为mongodb
    "mongodb"
    127.0.0.1:6379> getset db redis                //获取变量的值,并赋新值
    "mongodb"
    127.0.0.1:6379> get db
    "redis"
    127.0.0.1:6379> incr page                        //对变量进行递增运算,初始值为0
    (integer) 1
    127.0.0.1:6379> incr page
    (integer) 2
    127.0.0.1:6379> incr page
    (integer) 3
    127.0.0.1:6379> incr page
    (integer) 4
    127.0.0.1:6379> set string6 10                    //设置字符串变量为10
    OK
    127.0.0.1:6379> incr string6                    //对变量进行递增运算
    (integer) 11
    127.0.0.1:6379> incr string6
    (integer) 12
    127.0.0.1:6379> incrby string6 2                //对变量进行递增2运算
    (integer) 14
    127.0.0.1:6379> incrby string6 2
    (integer) 16
    127.0.0.1:6379> incrby string6 2
    (integer) 18
    127.0.0.1:6379> incrby string6 2
    (integer) 20
    127.0.0.1:6379> set num 16.1                //设置浮点数变量
    OK
    127.0.0.1:6379> incrbyfloat num 1.1        //对浮点数进行递增1.1运算
    "17.2"
    127.0.0.1:6379> incrbyfloat num 1.1
    "18.3"
    127.0.0.1:6379> incrbyfloat num 1.1
    "19.4"
    127.0.0.1:6379> incrbyfloat num 1.1
    "20.5"
    2)Hash表常见操作指南
    
     
    127.0.0.1:6379> hset hkey google “www.g.cn”        
    //设置hash表hkey,google列的值为www.g.cn
    (integer) 1
    127.0.0.1:6379> hset hkey baidu “www.baidu.com”
    //设置hash表hkey,baidu列的值为www.baidu.com
    (integer) 1
    127.0.0.1:6379> hget hkey google        //查看hash表hkey中google列的值
    "www.g.cn"
    127.0.0.1:6379> hget hkey baidu        //查看hash表hkey中baidu列的值
    "www.baidu.com"
    127.0.0.1:6379> hsetnx hkey sina "www.sina.com"
    //如果sina列不存在,则创建该列并赋值
    (integer) 1
    127.0.0.1:6379> hget hkey sina
    "www.sina.com"
    127.0.0.1:6379> hsetnx hkey sina "www.sina.com" //sina列已经存在时,不执行赋值
    (integer) 0
    127.0.0.1:6379> hmset site google "www.g.cn" baidu "www.baidu.com"
    OK
    //一次性查看hash表site的多个列与值
    127.0.0.1:6379> hmget site google baidu
    1) "www.g.cn"
    2) "www.baidu.com"
    //一次性查看hash表site的多个列值
    127.0.0.1:6379> hgetall site                //查看site表中所有的列与值
    1) "google"
    2) "www.g.cn"
    3) "baidu"
    4) "www.baidu.com"
    127.0.0.1:6379> hdel site google            //删除site表中google列
    (integer) 1
    127.0.0.1:6379> hgetall site                //验证删除效果
    1) "baidu"
    2) "www.baidu.com"
    127.0.0.1:6379> hlen site                    //统计site表列的数量
    (integer) 1
    127.0.0.1:6379> hmset site google "www.g.cn" baidu "www.baidu.com"
    OK
    127.0.0.1:6379> hlen site                    //统计site表列的数量
    (integer) 2
    127.0.0.1:6379> hmset site google "www.g.cn" baidu "www.baidu.com" sina "www.sina.com"
    OK
    127.0.0.1:6379> hlen site
    (integer) 3
    127.0.0.1:6379> hexists site baidu            //判断site表中是否存在baidu列
    (integer) 1
    127.0.0.1:6379> hexists site google        //判断site表中是否存在google列
    (integer) 1
    127.0.0.1:6379> hexists site souhu            //判断site表中是否存在souhu列
    (integer) 0
    127.0.0.1:6379> hkeys site                    //查看site表的所有列
    1) "baidu"
    2) "google"
    3) "sina"
    127.0.0.1:6379> hvals site                    //查看site表中所有列的值
    1) "www.baidu.com"
    2) "www.g.cn"
    3) "www.sina.com"
    127.0.0.1:6379> hmget site google            //一次性查看site表中的多个列值
    1) "www.g.cn"
    127.0.0.1:6379> hmget site google baidu    //一次性查看site表中的多个列值
    1) "www.g.cn"
    2) "www.baidu.com"
    3)List列表常见操作指南
    
     
    127.0.0.1:6379> lpush list1 a b c            //创建列表并赋值
    (integer) 3
    127.0.0.1:6379> lpush list2 a                //创建列表并赋值
    (integer) 1
    127.0.0.1:6379> lpush list2 b                //给列表追加新值
    (integer) 2
    127.0.0.1:6379> lpush list2 c                //给列表追加新值
    (integer) 3
    127.0.0.1:6379> lrange list1 0 -1            
    //查看列表list1中的所有值,从0位到最后1位
    1) "c"
    2) "b"
    3) "a"
    127.0.0.1:6379> lrange list1 1 2            //查看列表中第1和2位的值
    1) "b"
    2) "a"
    127.0.0.1:6379> lrange list1 1 -1            //查看列表中第1至最后1位的值
    1) "b"
    127.0.0.1:6379> lpushx list2 a                //向列表中追加新值
    (integer) 4
    127.0.0.1:6379> lrange list2 0 -1            //查看所有的值
    1) "a"
    2) "c"
    3) "b"
    4) "a"
    127.0.0.1:6379> lpushx list2 t
    (integer) 5
    127.0.0.1:6379> lrange list2 0 -1
    1) "t"
    2) "a"
    3) "c"
    4) "b"
    5) "a"
    127.0.0.1:6379> lpushx list3 a            //仅当list3存在时,才插入新值
    (integer) 0
    127.0.0.1:6379> lrange list3 0 -1        //验证上一步操作结果
    (empty list or set)
    127.0.0.1:6379> lpop list2                
    //返回list2列表头元素数据,并将该值从列表中删除,key不存在则返回nil
    "t"
    127.0.0.1:6379> lrange list2 0 -1        //验证结果
    1) "a"
    2) "c"
    3) "b"
    4) "a"
    127.0.0.1:6379> lpop list2                //接续删除头部元素
    "a"
    127.0.0.1:6379> lrange list2 0 -1        //验证结果
    1) "c"
    2) "b"
    3) "a"
    127.0.0.1:6379> lpop list2
    "c"
    127.0.0.1:6379> lrange list2 0 -1
    1) "b"
    2) "a"
    127.0.0.1:6379> llen list2                    //统计list2中元素的个数
    (integer) 2
    127.0.0.1:6379> lpush list4 a b c a b c a b c d e f
    (integer) 12
    127.0.0.1:6379> lrange list4 0 -1
    1) "f"
    2) "e"
    3) "d"
    4) "c"
    5) "b"
    6) "a"
    7) "c"
    8) "b"
    9) "a"
    10) "c"
    11) "b"
    12) "a"
    127.0.0.1:6379> lrem list4 2 c            //从前往后删除list4中的2个c值
    (integer) 2
    127.0.0.1:6379> lrange list4 0 -1        //验证结果
    1) "f"
    2) "e"
    3) "d"
    4) "b"
    5) "a"
    6) "b"
    7) "a"
    8) "c"
    9) "b"
    10) "a"
    127.0.0.1:6379> lrem list4 -2 a        //从后往前删除list4中的2个a值
    (integer) 2
    127.0.0.1:6379> lrange list4 0 -1        //验证结果
    1) "f"
    2) "e"
    3) "d"
    4) "b"
    5) "a"
    6) "b"
    7) "c"
    8) "b"
    127.0.0.1:6379> lrem list4 0 b            //删除全部的b值
    (integer) 3
    127.0.0.1:6379> lrange list4 0 -1        //验证结果
    1) "f"
    2) "e"
    3) "d"
    4) "a"
    5) "c"
    127.0.0.1:6379> lindex list4 0                //返回list4中第0个值
    "f"
    127.0.0.1:6379> lindex list4 1                //返回list4中第1个值
    "e"
    127.0.0.1:6379> lindex list4 3
    "a"
    127.0.0.1:6379> lindex list4 -1            //返回list4中最后1个值
    "c"
    127.0.0.1:6379> lindex list4 -2            //返回list4中倒数第2个值
    "a"
    127.0.0.1:6379> lrange list4 0 -1
    1) "f"
    2) "e"
    3) "d"
    4) "a"
    5) "c"
    127.0.0.1:6379> ltrim list4 1 -1            
    //保留list4中1位至最后1位的值,其余的全部删除
    OK
    127.0.0.1:6379> lrange list4 0 -1        //验证结果
    1) "e"
    2) "d"
    3) "a"
    4) "c"
    127.0.0.1:6379> ltrim list4 0 3
    //保留list4中0位至最后3位的值,其余的全部删除
    OK
    127.0.0.1:6379> lrange list4 0 -1
    1) "e"
    2) "d"
    3) "a"
    4) "c"
    127.0.0.1:6379> ltrim list4 0 2
    OK
    127.0.0.1:6379> lrange list4 0 -1
    1) "e"
    2) "d"
    3) "a"
    127.0.0.1:6379> lset list4 0 test        //给list4的第0为插入值,值为test
    OK
    127.0.0.1:6379> lrange list4 0 -1        //验证结果
    1) "test"
    2) "d"
    3) "a"
    127.0.0.1:6379> linsert list4 before d t1    //在list4列表中d值得前面插入值,值为t1
    (integer) 4
    127.0.0.1:6379> lrange list4 0 -1            //验证结果
    1) "test"
    2) "t1"
    3) "d"
    4) "a"
    127.0.0.1:6379> linsert list4 after d t1    //在list4列表中d值得后面插入值,值为t1
    (integer) 5
    127.0.0.1:6379> lrange list4 0 -1
    1) "test"
    2) "t1"
    3) "d"
    4) "t1"
    5) "a"
    127.0.0.1:6379> blpop list4 3            
    //返回list4的头元素并删除该值,如果list4无任何元素,则等待3秒后退出
    1) "list4"
    2) "test"
    127.0.0.1:6379> lrange list4 0 -1        //验证结果
    1) "t1"
    2) "d"
    3) "t1"
    4) "a"
    127.0.0.1:6379> blpop list4 3
    1) "list4"
    2) "t1"            //删除了t1
    127.0.0.1:6379> lrange list4 0 -1        //验证结果
    1) "d"
    2) "t1"
    3) "a"
    127.0.0.1:6379> blpop list4 3
    1) "list4"
    2) "d"            //删除了d
    127.0.0.1:6379> lrange list4 0 -1
    1) "t1"
    2) "a"
    127.0.0.1:6379> blpop list4 3
    1) "list4"
    2) "t1"
    127.0.0.1:6379> blpop list4 3
    1) "list4"
    2) "a"
    127.0.0.1:6379> blpop list4 3                //因为list4已经无值,故3秒后退出
    (nil)
    (3.02s)
    127.0.0.1:6379> lpush list1 a b c
    (integer) 6
    127.0.0.1:6379> lpush list2 a b c
    (integer) 5
    127.0.0.1:6379> blpop list1 list2 3        
    //对多个列表返回头部并删除,无返回值后,等待3秒退出
    1) "list1"
    2) "c"            //删除list1中的c
    127.0.0.1:6379> blpop list1 list2 3
    1) "list1"
    2) "b"            //删除list1中的b
    127.0.0.1:6379> blpop list1 list2 3
    1) "list1"
    2) "a"
    127.0.0.1:6379> blpop list1 list2 3
    1) "list2"
    2) "c"            //删除list2中c
    127.0.0.1:6379> blpop list1 list2 3
    1) "list2"
    2) "b"            //删除lit2中的b
    127.0.0.1:6379> blpop list1 list2 3
    1) "list2"
    2) "a"
    127.0.0.1:6379> blpop list1 list2 3        //无值后,等待3秒退出
    (nil)
    (3.07s)
    127.0.0.1:6379> lpush list1 a b c
    (integer) 3
    127.0.0.1:6379> lrange list1 0 -1
    1) "c"
    2) "b"
    3) "a"
    127.0.0.1:6379> rpush list1 test            //往list1列表的尾部插入test
    (integer) 4
    127.0.0.1:6379> lrange list1 0 -1
    1) "c"
    2) "b"
    3) "a"
    4) "test"            //被插入的值test
    127.0.0.1:6379> rpushx list2 test            
    //仅当list2存在时,往末尾插入test,如list2不存在,则退出
    127.0.0.1:6379> lpush list6 a b c        //定义list6
    (integer) 3
    127.0.0.1:6379> lpush list7 1 2 3        //定义list7
    (integer) 3
    127.0.0.1:6379> lrange list6 0 -1        //查看list6
    1) "c"
    2) "b"
    3) "a"
    127.0.0.1:6379> lrange list7 0 -1        //查看list7
    1) "3"
    2) "2"
    3) "1"
    127.0.0.1:6379> rpoplpush list6 list7        
    //将list6中尾部的a,插入到list7的头部(顶部)
    "a"
    127.0.0.1:6379> lrange list6 0 -1        //验证结果
    1) "c"
    2) "b"
    127.0.0.1:6379> lrange list7 0 -1        //验证结果
    1) "a"
    2) "3"
    3) "2"
    4) "1"
    4)其他操作指南
    
     
    127.0.0.1:6379> set mykey "hello"            //定义字符串变量
    OK
    127.0.0.1:6379> get mykey                    //查看变量
    "hello"
    127.0.0.1:6379> del mykey                    //删除变量
    (integer) 1
    127.0.0.1:6379> get mykey                    //验证结果
    (nil)
    127.0.0.1:6379> exists mykey                //测试变量是否存在
    (integer) 0
    127.0.0.1:6379> set mykey "hello"            //定义变量即值
    OK
    127.0.0.1:6379> exists mykey                //测试变量是否存在
    (integer) 1
    127.0.0.1:6379> expire mykey 5                //设置变量过期时间为5秒
    (integer) 1
    127.0.0.1:6379> get mykey                    //查看有值
    "hello"
    127.0.0.1:6379> get mykey
    "hello"
    127.0.0.1:6379> get mykey                    //5秒后查看,无值
    (nil)
    127.0.0.1:6379> set mykey "hello"            //设置变量
    OK
    127.0.0.1:6379> expire mykey 10            //定义过期时间
    (integer) 1
    127.0.0.1:6379> persist mykey                //重新定义过期时间为,永久有效
    (integer) 1
    127.0.0.1:6379> get mykey
    "hello"
    127.0.0.1:6379> get mykey
    "hello"
    127.0.0.1:6379> ttl mykey
    (integer) -1        //永不过期
    127.0.0.1:6379> expire mykey 10        //定义过期时间
    (integer) 1
    127.0.0.1:6379> ttl mykey                //查看过期时间
    (integer) 9
    127.0.0.1:6379> ttl mykey
    (integer) 8
    127.0.0.1:6379> ttl mykey
    (integer) 7
    127.0.0.1:6379> ttl mykey
    (integer) 6
    127.0.0.1:6379> ttl mykey
    (integer) 5
    127.0.0.1:6379> ttl mykey
    (integer) 4
    127.0.0.1:6379> ttl mykey
    (integer) 3
    127.0.0.1:6379> ttl mykey
    (integer) 3
    127.0.0.1:6379> ttl mykey
    (integer) 2
    127.0.0.1:6379> ttl mykey
    (integer) 1
    127.0.0.1:6379> ttl mykey
    (integer) -2        //已经过期
    127.0.0.1:6379> get mykey        //查看mykey的值已经为空
    (nil)
    127.0.0.1:6379> set mykey "hello"
    OK
    127.0.0.1:6379> keys *        //查看数据库下所有数据
    1) "string6"
    2) "list7"
    3) "mykey"
    4) "string4"
    5) "db"
    6) "num"
    7) "result"
    8) "hkey"
    9) "string5"
    10) "string1"
    11) "bit1"
    12) "page"
    13) "bit2"
    14) "site"
    15) "string2"
    16) "list1"
    17) "string3"
    18) "list6"
    127.0.0.1:6379> keys li*
    1) "list7"
    2) "list1"
    3) "list6"
    127.0.0.1:6379> keys s*
    1) "string6"
    2) "string4"
    3) "string5"
    4) "string1"
    5) "site"
    6) "string2"
    7) "string3"
    127.0.0.1:6379> keys string[15]        //查看string1或string5
    1) "string5"
    2) "string1"
    127.0.0.1:6379> keys string[0-9]        //查看string0值9的数据
    1) "string6"
    2) "string4"
    3) "string5"
    4) "string1"
    5) "string2"
    6) "string3"
    127.0.0.1:6379> keys ?it*        //使用通配符所有数据
    1) "bit1"
    2) "bit2"
    127.0.0.1:6379> select 1        //进入1数据库,默认数据库为0
    OK
    127.0.0.1:6379[1]> keys *        //在新数据库中查看数据为空
    (empty list or set)
    127.0.0.1:6379[1]> set test "test"        //在数据库1中创建变量
    OK
    127.0.0.1:6379[1]> get test            //查看变量的值
    "test"
    127.0.0.1:6379[1]> select 2            //进入2数据库
    OK
    127.0.0.1:6379[2]> keys *                //查看所有数据
    (empty list or set)
    127.0.0.1:6379[2]> select 1
    OK
    127.0.0.1:6379[1]> keys *
    1) "test"
    127.0.0.1:6379[1]> select 0
    OK
    127.0.0.1:6379> keys *
    1) "string6"
    2) "list7"
    3) "mykey"
    4) "string4"
    5) "db"
    6) "num"
    7) "result"
    8) "hkey"
    9) "string5"
    10) "string1"
    11) "bit1"
    12) "page"
    13) "bit2"
    14) "site"
    15) "string2"
    16) "list1"
    17) "string3"
    18) "list6"
    127.0.0.1:6379> flushall        //清空所有数据
    OK
    127.0.0.1:6379> keys *        //验证结果
    127.0.0.1:6379[2]> select 0
    OK
    127.0.0.1:6379> set mykey "hello"
    OK
    127.0.0.1:6379> keys *
    1) "mykey"
    127.0.0.1:6379> select 1
    OK
    127.0.0.1:6379[1]> keys *
    (empty list or set)
    127.0.0.1:6379[1]> select 0
    OK
    127.0.0.1:6379> keys *
    1) "mykey"
    127.0.0.1:6379> move mykey 1        //将数据库0中的mykey变量移动至数据库1
    (integer) 1
    127.0.0.1:6379> keys *        //在数据库0中查看为空
    (empty list or set)
    127.0.0.1:6379> select 1        //进入数据库1
    OK
    127.0.0.1:6379[1]> keys *        //查看所有数据库
    1) "mykey"
    127.0.0.1:6379[1]> get mykey        //查看mykey的值
    "hello"
    127.0.0.1:6379[1]> rename mykey testkey        //对变量进行重命名
    OK
    127.0.0.1:6379[1]> keys *
    1) "testkey"
    127.0.0.1:6379[1]> get testkey
    "hello"
    127.0.0.1:6379[1]> set name1 tom
    OK
    127.0.0.1:6379[1]> set name2 jerry
    OK
    127.0.0.1:6379[1]> rename name1 name2
    OK
    127.0.0.1:6379[1]> keys *
    1) "testkey"
    2) "name2"
    127.0.0.1:6379[1]> get name2        //name2被覆盖
    "tom"
    127.0.0.1:6379[1]> set name1 tom
    OK
    127.0.0.1:6379[1]> set name2 jerry
    OK
    127.0.0.1:6379[1]> renamenx name1 name2        //仅在name2不存在才进行重命名
    (integer) 0
    127.0.0.1:6379[1]> keys *                        //name1和name2无变化
    1) "name1"
    2) "testkey"
    3) "name2"
    127.0.0.1:6379[1]> get name1
    "tom"
    127.0.0.1:6379[1]> get name2
    "jerry"
    127.0.0.1:6379[1]> lpush cost 1 8 7 2 5        //创建列表cost
    (integer) 5
    127.0.0.1:6379[1]> sort cost                    //对列表值进行排序
    1) "1"
    2) "2"
    3) "5"
    4) "7"
    5) "8"
    127.0.0.1:6379[1]> lpush test "about" "site"
    (integer) 2
    127.0.0.1:6379[1]> sort test alpha            //对列表进行排序,按字母顺序排列
    1) "about"
    2) "site"
    127.0.0.1:6379[1]> lpush test a f c e g i
    (integer) 8
    127.0.0.1:6379[1]> sort test alpha
    1) "a"
    2) "about"
    3) "c"
    4) "e"
    5) "f"
    6) "g"
    7) "i"
    8) "site"
    127.0.0.1:6379[1]> lpush test a f c e g i
    (integer) 14
    127.0.0.1:6379[1]> sort test alpha
    1) "a"
    2) "a"
    3) "about"
    4) "c"
    5) "c"
    6) "e"
    7) "e"
    8) "f"
    9) "f"
    10) "g"
    11) "g"
    12) "i"
    13) "i"
    14) "site"
    127.0.0.1:6379[1]> sort test alpha limit 0 3    //排序后,仅显示0至3位的值
    1) "a"
    2) "a"
    3) "about"
    127.0.0.1:6379[1]> sort test alpha limit 0 3
    1) "a"
    2) "a"
    3) "about"
    127.0.0.1:6379[1]> sort test alpha limit 0 3 desc    //倒序排列
    1) "site"
    2) "i"
    3) "i"
    127.0.0.1:6379[1]> sort test alpha limit 0 3 desc store cost2
    //将排序后的结果存入cost2
    (integer) 3
    127.0.0.1:6379[1]> keys *
    1) "test"
    2) "c"
    3) "cost"
    4) "a"
    5) "name1"
    6) "name2"
    7) "testkey"
    8) "b"
    9) "cost2"
    127.0.0.1:6379[1]> lrange cost2 0 -1        //查看cost2中的值
    1) "site"
    2) "i"
    3) "i"
    127.0.0.1:6379[1]> type cost2                //查看cost2的数据类型
    list
    127.0.0.1:6379[1]> set name tom
    OK
    127.0.0.1:6379[1]> type name                //查看name的数据类型
    string
    常见Redis数据操作指令

    3 配置Redis主从服务器

    3.1 问题

    本案例要求先快速搭建好两台Redis服务器,实现两台服务器之间自动数据同步,具体要求如下:

    1. 主服务器IP为192.168.4.10
    2. 从服务器IP为192.168.4.20
    3. 主服务器认证密码为password
    4. 测试主从数据是否正常通过

    3.2 方案

    通过修改Redis配置文件,实现两台服务器之间的自动主从同步功能,方案拓扑如图-1所示。

    图-1

    3.3 步骤

    实现此案例需要按照如下步骤进行。

    步骤一:配置主从服务器设置

    1)主服务器安装Redis

     
    1. [root@svr10 ~]# tar -xzf redis-3.0.6.tar.gz
    2. [root@svr10 ~]# cd redis-3.0.6
    3. [root@svr10 ~]# make
    4. [root@svr10 ~]# make install
    5. [root@svr10 ~]# cd utils/
    6. [root@svr10 ~]#./install_server.sh
    7. Welcome to the redis service installer
    8. This script will help you easily set up a running redis server
    9. Please select the redis port for this instance: [6379]
    10. Selecting default: 6379
    11. Please select the redis config file name [/etc/redis/6379.conf]
    12. Selected default - /etc/redis/6379.conf
    13. Please select the redis log file name [/var/log/redis_6379.log]
    14. Selected default - /var/log/redis_6379.log
    15. Please select the data directory for this instance [/var/lib/redis/6379]
    16. Selected default - /var/lib/redis/6379
    17. Please select the redis executable path [/usr/local/bin/redis-server]
    18. Selected config:
    19. Port : 6379
    20. Config file : /etc/redis/6379.conf
    21. Log file : /var/log/redis_6379.log
    22. Data dir : /var/lib/redis/6379
    23. Executable : /usr/local/bin/redis-server
    24. Cli Executable : /usr/local/bin/redis-cli
    25. Is this ok? Then press ENTER to go on or Ctrl-C to abort.
    26. Copied /tmp/6379.conf => /etc/init.d/redis_6379
    27. Installing service...
    28. Successfully added to chkconfig!
    29. Successfully added to runlevels 345!
    30. Starting Redis server...
    31. Installation successful!

    2)从服务器安装Redis

     
    1. [root@svr20 ~]# tar -xzf redis-3.0.6.tar.gz
    2. [root@svr20 ~]# cd redis-3.0.6
    3. [root@svr20 ~]# make
    4. [root@svr20 ~]# make install
    5. [root@svr20 ~]# cd utils/
    6. [root@svr20 ~]#./install_server.sh
    7. Welcome to the redis service installer
    8. This script will help you easily set up a running redis server
    9. Please select the redis port for this instance: [6379]
    10. Selecting default: 6379
    11. Please select the redis config file name [/etc/redis/6379.conf]
    12. Selected default - /etc/redis/6379.conf
    13. Please select the redis log file name [/var/log/redis_6379.log]
    14. Selected default - /var/log/redis_6379.log
    15. Please select the data directory for this instance [/var/lib/redis/6379]
    16. Selected default - /var/lib/redis/6379
    17. Please select the redis executable path [/usr/local/bin/redis-server]
    18. Selected config:
    19. Port : 6379
    20. Config file : /etc/redis/6379.conf
    21. Log file : /var/log/redis_6379.log
    22. Data dir : /var/lib/redis/6379
    23. Executable : /usr/local/bin/redis-server
    24. Cli Executable : /usr/local/bin/redis-cli
    25. Is this ok? Then press ENTER to go on or Ctrl-C to abort.
    26. Copied /tmp/6379.conf => /etc/init.d/redis_6379
    27. Installing service...
    28. Successfully added to chkconfig!
    29. Successfully added to runlevels 345!
    30. Starting Redis server...
    31. Installation successful!

    步骤二:配置主从服务器设置

    1)修改主服务器/etc/redis/6379.conf配置文件

     
    1. [root@svr10 ~]# vim /etc/redis/6379.conf
    2. requirepass redis123                            //设置服务器密码
    3. [root@svr10 ~]# /etc/init.d/redis_6379 restart    //重启服务

    2)修改从服务器/etc/redis/6379.conf配置文件

     
    1. [root@svr20 ~]# vim /etc/redis/6379.conf
    2. slaveof 192.1684.10 6379
    3. masterauth redis123
    4. [root@svr20 ~]# /etc/init.d/redis_6379 restart

    步骤三:客户端验证效果

     
    1. [root@client ~]# redis-cli –h 192.168.4.10 –a redis123    //登录主服务器设置数据
    2. 192.168.4.10:6379> set test 123456
    3. OK
    4. [root@client ~]# redis-cli –h 192.168.4.20            //登录主服务器查看数据同步效果
    5. 192.168.4.20:6379> set test
    6. 123456”
     
  • 相关阅读:
    word(2)--如何解决word中首行失效的问题?
    Google AK47 的设计者 阿米特 辛格(Amit Singhal)博士如何评价机器学习?
    技术人如何克服拖延症?
    解决WebService 中泛型接口不能序列化问题
    如何减少每次同步数据量
    C#开源系统大汇总
    log4.net
    C#自定义控件背景色透明的方法
    [p2p]UDP用打洞技术穿透NAT的原理与实现
    商品管理方案V2.0
  • 原文地址:https://www.cnblogs.com/linux985/p/11344273.html
Copyright © 2011-2022 走看看