[kiosk@foundation0 ~]$ rht-vmctl start mastera Downloading virtual machine definition file for mastera. ######################################################################## 100.0% Downloading virtual machine disk image db100-mastera-vda.qcow2 ######################################################################## 100.0% Creating virtual machine disk overlay for db100-mastera-vda.qcow2 Downloading virtual machine disk image db100-mastera-vdb.qcow2 ######################################################################## 100.0% Creating virtual machine disk overlay for db100-mastera-vdb.qcow2 Starting mastera.
登录到实验主机上并配置基本环境
登录并配置相关安装源
[kiosk@foundation0 ~]$ ssh root@mastera0 Warning: Permanently added 'mastera0' (ECDSA) to the list of known hosts. root@mastera0's password: Last login: Sun Aug 14 08:19:23 2016 from 172.25.0.250 [root@mastera0 ~]# yum install wget -y Loaded plugins: product-id, search-disabled-repos, subscription-manager This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast rhel_dvd | 4.1 kB 00:00:00 Resolving Dependencies --> Running transaction check ---> Package wget.x86_64 0:1.14-10.el7_0.1 will be installed --> Finished Dependency Resolution
Dependencies Resolved
============================================================================================================================================================== Package Arch Version Repository Size ============================================================================================================================================================== Installing: wget x86_64 1.14-10.el7_0.1 rhel_dvd 546 k
[root@mastera0 yum.repos.d]# ls redhat.repo redis.repo rhel_dvd.repo [root@mastera0 yum.repos.d]# yum makecache Loaded plugins: product-id, search-disabled-repos, subscription-manager This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. redis | 2.9 kB 00:00:00 rhel_dvd | 4.1 kB 00:00:00 (1/3): redis/filelists_db | 1.6 kB 00:00:00 (2/3): redis/other_db | 2.4 kB 00:00:00 (3/3): redis/primary_db | 3.7 kB 00:00:00 Metadata Cache Created
安装相关rpm包
[root@mastera0 yum.repos.d]# yum install redis -y Loaded plugins: product-id, search-disabled-repos, subscription-manager This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. Resolving Dependencies --> Running transaction check ---> Package redis.x86_64 0:2.8.19-2.el7 will be installed --> Processing Dependency: libjemalloc.so.1()(64bit) for package: redis-2.8.19-2.el7.x86_64 --> Running transaction check ---> Package jemalloc.x86_64 0:3.6.0-1.el7 will be installed --> Finished Dependency Resolution
Dependencies Resolved
============================================================================================================================================================== Package Arch Version Repository Size ============================================================================================================================================================== Installing: redis x86_64 2.8.19-2.el7 redis 419 k Installing for dependencies: jemalloc x86_64 3.6.0-1.el7 redis 105 k
[root@mastera0 ~]# redis-cli -h 127.0.0.1 -p 6379 127.0.0.1:6379> set name mark OK
Ex: 设定name2键,值为jack , 追加赋值 tom
[root@mastera0 ~]# redis-cli -h 127.0.0.1 -p 6379 127.0.0.1:6379> set name2 "jack" OK 127.0.0.1:6379> get name2 "jack" 127.0.0.1:6379> append name2 " tom" (integer) 8 127.0.0.1:6379> get name2 "jack tom" 127.0.0.1:6379>
查询语法
语法
作用
keys [ keyname / * ]
列出已经生效的key
get keyname
查询某个key的值
Ex: 列出当前生效的key,并查询name的值
[root@mastera0 ~]# redis-cli -h 127.0.0.1 -p 6379 127.0.0.1:6379> keys * 1) name 2) "name2" 127.0.0.1:6379> get name "mark"
其他基础语法
语法
作用
del keyname
删除一个key
rename key newkey
重命名一个key
renamenx key newkey
重命名一个key (不会覆盖同名key)
exists key
判断一个键是否存在
move key dbnumber
将一个key移动到其他库
flushdb
清空当前库
type keyname
查询一个键的数据类型
Ex:删除name健,并判断name是否还存在
[root@mastera0 ~]# redis-cli -h 127.0.0.1 -p 6379 127.0.0.1:6379> del name (integer) 1 127.0.0.1:6379> exists name (integer) 0
Ex:在0号库中创建name键,并将它移动到2号库
[root@mastera0 ~]# redis-cli -h 127.0.0.1 -p 6379 127.0.0.1:6379> select 0 OK 127.0.0.1:6379> set name mark OK 127.0.0.1:6379> get name "mark" 127.0.0.1:6379> move name 2 (integer) 1 127.0.0.1:6379> get name (nil) 127.0.0.1:6379> select 2 OK 127.0.0.1:6379[2]> get name "mark"
Ex:清空当前库
[root@mastera0 ~]# redis-cli -h 127.0.0.1 -p 6379 127.0.0.1:6379> flushdb OK
Redis 常见数据类型
字符串
语法:Set key value [ex][px] [nx/xx]
参数
作用
EX seconds
设置指定的到期时间,单位为秒。
PX milliseconds
设置指定到期时间,单位为毫秒。
NX
设置键,如果它不存在。
XX
设置键,如果它已经存在。
——————-
——————-
Ex : 如果name键不存在则设置键name,值为mark,生效时间为10秒
[root@mastera0 ~]# redis-cli -h 127.0.0.1 -p 6379 127.0.0.1:6379> flushdb OK 127.0.0.1:6379> set name mark ex 10 nx OK 127.0.0.1:6379[2]> get name "mark" 127.0.0.1:6379[2]> get name (nil)
[root@mastera0 ~]# redis-cli -h 127.0.0.1 -p 6379 127.0.0.1:6379> set number 10 OK 127.0.0.1:6379> INCR number (integer) 11 127.0.0.1:6379> INCRBY number 10 (integer) 21 127.0.0.1:6379> DECR number (integer) 20 127.0.0.1:6379> DECRBY number 5 (integer) 15 127.0.0.1:6379> INCRBYFLOAT number 0.5 "15.5"
[root@mastera0 ~]# redis-cli -h 127.0.0.1 -p 6379 127.0.0.1:6379> multi OK 127.0.0.1:6379> set name1 zhangsan QUEUED 127.0.0.1:6379> set name2 (error) ERR wrong number of arguments for 'set' command 127.0.0.1:6379> set name3 lisi QUEUED 127.0.0.1:6379> exec (error) EXECABORT Transaction discarded because of previous errors. 127.0.0.1:6379> get name1
上述例子中可以看到因为语法错误而导致所有语句全部未执行。
例-error2:
[root@mastera0 ~]# redis-cli -h 127.0.0.1 -p 6379 127.0.0.1:6379> MULTI OK 127.0.0.1:6379> RPUSH testlist aa bb cc dd QUEUED 127.0.0.1:6379> LRANGE testlist 0 -1 QUEUED 127.0.0.1:6379> zadd testlist 10 ee QUEUED 127.0.0.1:6379> RPUSH testlist ff QUEUED 127.0.0.1:6379> exec 1) (integer) 4 2) 1) "aa" 2) "bb" 3) "cc" 4) "dd" 3) (error) WRONGTYPE Operation against a key holding the wrong kind of value 4) (integer) 5 127.0.0.1:6379> LRANGE testlist 0 -1 1) "aa" 2) "bb" 3) "cc" 4) "dd" 5) "ff"
可以看到zadd语句是有问题的,执行过程中zadd被忽略,其他语句仍然执行了。
另一个与事务处理相关的命令watch. 此命令可以用于监控某些键值,以决定是否运行事务。
例-watch
终端1:
[root@mastera0 ~]# redis-cli -h 127.0.0.1 -p 6379 127.0.0.1:6379> set key1 100 OK 127.0.0.1:6379> get key1 "100" 127.0.0.1:6379> watch key1 OK 127.0.0.1:6379> multi OK 127.0.0.1:6379> INCR key1 QUEUED
终端2:
[root@mastera0 ~]# redis-cli -h 127.0.0.1 -p 6379 127.0.0.1:6379> set key1 101 OK