xywuyiba7 发表于 2018-11-3 08:58:05

Redis基础应用&高级应用

  案例1:源码安装Redis缓存服务
  本案例要求先快速搭建好一台Redis服务器,并测试该缓存服务器:
  设置变量test,值为123
  查看变量test的值
  设置计数器mycounter
  对计数器mycounter进行增量加1操作
  步骤一:安装Redis服务器
  1)源码安装Redis软件
  # tar -xzf redis-3.0.6.tar.gz
  # cd redis-3.0.6
  # make && make install
  # ./utils/install_server.sh            //初始化
  提示的默认配置均可回车确认
  Welcome to the redis service installer
  This script will help you easily set up a running redis server
  Please select the redis port for this instance:     //设置端口号,默认即可
  Selecting default: 6379
  Please select the redis config file name //配置文件
  Selected default - /etc/redis/6379.conf
  Please select the redis log file name //日志文件
  Selected default - /var/log/redis_6379.log
  Please select the data directory for this instance
  //数据目录
  Selected default - /var/lib/redis/6379
  Please select the redis executable path
  //Redis服务器软件存储路径
  Selected config:
  Port         : 6379
  Config file    : /etc/redis/6379.conf
  Log file       : /var/log/redis_6379.log
  Data dir       : /var/lib/redis/6379
  Executable   : /usr/local/bin/redis-server
  Cli Executable : /usr/local/bin/redis-cli
  Is this ok? Then press ENTER to go on or Ctrl-C to abort.
  //确认信息是否正确,回车确认即可
  Copied /tmp/6379.conf => /etc/init.d/redis_6379
  Installing service...
  Successfully added to chkconfig!
  Successfully added to runlevels 345!
  Starting Redis server...
  Installation successful!
  # ls /etc/init.d/redis_6379         //查看启动脚本
  # service redis_6379 status   查看状态
  # service redis_6379 stop
  # service redis_6379 start
  步骤二:测试缓存数据库r
  1)使用redis-cli测试数据库
  # redis-cli
  127.0.0.1:6379> ping                        //测试服务器
  PONG
  127.0.0.1:6379> set test 123                //设置变量
  OK
  127.0.0.1:6379> get test                  //查看test值
  "123"
  127.0.0.1:6379> INCR mycounter                //设置计数器mycounter
  (integer) 1
  127.0.0.1:6379> INCR mycounter                //对计数器mycounter进行自增运算
  (integer) 2
  2)常用Redis数据库操作指令
  # redis-cli
  127.0.0.1:6379> set first 'hello world'
  OK
  127.0.0.1:6379> get first
  "hello world"
  127.0.0.1:6379> setrange first 6 'tedu.cn'   //从偏移位6开始改写原变量,从0开始计量
  127.0.0.1:6379> get first
  "hello tedu.cn"
  127.0.0.1:6379> append test “sudan”            //向变量中追加
  127.0.0.1:6379> SETBIT chihiro 100 1         //设置2进制的第100位为1(从右往左数)
  127.0.0.1:6379> SETBIT chihiro 105 1         //设置2进制的第105位为1
  127.0.0.1:6379> bitcount chihiro               //显示chihiro为1的个数
  127.0.0.1:6379> set num1 100                   //num1=100
  127.0.0.1:6379> INCR num1                      //num1++
  127.0.0.1:6379> DECR num1                      //num1--
  127.0.0.1:6379> DECRBY num1 10               //num1=num1-10
  127.0.0.1:6379> INCRBY num1 10               //num1=num1+10
  127.0.0.1:6379> set hi “hello world”         //定义变量
  127.0.0.1:6379> strlen hi                      //显示该变量changdu
  127.0.0.1:6379> getrange hi 6 -1               //输出6到末尾(-1表示末尾)
  127.0.0.1:6379> getrange hi 6 10               //输出6到10的字节
  #输出均为world
  127.0.0.1:6379> set num2 10.5
  127.0.0.1:6379> incrbyfloat num2 0.3         //浮点数增量
  127.0.0.1:6379> incrbyfloat num2 -0.8
  127.0.0.1:6379> mget num1 num2               //同时查看多个变量
  127.0.0.1:6379> mset a 10 b "abc"            //同时设置多个变量
  127.0.0.1:6379> get a
  127.0.0.1:6379> get b
  hash表:Redis hash是一个string类型的field和value的映射表,一个key可对应多改field,一个field对应一个value。
  127.0.0.1:6379> hset site google "www.google.com"    //将hash表中gield值设置位value
  127.0.0.1:6379> hset site baidu "www.baidu.com"
  127.0.0.1:6379> hget site google                  //获取hash表中field的值
  127.0.0.1:6379> hget site baidu
  127.0.0.1:6379> hmset site2 tedu "www.tedu.cn" tarena "www.tarena.com"
  127.0.0.1:6379> hmget site2 tedu tarena
  127.0.0.1:6379> hkeys site                        //查看key中存在的value
  127.0.0.1:6379> hkeys site2
  127.0.0.1:6379> hgetall site                        //返回field和value
  127.0.0.1:6379> hvals site                            //返回site中所有的value
  127.0.0.1:6379> hdel site google                      //从site中删除google
  List列表
  127.0.0.1:6379> lpush mylist chensheng wuguang xiangyu
  127.0.0.1:6379> lrange mylist 0 -1
  127.0.0.1:6379> lpush mylist liubang
  127.0.0.1:6379> lrange mylist 0 -1
  127.0.0.1:6379> help @                  //表示按tab键
  127.0.0.1:6379> help @list
  127.0.0.1:6379> rpush mylist wanghong      //将一个或多个值value插入到列表key的表头
  127.0.0.1:6379> lrange mylist 0 -1
  127.0.0.1:6379> lset mylist 2 chenyuan
  127.0.0.1:6379> llen mylist                   //返回列表key的长度
  127.0.0.1:6379> lpop mylist                   //删除表头元素
  127.0.0.1:6379> rpop mylist
  127.0.0.1:6379> lindex mylist 1
  127.0.0.1:6379> get username               //查看username,没有就set建立一个
  127.0.0.1:6379> ttl username               //查看生存周期
  127.0.0.1:6379> persist username         //设置永不过期
  127.0.0.1:6379> expire username 30         //生存时间为30秒
  127.0.0.1:6379> del mylist               //删除mylist
  keys匹配
  keys                //显示所有key
  keys h?llo            //?匹配单个字节,可匹配hello,hpllo等
  keys hllo             //匹配多个字节,如haaaallo,hllo等
  keys hllo         //匹配单个字母字节
  keys hllo      //匹配单个字母或数字字节
  flushall               //清空所有数据

  select>  egslect 0
  127.0.0.1:6379> set username zhangsan      //在默认数据库0建立key
  127.0.0.1:6379> get username               //在数据库0能看到对应数据
  127.0.0.1:6379> select 1                     //进入数据库1
  127.0.0.1:6379> get username            //没有对应的key
  127.0.0.1:6379> select 0
  127.0.0.1:6379> move username 1            //将username移到数据库1
  127.0.0.1:6379> get username               //数据库0中没有username的数据
  127.0.0.1:6379> select 1
  127.0.0.1:6379> get username            //数据库1中能找到
  rename key newkey   //给可以改名为newkey,newkey已存在时,则覆盖其值
  127.0.0.1:6379> lpush mylist 10 2 38 69 42
  (integer) 5
  127.0.0.1:6379> sort mylist         //排序(升序),不会改变mylist
  1) "2"
  2) "10"
  3) "38"
  4) "42"
  5) "69"
  127.0.0.1:6379> lrange mylist 0 -1
  1) "42"
  2) "69"
  3) "38"
  4) "2"
  5) "10"
  127.0.0.1:6379> sort mylist desc    //降序
  1) "69"
  2) "42"
  3) "38"
  4) "10"
  5) "2"
  lpush names zhangsan lisi bob alice
  sort names                        //语法错误
  sort names alpha                  //按字母顺序排序
  sort names alpha limit 1 2         //按顺序从1开始取2个数据(开头是0)
  sort mylist store mylist2          //将mylist排序后,另存为mylist2
  ########################################################################
  配置Redis主从服务器
  要求先快速搭建好两台Redis服务器,实现两台服务器之间自动数据同步,具体要求如下:
  主服务器IP为192.168.2.100
  从服务器IP为192.168.2.200
  主服务器认证密码为redis123
  测试主从数据是否正常通过
  步骤一:配置主从服务器设置
  1)主服务器安装Redis
  上面案例的服务器可做主服务器
  2)从服务器安装Redis
  # yum -y install gcc gcc-c++            //安装依赖包
  # tar -xzf redis-3.0.6.tar.gz
  # cd redis-3.0.6
  # make
  # make install
  # cd utils/
  #./install_server.sh                  //初始化环境
  步骤二:配置主从服务器设置
  1)修改主服务器/etc/redis/6379.conf配置文件
  # vim /etc/redis/6379.conf
  requirepass redis123                               //设置服务器密码
  # /etc/init.d/redis_6379 restart       //重启服务
  2)修改主服务器的启动脚本,添加服务器密码
  # vim/etc/init.d/redis_6379
  $CLIEXEC –a redis123-p $REDISPORT shutdown
  3)修改从服务器/etc/redis/6379.conf配置文件
  # vim /etc/redis/6379.conf
  slaveof 192.168.4.100 6379                     //在从服务器上设定主服务器IP及端口号
  masterauth redis123
  # /etc/init.d/redis_6379restart
  步骤三:验证效果
  1) 主服务器操作
  # redis-cli –h 192.168.4.100 –a redis123      //登录主服务器设置数据
  192.168.4.100:6379> set name harry
  OK
  2) 从服务器操作
  # redis-cli –h 192.168.4.200            //登录主服务器查看数据同步效果
  192.168.4.200:6379> get name
  “harry”

页: [1]
查看完整版本: Redis基础应用&高级应用