Redis数据库
一、基础
1.yum 安装
[root@target ~]# yum install -y redis
[root@target ~]# systemctl start redis
#登录
[root@target ~]# redis-cli
#和mysql意义对大小写不敏感 支持补齐
2.Redis 命令工具
redis-server 等等
redis-server: #服务器启动命令
#可以开启新线程
#备份一个新的配置文件
[root@target ~]# cp /etc/redis.conf /etc/redis_6380.conf
#修改日志文件名字 并把对应端口号修改
[root@target ~]# vim /etc/redis_6380.conf
logfile /var/log/redis/redis_6380.log
#启动新线程 此时只能在前台运行
[root@target ~]# redis-server /etc/redis_6380.conf
#修改配置文件 让其在后台运行
[root@target ~]vim /etc/redis_6380.conf nf
daemonize yes
#重启检测
[root@target ~]# redis-server /etc/redis_6380.conf
[root@target ~]# ps aux | grep redis-server
redis 275736 0.0 1.5 76532 22388 ? Ssl 09:06 0:02 /usr/bin/redis-server 127.0.0.1:6379
root 288551 0.0 1.1 152312 17396 ? Ssl 09:49 0:00 redis-server 127.0.0.1:6380
root 292056 0.0 0.5 21988 7984 pts/0 S+ 10:00 0:00 grep --color=auto redis-server
[root@target ~]# killall redis-server
[root@target ~]# ps aux | grep redis-server
root 292615 0.0 0.5 21988 8008 pts/0 S+ 10:02 0:00 grep --color=auto redis-server
redis-benchmark
[root@target ~]# redis-benchmark
====== PING_INLINE ======
100000 requests completed in 1.41 seconds
50 parallel clients
3 bytes payload
keep alive: 1
host configuration "save": 3600 1 300 100 60 10000
host configuration "appendonly": no
multi-thread: no
Latency by percentile distribution:
0.000% <= 0.055 milliseconds (cumulative count 4)
50.000% <= 0.263 milliseconds (cumulative count 50857)
75.000% <= 0.463 milliseconds (cumulative count 75646)
.....
redis-cli 远程连接
[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 数据库常用命令
[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
#查看当前数据库中所有键
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??
###
批量设置键和查看键
#批量设置键和查看键
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 命令
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
#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
#存在追加 没有赋值
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
[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
[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关闭之后 重新开启 数据会丢失 因为原文件会被覆盖
三、密码
#修改配置文件 设置密码
[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"