Redis(一)

Redis(一)

本文分为以下几个部分

介绍

Linux安装

远程连接Redis

其他配置

总结

介绍

Redis是一种非关系型数据库(NoSQL)

NoSQL

NoSQL = not only SQL (不仅仅是sql),并不是没有SQL,泛指非关系型数据库。(关系型数据库为表格类型)

特点

  1. 方便扩展(数据之间没有关系,很好扩展)
  2. 大数据高性能(Redis 一秒写 8w 次,读取 11w 次,NoSQL 的缓存记录级,是一种细粒度的缓存,性能高)
  3. 数据类型多样(不需要事先设计数据库,随取随用)

image-20210715094031871

  • 从上面的话中,看到Redis官方是不支持windows版本的

Redis 为单线程

Redis 将所有数据全部放在内存中,使用单线程去操作效率最高,多线程CPU上下文切换会耗时间,对于内存来说,如果没有上下文切换就是效率最高!

Linux安装

将官网下载的Redis压缩包传至linux服务器。

image-20210715094751003

使用 tar -zxvf redis-6.2.4.tar.gz 命令解压文件(后面为自己redis的压缩包名称)

image-20210715095026361

切换至Redis解压目录,使用make命令进行编译

image-20210715095336826

等待完成,切换到该目录的src目录下,启动redis,输入redis-server

[root@qundd src]# redis-server 
2223:C 15 Jul 2021 10:04:40.035 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2223:C 15 Jul 2021 10:04:40.035 # Redis version=6.2.4, bits=64, commit=00000000, modified=0, pid=2223, just started
2223:C 15 Jul 2021 10:04:40.035 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
2223:M 15 Jul 2021 10:04:40.035 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.2.4 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 2223
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

2223:M 15 Jul 2021 10:04:40.036 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2223:M 15 Jul 2021 10:04:40.036 # Server initialized
2223:M 15 Jul 2021 10:04:40.036 # 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.
2223:M 15 Jul 2021 10:04:40.036 * Ready to accept connections

默认端口为6379,新建一个窗口,同样切换至redis的src目录下,启动redis-cli

[root@qundd src]# redis-cli 
127.0.0.1:6379> ping	#输入ping,显示pong,则说明连接成功
PONG
127.0.0.1:6379> exit
[root@qundd src]# 

远程连接Redis

使用连接网络、端口号方式连接redis-server

#redis-cli -h IP地址 -p 端口号 -a redis的密码
[root@qundd src]# redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> exit
[root@qundd src]# 

ifconfig 查看linux ip地址,将上方127.0.0.1换成ip地址,模拟外网访问,发现无法ping通

[root@qundd src]# redis-cli -h 172.31.43.147 -p 6379
172.31.43.147:6379> ping
(error) DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
172.31.43.147:6379> exit
[root@qundd src]# 

修改配置文件,更换地址等信息,切换至Redis目录(src上一层),里面有个官方的 redis.conf (也可以自己写一个配置文件)配置文件。

在network中,有一个

bind 127.0.0.1 -::1

注释之,修改成

#bind 127.0.0.1 -::1
bind 127.0.0.1 172.31.43.147	#172.31.43.147 为我的ip,需要自己修改成自己的ip

wq保存,使用配置文件启动redis,也可以切换至src目录下,笔者懒得切换,直接使用src下的redis-server

#redis-server 配置文件
[root@qundd redis-6.2.4]# src/redis-server /root/redis-6.2.4/redis.conf
2242:C 15 Jul 2021 10:19:13.037 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2242:C 15 Jul 2021 10:19:13.038 # Redis version=6.2.4, bits=64, commit=00000000, modified=0, pid=2242, just started
2242:C 15 Jul 2021 10:19:13.038 # Configuration loaded
2242:M 15 Jul 2021 10:19:13.038 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.2.4 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 2242
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

2242:M 15 Jul 2021 10:19:13.039 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2242:M 15 Jul 2021 10:19:13.039 # Server initialized
2242:M 15 Jul 2021 10:19:13.039 # 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.
2242:M 15 Jul 2021 10:19:13.039 * Ready to accept connections

再次使用redis-cli连接

[root@qundd src]# redis-cli -h 172.31.43.147 -p 6379
172.31.43.147:6379> ping
PONG
172.31.43.147:6379> exit

其他配置

部分后面文章会了解

单位

image-20210713184734887

  1. 配置文件Unit,大小写不敏感

INCLUDES 包含

image-20210713184834287

  1. 可以将多个redis conf组合成一个,就好比spring的import,jsp include

NETWORK 网络

bind 127.0.0.1 172.31.43.147	#绑定ip
protected-mode yes	#保护模式,默认yes
port 6379	#端口设置

GENERAL 通用

daemonize yes后台方式运行

daemonize no	#是否以守护(后台)进程方式运行,默认为no,需要手动改为yes
pidfile /var/run/redis_6379.pid	#如果以后台方式运行,需要指定pid进程文件

# 日志
# Specify the server verbosity level.
# This can be one of:
# debug (a lot of information, useful for development/testing)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably)
# warning (only very important / critical messages are logged)
loglevel notice
logfile ""	#日志文件位置
databases 16	#数据库数量
always-show-logo no	#是否总是显示logo

SNAPSHOTTING 快照

持久化,默认为rdb持久化

在规定的时间内,执行了多少次操作,则会持久化到文件 .rdb .aof

redis 是内存数据库,如果没有持久化,断电即失

# 如果3600s内,如果至少有 1个 key进行了修改,我们就进行持久化操作,3600s才会去保存
# save 3600 1
# 如果300s内,如果至少有 100个 key进行了修改,我们就进行持久化操作,300s才会去保存
# save 300 100
# 如果60s内,如果至少有 10000个 key进行了修改,我们就进行持久化操作
# save 60 10000
stop-writes-on-bgsave-error yes	#持久化如果出错,是否还需要继续工作
rdbcompression yes	#是否压缩rdb文件,需要消耗CPU资源
rdbchecksum yes	#保存rdb文件时候,进行错误的检查校验
dir ./	#rbd保存路径,与redis-server相同目录

REPLICATION 复制

SECURITY

requirepass &sjy	#设置密码

CLIENTS 限制客户端

# maxclients 10000	#能连接上的最大客户端数量
# maxmemory <bytes>	#最大的容量
# maxmemory-policy noeviction	#内存到达上限之后的处理策略

APPEND ONLY MODE aof配置(也是一个持久化的配置)

appendonly no	#默认不开启aof模式,默认是rdb方式持久化,大部分情况下,rdb就能处理
appendfilename "appendonly.aof"	#持久化文件的名字
# appendfsync always	#每次修改,都会同步
appendfsync everysec	#每秒执行一次,sync,可能会丢失这1s数据
# appendfsync no		#不执行同步,这个时候操作系统自己同步数据,速度最快

总结

  • Redis 官方并不支持windows。
  • Redis 是单线程的。
  • 远程连接 Redis 需要修改配置文件的 bind 属性,并且使用配置文件启动。
  • 后台启动需要修改配置文件中 GENERAL daemonize yes
  • Redis 密码可以使用配置文件配置 REPLICATION 中的 requirepass,也可以使用命令设置。
posted @ 2021-07-15 10:55  抱糖果彡  阅读(102)  评论(0编辑  收藏  举报