[root@target ~]# systemctl stop redis
#修改IP地址
[root@target ~]# vim /etc/redis.conf
#bind 127.0.0.1 -::1
bind 192.168.157.168
[root@target ~]# systemctl start redis
[root@target ~]# redis-cli
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected>
#使用指定IP登录
[root@target ~]# redis-cli -h 192.168.157.168
192.168.157.168:6379>
#需要关闭 保护模式
[root@target ~]# vim /etc/redis.conf
protected-mode no
[root@target ~]# systemctl restart redis
3.Redis 数据库常用命令
shell复制代码
[root@target ~]# redis-cli -h 192.168.157.168
#存放数据
192.168.157.168:6379> set name zy
OK
#再次设置 覆盖原有值
192.168.157.168:6379> set name hzk
OK
192.168.157.168:6379> get name
"hzk"
#获取数据
192.168.157.168:6379> get name
"zy"
#如果没定义 就显示nil 表示空
192.168.157.168:6379> get age
(nil)
#多数据库间切换
#select 序号
使用 redis-cli 连接 Redis 数据库后,默认使用的是序号为 0 的数据库。
192.168.157.168:6379> select 1
OK
#move 移动
192.168.157.168:6379> keys *
1) "c"
2) "name"
192.168.157.168:6379> move c 1
(integer) 1
192.168.157.168:6379> get c
(nil)
192.168.157.168:6379> SELECT 1
OK
192.168.157.168:6379[1]> get c
"5"
#ttl 命令
192.168.157.168:6379> ttl d
(integer) -1
#expire
192.168.157.168:6379> EXPIRE d 10
(integer) 1
192.168.157.168:6379> ttl d
(integer) 6
192.168.157.168:6379> ttl d
(integer) 4
192.168.157.168:6379> ttl d
(integer) 1
192.168.157.168:6379> ttl d
(integer) -2
192.168.157.168:6379> get d
(nil)
key
shell复制代码
#查看当前数据库中所有键
192.168.157.168:6379[1]> keys *
(empty array)
192.168.157.168:6379[1]> SELECT 0
OK
192.168.157.168:6379> keys *
1) "key:__rand_int__"
2) "name"
#查看当前数据库中所有键
192.168.157.168:6379> KEYS *
#查看当前数据库中以 v 开头的数据
192.168.157.168:6379> KEYS v*
#查看当前数据库中以 v 开头后面包含任意一位的数据
192.168.157.168:6379> KEYS v?
#查看当前数据库中以 v 开头 v 开头后面包含任意两位的数据
192.168.157.168:6379> KEYS v??
###
批量设置键和查看键
shell复制代码
#批量设置键和查看键
192.168.157.168:6379> mset a 1 b 2
OK
192.168.157.168:6379> get a
"1"
192.168.157.168:6379> get b
"2"
192.168.157.168:6379> keys *
1) "c"
2) "name"
192.168.157.168:6379> mget c name
1) "5"
2) "hzk"
del 命令
shell复制代码
192.168.157.168:6379> keys *
1) "a"
2) "b"
3) "key:__rand_int__"
4) "name"
192.168.157.168:6379> del "key:__rand_int__"
(integer) 1
192.168.157.168:6379> del a
(integer) 1
192.168.157.168:6379> get a
(nil)
rename与renamenx
shell复制代码
#rename
#对已有 key 进行重命名。(覆盖)
192.168.157.168:6379> set c 22
OK
192.168.157.168:6379> RENAME c d
OK
192.168.157.168:6379> get c
(nil)
192.168.157.168:6379> get d
"22"
192.168.157.168:6379> set c 4
OK
192.168.157.168:6379> RENAME c d
OK
192.168.157.168:6379> get c
(nil)
192.168.157.168:6379> get d
"4"
#renamenx
#对已有 key 进行重命名,并检测新名是否存在,如果目标 key 存在则不进行重命名。(不覆盖)
192.168.157.168:6379> set c 5
OK
192.168.157.168:6379> RENAMENX c d
(integer) 0
192.168.157.168:6379> get c
"5"
192.168.157.168:6379> get d
"4"
append
shell复制代码
#存在追加 没有赋值
192.168.157.168:6379> get name
"hzk"
192.168.157.168:6379> APPEND name bbbb
(integer) 7
192.168.157.168:6379> get name
"hzkbbbb"
192.168.157.168:6379> get p
(nil)
192.168.157.168:6379> APPEND p 1
(integer) 1
192.168.157.168:6379> get p
"1"
二、redis持久化
RDB
shell复制代码
[root@target ~]# vim /etc/redis.conf
save 3600 1 300 100 60 10000
[root@target ~]# cd /var/lib/r
redis/ rpcbind/ rpm/ rpm-state/ rsyslog/
[root@target ~]# cd /var/lib/redis/
#保存在这个目录下
[root@target redis]# ls
dump.rdb
[root@target redis]# systemctl restart redis
AOF
shell复制代码
[root@nfs-server redis]# vim /etc/redis.conf
appendonly yes
[root@nfs-server redis]# cd appendonlydir/
[root@nfs-server appendonlydir]# ls
appendonly.aof.1.base.rdb appendonly.aof.1.incr.aof appendonly.aof.manifest
[root@nfs-server appendonlydir]# vim appendonly.aof.1.incr.aof
$1
0
*3
$3
set
$3
bbb
$3
222
*3
$3
set
....
#aof关闭之后 重新开启 数据会丢失 因为原文件会被覆盖
三、密码
shell复制代码
#修改配置文件 设置密码
[root@nfs-server appendonlydir]# vim /etc/redis.conf
requirepass 123.com
#重启
[root@nfs-server appendonlydir]# systemctl restart redis
#尝试登录
[root@target redis]# redis-cli -h 192.168.157.168
192.168.157.168:6379> keys *
#被拒绝
(error) NOAUTH Authentication required.
#auth命令 进行密码认证
192.168.157.168:6379> auth 123.com
OK
192.168.157.168:6379> keys *
1) "bbb"
2) "v"
3) "a"
4) "m"
5) "z"
6) "kk"
#或者 -a指密码
[root@target redis]# redis-cli -h 192.168.157.168 -a 123.com
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.157.168:6379> keys *
1) "bbb"
2) "v"
3) "a"
4) "m"
5) "z"