Redis 官网:https://redis.io/
源码地址:https://github.com/redis/redis
Redis 在线测试:http://try.redis.io/
Redis 命令参考:http://doc.redisfans.com/
REmote DIctionary Server(远程字典服务器)
是完全开源免费的,用C语言编写的,遵守BSD协议,是一个高性能的(key/value)分布式内存数据库,基于内存运行 并支持持久化的NoSQL数据库,是当前最热门的NoSql数据库之一,也被人们称为数据结构服务器。
Redis 与其他 key - value 缓存产品有以下三个特点:
一、安装gcc依赖
yum install -y gcc
二、下载并解压安装包
wget http://download.redis.io/releases/redis-6.2.1.tar.gz
tar -zxvf redis-6.2.1.tar.gz
三、cd切换到redis解压目录下,执行编译
cd redis-6.2.1
make
四、安装并指定安装目录
[root@localhost redis-6.2.1]# make install PREFIX=/usr/local/redis
#复制一份配置文件到自己文件夹
[root@VM-4-15-centos ~]# mkdir myredis
[root@VM-4-15-centos ~]# cd redis-6.2.1
[root@VM-4-15-centos redis-6.2.1]# cp redis.conf /root/myredis/
在redis src目录下
[root@VM-4-15-centos redis-6.2.1]# cd src
[root@VM-4-15-centos src]# ./redis-server
16469:C 08 Aug 2021 00:33:43.312 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
16469:C 08 Aug 2021 00:33:43.312 # Redis version=6.2.1, bits=64, commit=00000000, modified=0, pid=16469, just started
16469:C 08 Aug 2021 00:33:43.312 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
16469:M 08 Aug 2021 00:33:43.313 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ‘‘-._
_.-`` `. `_. ‘‘-._ Redis 6.2.1 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ‘‘-._
( ‘ , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|‘` _.-‘| Port: 6379
| `-._ `._ / _.-‘ | PID: 16469
`-._ `-._ `-./ _.-‘ _.-‘
|`-._`-._ `-.__.-‘ _.-‘_.-‘|
| `-._`-._ _.-‘_.-‘ | http://redis.io
`-._ `-._`-.__.-‘_.-‘ _.-‘
|`-._`-._ `-.__.-‘ _.-‘_.-‘|
| `-._`-._ _.-‘_.-‘ |
`-._ `-._`-.__.-‘_.-‘ _.-‘
`-._ `-.__.-‘ _.-‘
`-._ _.-‘
`-.__.-‘
16469:M 08 Aug 2021 00:33:43.313 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
16469:M 08 Aug 2021 00:33:43.313 # Server initialized
16469:M 08 Aug 2021 00:33:43.313 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add ‘vm.overcommit_memory = 1‘ to /etc/sysctl.conf and then reboot or run the command ‘sysctl vm.overcommit_memory=1‘ for this to take effect.
16469:M 08 Aug 2021 00:33:43.313 * Ready to accept connections
启动成功
在有redis.conf的目录下
[root@VM-4-15-centos myredis]# vi redis.conf
把 daemonize no 改为 daemonize yes
然后指定redis.conf文件启动
./redis-server 【redis配置文件所在路径】
[root@VM-4-15-centos src]# ./redis-server /root/myredis/redis.conf
关闭redis进程
[root@VM-4-15-centos src]# ps -aux | grep redis
root 17297 0.2 0.5 195544 10068 ? Ssl 00:38 0:00 ./redis-server 127.0.0.1:6379
root 17494 0.0 0.0 112812 972 pts/1 R+ 00:39 0:00 grep --color=auto redis
使用kill命令杀死进程
[root@VM-4-15-centos src]# kill 17297
添加开机启动服务
[root@VM-4-15-centos src]# vi /etc/systemd/system/redis.service
复制内容
[Unit]
Description=redis-server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/redis/bin/redis-server /root/myredis/redis.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
ExecStart=/usr/local/redis/bin/redis-server 【redis配置文件所在路径】
[root@VM-4-15-centos src]# redis
127.0.0.1:6379> ping
PONG
表示这是一个正常的联通状态
[root@VM-4-15-centos src]# ps -aux | grep redis
root 17297 0.2 0.5 195544 10068 ? Ssl 00:38 0:00 ./redis-server 127.0.0.1:6379
root 17494 0.0 0.0 112812 972 pts/1 R+ 00:39 0:00 grep --color=auto redis
使用kill命令杀死进程
[root@VM-4-15-centos src]# kill 17297
单实例关闭
redis-cli shutdown
在终端关机
shutdown
在终端退出
exit
在redis安装目录下输入
[root@VM-4-15-centos bin]# redis-benchmark
类似数组下表从零开始,初始默认使用零号库,可在配置文件配置
select
用于切换数据库127.0.0.1:6379> select 15
OK
127.0.0.1:6379[15]>
dbsize
查看当前数据库的key的数量127.0.0.1:6379> dbsize
(integer) 4
keys *
用于查看当前库的key127.0.0.1:6379> keys *
1) "k1"
2) "myhash"
3) "key:__rand_int__"
4) "counter:__rand_int__"
keys k?相当于模糊查询
127.0.0.1:6379> keys k?
1) "k1"
flushdb
:清空当前库flushall
:通杀全部库统一密码管理,16个库都是同样密码,要么都OK要么一个也连接不上
Redis索引都是从零开始
为什么默认端口是6379:MDRZ
按 ESC,左下角就可以进行输入
:w
保存但不退出
:wq
保存并退出
:q
退出
:q!
强制退出,不保存
:e!
放弃所有修改,从上次保存文件开始再编辑命令历史
服务操作命令
systemctl start redis.service #启动redis服务
systemctl stop redis.service #停止redis服务
systemctl restart redis.service #重新启动服务
systemctl status redis.service #查看服务当前状态
systemctl enable redis.service #设置开机自启动
systemctl disable redis.service #停止开机自启动
原文:https://www.cnblogs.com/cg-ww/p/15176278.html