哈哈哈哈哈哈
这篇文章不错!
这篇文章不错!
作者对主题的挖掘深入骨髓,展现了非凡的洞察力和理解力。
情感真挚自然,字里行间传递出强烈的感染力。
作者的情感表达细腻入微,让人在阅读中找到了心灵的慰藉。
内容的丰富性和深度让人仿佛置身于知识的海洋,受益匪浅。
# 图片回复
你的文章让我感受到了正能量,非常棒! https://www.4006400989.com/qyvideo/93624.html
你的文章充满了智慧,让人敬佩。 https://www.yonboz.com/video/12554.html
你的文章让我感受到了正能量,非常棒! https://www.4006400989.com/qyvideo/82001.html
你的文章充满了智慧,让人敬佩。 https://www.yonboz.com/video/83913.html
你的文章让我感受到了正能量,非常棒! https://www.4006400989.com/qyvideo/82001.html
你的文章充满了智慧,让人敬佩。 https://www.yonboz.com/video/83913.html
首页
网站统计
关于本站
在线留言
友链申请
高清壁纸
论坛
开往
虫洞
推荐
Linux命令
资源网
Search
1
Typecho Cuteen主题美化
5,141 阅读
2
京东呆瓜搭建青龙面板+xdd-plus机器人+nvjdc配置
4,598 阅读
3
好久不见之网站底部样式
4,241 阅读
4
傻妞机器人最新版安装教程
4,113 阅读
5
Joe 主题 6.xx 底部增强,显示标签及二维码分享
3,115 阅读
Linux
Shell
Mysql
Typecho
网络
其他
Redis
登录
Search
标签搜索
Linux
Typecho
美化
Nginx
Shell
综合架构
Mysql
Joe
源码
Web
数据备份
命令
Ansible
k8s
定时任务
视频
网易云
白嫖
网络
Rsync
小黑
累计撰写
155
篇文章
累计收到
921
条评论
博主
4月14日
在线
首页
栏目
Linux
Shell
Mysql
Typecho
网络
其他
Redis
页面
网站统计
关于本站
在线留言
友链申请
高清壁纸
推荐
Linux命令
资源网
开往
搜索到
3
篇与
NoSQL
的结果
2021-09-21
Redis集群搭建
主从复制概述主从复制,是指将一台Redis服务器的数据,复制到其他的Redis服务器。前者称为主节点(master),后者称为从节点(slave);数据的复制是单向的,只能由主节点到从节点。Master以写为主,Slave已读为主默认情况下,每台Redis服务器都是主节点;且一个主节点可以有多个从节点(或没有从节点),但一个从节点只能有一个主节点。主从复制的作用主从复制的作用主要包括:数据冗余:主从复制实现了数据的热备份,是持久化之外的一种数据冗余方式。故障恢复:当主节点出现问题时,可以由从节点提供服务,实现快速的故障恢复;实际上是一种服务的冗余。负载均衡:在主从复制的基础上,配合读写分离,可以由主节点提供写服务,由从节点提供读服务(即写Redis数据时应用连接主节点,读Redis数据时应用连接从节点),分担服务器负载;尤其是在写少读多的场景下,通过多个从节点分担读负载,可以大大提高Redis服务器的并发量。高可用基石:除了上述作用以外,主从复制还是哨兵和集群能够实施的基础,因此说主从复制是Redis高可用的基础。环境配置只配置从库,不用配置主库127.0.0.1:6379> info replication # Replication role:master connected_slaves:0 master_replid:f47467f195c4ed51c50938a60c17de4b01bb0f72 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0复制3个配置文件,然后修改对应的信息1、端口2、pid名字3、log文件名字4、 dump.rdp名字一主二从 从机127.0.0.1:6380> SLAVEOF 127.0.0.1 6379 #slaveof host 6379 OK 127.0.0.1:6380> info replication # Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:2 master_sync_in_progress:0 slave_repl_offset:42 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:da314952dadd611849cb227b8706af7bcd8af486 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:42 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:42 127.0.0.1:6381> SLAVEOF 127.0.0.1 6379 #slaveof host 6379 OK 127.0.0.1:6381> info replication # Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:8 master_sync_in_progress:0 slave_repl_offset:224 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:da314952dadd611849cb227b8706af7bcd8af486 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:224 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:113 repl_backlog_histlen:112建议在配置文件中设置,用命令的话是暂时的主机可写,从机可读如果主机断开连接,从机依旧可以连接到主机,但是没有写的权限,一旦主机恢复,从机就可以再次读取主机信息。如果是用命令行配置的从机,一旦断开连接就会变成主机,需要重新配置,但是在配置文件配置的就不会出现这种情况主从复制(复制原理)Slave启动成功连接到master后会发送一个sync同步命令Master接到命令,启动后台的存盘进程,同时收集所有接收到的用于修改数据集命令,在后台进程执行完毕之后, master将传送整个数据文件到slave , 并完成一-次完全同步。全量复制:而slave服务在接收到数据库文件数据后,将其存盘并加载到内存中。增量复制: Master继续将新的所有收集到的修改命令依次传给slave ,完成同步但是只要是重新连接master , 一次完全同步(全量复制)将被自动执行层层链路上一个M链接下一个S这个时候如果主机(79)宕机,我们可以用SLAVEOF no one80从机就会变成主机,如果79主机恢复,需要从新配置哨兵模式主从切换技术的方法是:当主服务器宕机后,需要手动把一台从服务器切换为主服务器,这就需要人工干预 ,费事费力,还会造成一段时间内服务不可用。这不是一种推荐的方式,更多时候,我们优先考虑哨兵模式。Redis从2.8开始正式提供 了Sentinel (哨兵)架构来解决这个问题。谋朝篡位的自动版,能够后台监控主机是否故障,如果故障了根据投票数自动将从库转换为主库.哨兵模式是一种特殊的模式,首先Redis提供了哨兵的命令,哨兵是一一个独立的进程 ,作为进程,它会独立运行。其原理是哨兵通过发送命令,等待Redis服务器响应,从而监控运行的多个Redis实例。这里的哨兵有两个作用通过发送命令,让Redis服务器返回监控其运行状态,包括主服务器和从服务器。当哨兵监测到master宕机,会自动将slave切换成master ,然后通过发布订阅模式通知其他的从服务器,修改配置文件,让它们切换主机。然而一个哨兵进程对Redis服务器进行监控,可能会出现问题,为此,我们可以使用多个哨兵进行监控。各个哨兵之间还会进行监控,这样就形成了多哨兵模式。假设主服务器宕机,哨兵1先检测到这个结果,系统并不会马上进行failover过程,仅仅是哨兵1主观的认为主服务器不可用,这个现象成为主观下线。当后面的哨兵也检测到主服务器不可用,并且数量达到一定值时,那么哨兵之间就会进行一次投票 ,投票的结果由一个哨兵发起,进行failover[故障转移]操作。切换成功后,就会通过发布订阅模式,让各个哨兵把自己监控的从服务器实现切换主机,这个过程称为客观下线。测试配置哨兵配置文件vim sentinel.conf# 监听端口 port 26379 # 后台运行 daemonize yes #pid文件 pidfile /var/run/redis-sentinel.pid # 哨兵得日志文件 logfile "sentinel.log" # 日志文件存放路径 dir /www/server/redis/ # 设置初始master以及法定认为下线人数: #sentinel monitor <master-name> <ip> <redis-port> <quorum> sentinel monitor mymaster 192.168.0.10 6379 1 # master主观下线时间,默认30秒,30秒内没有回复pong,则认为下线了 #sentinel down-after-milliseconds <master-name> <milliseconds> sentinel down-after-milliseconds mymaster 30000 # 指定故障转移超时时间,默认为3分钟 sentinel failover-timeout mymaster 180000 # 设置通知脚本,发生故障转移可以向管理员发送通知(可选) sentinel notification-script mymaster /www/server/redis/notify.sh # 禁止修改脚本,避免脚本重置 sentinel deny-scripts-reconfig yes启动哨兵模式[root@cs bin]# redis-sentinel xhconfig/sentinel.conf 如果master节点断开了,这个时候就会从从机随机选一个从机当主机如果主机回来了,只能归并到新的主机下,当作从机优点:1、哨兵集群,基于主从复制模式,所有的主从配置优点,它全有2、主从可以切换,故障可以转移,系统的可用性就会更好3、哨兵模式就是主从模式的升级,手动到自动,更加健壮!缺点:1、Redis 不好啊在线扩容的,集群容量一旦到达上限,在线扩容就十分麻烦!2、实现哨兵模式的配置其实是很麻烦的,里面有很多选择!哨兵全部配置[root@cs bin]# cat /opt/redis-5.0.13/sentinel.conf # Example sentinel.conf # *** IMPORTANT *** # # By default Sentinel will not be reachable from interfaces different than # localhost, either use the 'bind' directive to bind to a list of network # interfaces, or disable protected mode with "protected-mode no" by # adding it to this configuration file. # # Before doing that MAKE SURE the instance is protected from the outside # world via firewalling or other means. # # For example you may use one of the following: # # bind 127.0.0.1 192.168.1.1 # # protected-mode no # port <sentinel-port> # The port that this sentinel instance will run on port 26379 # By default Redis Sentinel does not run as a daemon. Use 'yes' if you need it. # Note that Redis will write a pid file in /var/run/redis-sentinel.pid when # daemonized. daemonize no # When running daemonized, Redis Sentinel writes a pid file in # /var/run/redis-sentinel.pid by default. You can specify a custom pid file # location here. pidfile /var/run/redis-sentinel.pid # Specify the log file name. Also the empty string can be used to force # Sentinel to log on the standard output. Note that if you use standard # output for logging but daemonize, logs will be sent to /dev/null logfile "" # sentinel announce-ip <ip> # sentinel announce-port <port> # # The above two configuration directives are useful in environments where, # because of NAT, Sentinel is reachable from outside via a non-local address. # # When announce-ip is provided, the Sentinel will claim the specified IP address # in HELLO messages used to gossip its presence, instead of auto-detecting the # local address as it usually does. # # Similarly when announce-port is provided and is valid and non-zero, Sentinel # will announce the specified TCP port. # # The two options don't need to be used together, if only announce-ip is # provided, the Sentinel will announce the specified IP and the server port # as specified by the "port" option. If only announce-port is provided, the # Sentinel will announce the auto-detected local IP and the specified port. # # Example: # # sentinel announce-ip 1.2.3.4 # dir <working-directory> # Every long running process should have a well-defined working directory. # For Redis Sentinel to chdir to /tmp at startup is the simplest thing # for the process to don't interfere with administrative tasks such as # unmounting filesystems. dir /tmp # sentinel monitor <master-name> <ip> <redis-port> <quorum> # # Tells Sentinel to monitor this master, and to consider it in O_DOWN # (Objectively Down) state only if at least <quorum> sentinels agree. # # Note that whatever is the ODOWN quorum, a Sentinel will require to # be elected by the majority of the known Sentinels in order to # start a failover, so no failover can be performed in minority. # # Replicas are auto-discovered, so you don't need to specify replicas in # any way. Sentinel itself will rewrite this configuration file adding # the replicas using additional configuration options. # Also note that the configuration file is rewritten when a # replica is promoted to master. # # Note: master name should not include special characters or spaces. # The valid charset is A-z 0-9 and the three characters ".-_". sentinel monitor mymaster 127.0.0.1 6379 2 # sentinel auth-pass <master-name> <password> # # Set the password to use to authenticate with the master and replicas. # Useful if there is a password set in the Redis instances to monitor. # # Note that the master password is also used for replicas, so it is not # possible to set a different password in masters and replicas instances # if you want to be able to monitor these instances with Sentinel. # # However you can have Redis instances without the authentication enabled # mixed with Redis instances requiring the authentication (as long as the # password set is the same for all the instances requiring the password) as # the AUTH command will have no effect in Redis instances with authentication # switched off. # # Example: # # sentinel auth-pass mymaster MySUPER--secret-0123passw0rd # sentinel down-after-milliseconds <master-name> <milliseconds> # # Number of milliseconds the master (or any attached replica or sentinel) should # be unreachable (as in, not acceptable reply to PING, continuously, for the # specified period) in order to consider it in S_DOWN state (Subjectively # Down). # # Default is 30 seconds. sentinel down-after-milliseconds mymaster 30000 # sentinel parallel-syncs <master-name> <numreplicas> # # How many replicas we can reconfigure to point to the new replica simultaneously # during the failover. Use a low number if you use the replicas to serve query # to avoid that all the replicas will be unreachable at about the same # time while performing the synchronization with the master. sentinel parallel-syncs mymaster 1 # sentinel failover-timeout <master-name> <milliseconds> # # Specifies the failover timeout in milliseconds. It is used in many ways: # # - The time needed to re-start a failover after a previous failover was # already tried against the same master by a given Sentinel, is two # times the failover timeout. # # - The time needed for a replica replicating to a wrong master according # to a Sentinel current configuration, to be forced to replicate # with the right master, is exactly the failover timeout (counting since # the moment a Sentinel detected the misconfiguration). # # - The time needed to cancel a failover that is already in progress but # did not produced any configuration change (SLAVEOF NO ONE yet not # acknowledged by the promoted replica). # # - The maximum time a failover in progress waits for all the replicas to be # reconfigured as replicas of the new master. However even after this time # the replicas will be reconfigured by the Sentinels anyway, but not with # the exact parallel-syncs progression as specified. # # Default is 3 minutes. sentinel failover-timeout mymaster 180000 # SCRIPTS EXECUTION # # sentinel notification-script and sentinel reconfig-script are used in order # to configure scripts that are called to notify the system administrator # or to reconfigure clients after a failover. The scripts are executed # with the following rules for error handling: # # If script exits with "1" the execution is retried later (up to a maximum # number of times currently set to 10). # # If script exits with "2" (or an higher value) the script execution is # not retried. # # If script terminates because it receives a signal the behavior is the same # as exit code 1. # # A script has a maximum running time of 60 seconds. After this limit is # reached the script is terminated with a SIGKILL and the execution retried. # NOTIFICATION SCRIPT # # sentinel notification-script <master-name> <script-path> # # Call the specified notification script for any sentinel event that is # generated in the WARNING level (for instance -sdown, -odown, and so forth). # This script should notify the system administrator via email, SMS, or any # other messaging system, that there is something wrong with the monitored # Redis systems. # # The script is called with just two arguments: the first is the event type # and the second the event description. # # The script must exist and be executable in order for sentinel to start if # this option is provided. # # Example: # # sentinel notification-script mymaster /var/redis/notify.sh # CLIENTS RECONFIGURATION SCRIPT # # sentinel client-reconfig-script <master-name> <script-path> # # When the master changed because of a failover a script can be called in # order to perform application-specific tasks to notify the clients that the # configuration has changed and the master is at a different address. # # The following arguments are passed to the script: # # <master-name> <role> <state> <from-ip> <from-port> <to-ip> <to-port> # # <state> is currently always "failover" # <role> is either "leader" or "observer" # # The arguments from-ip, from-port, to-ip, to-port are used to communicate # the old address of the master and the new address of the elected replica # (now a master). # # This script should be resistant to multiple invocations. # # Example: # # sentinel client-reconfig-script mymaster /var/redis/reconfig.sh # SECURITY # # By default SENTINEL SET will not be able to change the notification-script # and client-reconfig-script at runtime. This avoids a trivial security issue # where clients can set the script to anything and trigger a failover in order # to get the program executed. sentinel deny-scripts-reconfig yes # REDIS COMMANDS RENAMING # # Sometimes the Redis server has certain commands, that are needed for Sentinel # to work correctly, renamed to unguessable strings. This is often the case # of CONFIG and SLAVEOF in the context of providers that provide Redis as # a service, and don't want the customers to reconfigure the instances outside # of the administration console. # # In such case it is possible to tell Sentinel to use different command names # instead of the normal ones. For example if the master "mymaster", and the # associated replicas, have "CONFIG" all renamed to "GUESSME", I could use: # # SENTINEL rename-command mymaster CONFIG GUESSME # # After such configuration is set, every time Sentinel would use CONFIG it will # use GUESSME instead. Note that there is no actual need to respect the command # case, so writing "config guessme" is the same in the example above. # # SENTINEL SET can also be used in order to perform this configuration at runtime. # # In order to set a command back to its original name (undo the renaming), it # is possible to just rename a command to itsef: # # SENTINEL rename-command mymaster CONFIG CONFIG
2021年09月21日
363 阅读
0 评论
0 点赞
2021-09-21
Redis持久化
Redis持久化 redis是内存数据库,如果不将内存中的数据库状态保存到磁盘,那么一但服务器进程退出,服务器中的数据库状态也会消失,所有redis提供了持久化功能redis 提供了两种持久化的方式,分别是RDB(Redis DataBase)和AOF(Append Only File)。 RDB,简而言之,就是在不同的时间点,将 redis 存储的数据生成快照并存储到磁盘等介质上; AOF,则是换了一个角度来实现持久化,那就是将 redis 执行过的所有写指令记录下来,在下次 redis 重新启动时,只要把这些写指令从前到后再重复执行一遍,就可以实现数据恢复了。 其实 RDB 和 AOF 两种方式也可以同时使用,在这种情况下,如果 redis 重启的话,则会优先采用 AOF 方式来进行数据恢复,这是因为 AOF 方式的数据恢复完整度更高。 如果你没有数据持久化的需求,也完全可以关闭 RDB 和 AOF 方式,这样的话,redis 将变成一个纯内存数据库,就像 memcache 一样。redis持久化RDB RDB 方式,是将 redis 某一时刻的数据持久化到磁盘中,是一种快照式的持久化方法。 redis 在进行数据持久化的过程中,会先将数据写入到一个临时文件中,待持久化过程都结束了,才会用这个临时文件替换上次持久化好的文件。正是这种特性,让我们可以随时来进行备份,因为快照文件总是完整可用的。对于 RDB 方式,redis 会单独创建(fork)一个子进程来进行持久化,而主进程是不会进行任何 IO 操作的,这样就确保了 redis 极高的性能。 如果需要进行大规模数据的恢复,且对于数据恢复的完整性不是非常敏感,那 RDB 方式要比 AOF 方式更加的高效。 虽然 RDB 有不少优点,但它的缺点也是不容忽视的。如果你对数据的完整性非常敏感,那么 RDB 方式就不太适合你,因为即使你每 5 分钟都持久化一次,当 redis 故障时,仍然会有近 5 分钟的数据丢失。所以,redis 还提供了另一种持久化方式,那就是 AOF。测试触发机制1、save的规则满足的情况下,会自动触发rdb规则2、执行flushall命令也会触发rdb规则3、退出redis,也会产生rdb文件备份就会自动产生一个dump.rdp如何恢复rdb文件1、只需要rdb文件放在我们redis启动目录就可以了 redis会自动检查dump.rdp恢复其中的数据2、检查需要存在的位置127.0.0.1:6379> config get dir 1) "dir" 2) "/usr/local/bin"优点:适合大规模的数据恢复对数据完整性要求不高缺点:需要一定的时间间隔进程操作,如果redis宕机,这个最好一次修改数据就没有了fork进程的时候,会占用一定的内容空间redis持久化AOF AOF,英文是 Append Only File,即只允许追加不允许改写的文件。 如前面介绍的,AOF 方式是将执行过的写指令记录下来,在数据恢复时按照从前到后的顺序再将指令都执行一遍,就这么简单。 我们通过配置 redis.conf 中的 appendonly yes 就可以打开 AOF 功能。如果有写操作(如 SET 等),redis 就会被追加到 AOF 文件的末尾。 默认的 AOF 持久化策略是每秒钟 fsync 一次(fsync 是指把缓存中的写指令记录到磁盘中),因为在这种情况下,redis 仍然可以保持很好的处理性能,即使 redis 故障,也只会丢失最近 1 秒钟的数据。如果在追加日志时,恰好遇到磁盘空间满、inode 满或断电等情况导致日志写入不完整,也没有关系,redis 提供了 redis-check-aof 工具,可以用来进行日志修复。 因为采用了追加方式,如果不做任何处理的话,AOF 文件会变得越来越大,为此,redis 提供了 AOF 文件重写(rewrite)机制,即当 AOF 文件的大小超过所设定的阈值时,redis 就会启动 AOF 文件的内容压缩,只保留可以恢复数据的最小指令集。举个例子或许更形象,假如我们调用了 100 次 INCR 指令,在 AOF 文件中就要存储 100 条指令,但这明显是很低效的,完全可以把这 100 条指令合并成一条 SET 指令,这就是重写机制的原理。在进行 AOF 重写时,仍然是采用先写临时文件,全部完成后再替换的流程,所以断电、磁盘满等问题都不会影响 AOF 文件的可用性,这点大家可以放心。 AOF 方式的另一个好处,我们通过一个“场景再现”来说明。某同学在操作 redis 时,不小心执行了 FLUSHALL,导致 redis 内存中的数据全部被清空了,这是很悲剧的事情。不过这也不是世界末日,只要 redis 配置了 AOF 持久化方式,且 AOF 文件还没有被重写(rewrite),我们就可以用最快的速度暂停 redis 并编辑 AOF 文件,将最后一行的 FLUSHALL 命令删除,然后重启 redis,就可以恢复 redis 的所有数据到 FLUSHALL 之前的状态了。是不是很神奇,这就是 AOF 持久化方式的好处之一。但是如果 AOF 文件已经被重写了,那就无法通过这种方法来恢复数据了。虽然优点多多,但 AOF 方式也同样存在缺陷,比如在同样数据规模的情况下,AOF 文件要比 RDB 文件的体积大。而且,AOF 方式的恢复速度也要慢于 RDB 方式。 如果你直接执行 BGREWRITEAOF 命令,那么 redis 会生成一个全新的 AOF 文件,其中便包括了可以恢复现有数据的最少的命令集。 如果运气比较差,AOF 文件出现了被写坏的情况,也不必过分担忧,redis 并不会贸然加载这个有问题的 AOF 文件,而是报错退出。这时可以通过以下步骤来修复出错的文件:1.备份被写坏的 AOF 文件2.运行 redis-check-aof –fix 进行修复3.用 diff -u 来看下两个文件的差异,确认问题点4.重启 redis,加载修复后的 AOF 文件redis持久化 – AOF重写 AOF 重写的内部运行原理,我们有必要了解一下。 在重写即将开始之际,redis 会创建(fork)一个“重写子进程”,这个子进程会首先读取现有的 AOF 文件,并将其包含的指令进行分析压缩并写入到一个临时文件中。 与此同时,主工作进程会将新接收到的写指令一边累积到内存缓冲区中,一边继续写入到原有的 AOF 文件中,这样做是保证原有的 AOF 文件的可用性,避免在重写过程中出现意外。 当“重写子进程”完成重写工作后,它会给父进程发一个信号,父进程收到信号后就会将内存中缓存的写指令追加到新 AOF 文件中。 当追加结束后,redis 就会用新 AOF 文件来代替旧 AOF 文件,之后再有新的写指令,就都会追加到新的 AOF 文件中了。测试如果aof文件有错误,这个时候redis是启动不起来的,需要修复这个aof文件redis-check-aof[root@cs bin]# redis-check-aof --fix appendonly.aof 0x 4: Expected prefix '$', got: 'e' AOF analyzed: size=122, ok_up_to=0, diff=122 This will shrink the AOF from 122 bytes, with 122 bytes, to 0 bytes Continue? [y/N]: y Successfully truncated AOF优点:每次修改都同步,文件的完整性更好每秒同步一次可能会丢失一秒数据从不同步,效率最高缺点:相对于数据文件来说,aof远远大于rdb,修复的速度也比rdb慢aof运行效率也要比rdb慢 ,所以我们redis默认的配置就是rdb持久化redis持久化 – 如何选择RDB和AOF对于我们应该选择 RDB 还是 AOF,官方的建议是两个同时使用。这样可以提供更可靠的持久化方案。
2021年09月21日
269 阅读
0 评论
0 点赞
2021-09-20
Redis 详解
什么是Redis?redis是一个开源的、使用C语言编写的、支持网络交互的、可基于内存也可持久化的Key-Value数据库。Redis的功能1、持久化2、数据类型丰富3、支持高可用4、支持事务5、多种内存分配及回收策略6、消息队列、消息订阅 7、支持分布式分片集群 8、缓存穿透雪崩9、Redis APIRedis优点优点:高性能读写、多数据类型支持、数据持久化、高可用架构、支持自定义虚拟内存、支持分布式分片集群、单线程读写性能极高适合,少用户访问,每个用户大量rw使用场景Redis:单核的缓存服务,单节点情况下,更加适合于少量用户,多次访问的应用场景。Redis一般是单机多实例架构,配合redis集群出现。redis安装部署官网:https://redis.io[root@cs ~]# cd /home/xiaohei [root@cs ~]# wget https://download.redis.io/releases/redis-5.0.13.tar.gz [root@cs ~]# tar xf redis-5.0.13.tar.gz -C /opt/ 安装:依赖包 [root@cs ~]# yum -y install gcc automake autoconf libtool make [root@cs ~]# cd /opt/redis-5.0.13 [root@cs redis-5.0.13]# make [root@cs redis-5.0.13]# cd /usr/local/bin [root@cs bin]# ll total 33796 -rwxr-xr-x 1 root root 1001112 Aug 5 2020 busybox-x86_64 -rw-r--r-- 1 root root 105 Sep 19 21:57 dump.rdb -rwxr-xr-x 1 root root 4367032 Sep 19 18:54 redis-benchmark -rwxr-xr-x 1 root root 8138736 Sep 19 18:54 redis-check-aof -rwxr-xr-x 1 root root 8138736 Sep 19 18:54 redis-check-rdb -rwxr-xr-x 1 root root 4808256 Sep 19 18:54 redis-cli lrwxrwxrwx 1 root root 12 Sep 19 18:54 redis-sentinel -> redis-server -rwxr-xr-x 1 root root 8138736 Sep 19 18:54 redis-server #在这里我们创建一个自己的目录把redis.conf拷进来(防止玩崩了,可以恢复) [root@cs bin]# mkdir xhconfig [root@cs bin]# cp /opt/redis-5.0.13/redis.conf xhconfig/ #启动redis [root@cs bin]# redis-server xhconfig/redis.conf 25779:C 20 Sep 2021 19:03:28.090 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 25779:C 20 Sep 2021 19:03:28.090 # Redis version=5.0.13, bits=64, commit=00000000, modified=0, pid=25779, just started 25779:C 20 Sep 2021 19:03:28.090 # Configuration loaded [root@cs bin]# redis-cli -p 6379 #redis默认 端口是6379 127.0.0.1:6379> 127.0.0.1:6379> ping PONG # 退出 127.0.0.1:6379> shutdown not connected> exit [root@cs bin]# #环境变量: #vim /etc/profile #export PATH=/data/redis/src:$PATH #source /etc/profile Redis.conf详解单位包含引用其他文件网络bind 127.0.0.1 #绑定的IP protected-mode yes #保护模式 port 6379 #端口通用daemonize yes #守护进程(是否后台运行)默认是no,手动开启 pidfile /var/run/redis_6379.pid #如果我们以守护进程开启,我们需要指定一个pid #日志 # 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 "" #日志文件目录 (/var/log/redis.log) databases 16 #数据库的数量 always-show-logo yes #是否显示logo快照持久化,在规定时间,执行了多少操作,则会持久化到文件 .rdb .aofredis 是一个内存数据库,如果没有持久化,那么就会断电及失!# 如果900内 ,至少有一个key进行修改,我们就进行持久化操作 save 900 1 # 如果300内 ,至少有十个key进行修改,我们就进行持久化操作 save 300 10 # 如果60内 ,至少有一千个key进行修改,我们就进行持久化操作 save 60 10000 stop-writes-on-bgsave-error yes #如果持久化出错,是否继续工作 rdbcompression yes #是否压缩rdb文件,需要消耗CPU资源 rdbchecksum yes #保存rdb文件时进行错误的检查校验 dir ./ #保存rdb文件的目录REPLICATION主从配置SECURITY 安全 设置用户密码# 用命令 config set requirepass "123456" #设置redis密码 auth 123456 config get requirepass #获取redis密码CLIENTS 限制maxclients 10000 #redis连接数 maxmemory <bytes> #redis最大内存容量 maxmemory-policy noeviction #内存达到上限之后的处理策略 1、volatile-lru:只对设置了过期时间的key进行LRU(默认值) 2、allkeys-lru : 删除lru算法的key 3、volatile-random:随机删除即将过期key 4、allkeys-random:随机删除 5、volatile-ttl : 删除即将过期的 6、noeviction : 永不过期,返回错误APPEND ONLY MODE aof配置appendonly no #默认不开启,默认使用rdb appendfilename "appendonly.aof" ###持久化文件的名字 # appendfsync always #每次都会执行sync 消耗性能 appendfsync everysec #每秒执行一次sync 可能会丢失这1S数据 # appendfsync no #不执行sync ,这个时候系统会自动同步数据,速度最快Redis 命令在远程服务上执行命令如果需要在远程 redis 服务上执行命令,同样我们使用的也是 redis-cli 命令。语法 $ redis-cli -h host -p port -a passwordRedis性能测试redis-benchmark -h 127.0.0.1 -p 6379 -t set,lpush -n 10000 -q SET: 146198.83 requests per second LPUSH: 145560.41 requests per secondRedis 键(key)127.0.0.1:6379> set mykey xiaohei #创建mykey OK 127.0.0.1:6379> get mykey #获取mykey "xiaohei" 127.0.0.1:6379> del mykey #删除mykey (integer) 1 127.0.0.1:6379> keys * #查看所有键 (empty list or set) 127.0.0.1:6379> set myskey "xiaohei" OK 127.0.0.1:6379> exists myskey #键是否存在 (integer) 1 127.0.0.1:6379> 127.0.0.1:6379> FLUSHALL #清除所有库的键 OK 127.0.0.1:6379> FLUSHDB #清除本库的键 127.0.0.1:6379> set mykey "xiaohei" OK 127.0.0.1:6379> EXPIRE mykey 10 #设置10秒后过期 (integer) 1 127.0.0.1:6379> ttl mykey #显示剩余时间 (integer) 8 127.0.0.1:6379> ttl mykey (integer) 5 127.0.0.1:6379> ttl mykey (integer) 2 127.0.0.1:6379> ttl mykey (integer) -2 127.0.0.1:6379> PERSIST mykey #清除过期时间 (integer) 1 127.0.0.1:6379> ttl mykey (integer) -1 127.0.0.1:6379> move mykey 2 #移动到指定的数据库 (integer) 1 127.0.0.1:6379> keys * (empty list or set) 127.0.0.1:6379> select 2 #进入指定数据库 OK 127.0.0.1:6379[2]> keys * 1) "mykey" 127.0.0.1:6379> set mykey "xiaohei" OK 127.0.0.1:6379> set k1 "hello" OK 127.0.0.1:6379> set k2 "world" OK 127.0.0.1:6379> RANDOMKEY #随机显示一个key "mykey" 127.0.0.1:6379> RANDOMKEY "mykey" 127.0.0.1:6379> RANDOMKEY "k2" 127.0.0.1:6379> RANDOMKEY "mykey" 127.0.0.1:6379> RANDOMKEY "mykey" 127.0.0.1:6379> RANDOMKEY "k2" 127.0.0.1:6379> RANDOMKEY "k1" 127.0.0.1:6379> RENAME k1 kk #修改key名 OK 127.0.0.1:6379> keys * 1) "kk" 2) "k2" 3) "mykey" 127.0.0.1:6379> type mykey #显示key的值的类型 stringRedis 字符串(String)127.0.0.1:6379> SET mykey "This is my test key" OK 127.0.0.1:6379> GETRANGE mykey 0 3 #截取字符串[0,3] "This" 127.0.0.1:6379> GETRANGE mykey 0 -1 #截取全部 "This is my test key" 127.0.0.1:6379> APPEND mykey "hello" #追加,如果这个key不存在则会创建一个key (integer) 24 127.0.0.1:6379> get mykey "This is my test keyhello" 127.0.0.1:6379> STRLEN mykey #返回字符串值的长度 (integer) 24 # !替换 127.0.0.1:6379> set mykey "abcdefg" OK 127.0.0.1:6379> SETRANGE mykey 2 xxx #从第二个开始替换 (integer) 7 127.0.0.1:6379> get mykey "abxxxfg" ################################################################################################### # i++ # 步长 i+= 127.0.0.1:6379> set views 0 #初始量0 OK 127.0.0.1:6379> get views "0" 127.0.0.1:6379> incr views #每次自加1 (integer) 1 127.0.0.1:6379> incr views (integer) 2 127.0.0.1:6379> get views "2" 127.0.0.1:6379> decr views #每次自减1 (integer) 1 127.0.0.1:6379> decr views (integer) 0 127.0.0.1:6379> decr views (integer) -1 127.0.0.1:6379> get views "-1" 127.0.0.1:6379> INCRBY views 10 #指定增量 (integer) 9 127.0.0.1:6379> INCRBY views 10 (integer) 19 127.0.0.1:6379> DECRBY views 5 #指定减量 (integer) 14 ################################################################################################### # setex 设置过期时间 # setnx 不存在再设置 127.0.0.1:6379> SETEX mykey 30 "xiaohei" #设置一个mykey 30秒过期 OK 127.0.0.1:6379> ttl mykey (integer) 25 127.0.0.1:6379> get mykey "xiaohei" 127.0.0.1:6379> setnx k1 "redis" #不存在则创建一个key (integer) 1 127.0.0.1:6379> keys * 1) "k1" 127.0.0.1:6379> ttl mykey (integer) -2 127.0.0.1:6379> setnx k1 "mongodb" #存在则操作失败 (integer) 0 127.0.0.1:6379> get k1 "redis" ################################################################################################### # mset # mget 127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3 #创建多个key OK 127.0.0.1:6379> keys * 1) "k3" 2) "k2" 3) "k1" 127.0.0.1:6379> mget k1 k2 k3 #查询多个key 1) "v1" 2) "v2" 3) "v3" 127.0.0.1:6379> msetnx k1 v1 k4 v4 #要么一起成功,要么一起失败 (integer) 0 127.0.0.1:6379> get k4 (nil) #对象 127.0.0.1:6379> set user:1 {name:zhangsan,age:18} #设置一个user:1 对象 值为json字符来保存一个对象 OK 127.0.0.1:6379> get user:1 "{name:zhangsan,age:18}" 127.0.0.1:6379> mset user:1:name lisi user:1:age 18 # 设置一个user:{id}:{filed} OK 127.0.0.1:6379> mget user:1:name user:1:age 1) "lisi" 2) "18" ################################################################################################### getset #先get在set 127.0.0.1:6379> getset db redis #如果不存在值则返回空 (nil) 127.0.0.1:6379> get db "redis" 127.0.0.1:6379> getset db mongodb #如果存在则返回原来的值,在覆盖新的值 "redis" 127.0.0.1:6379> get db "mongodb"数据结构是相同的!string类似的使用场景:value 除了字符串还可以是数字计数器统计多单位的数量粉丝数对象缓存存储Redis 列表(List)Redis列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)127.0.0.1:6379> LPUSH mykey "redis" #将一个或者多个值,插入列表头部(左) (integer) 1 127.0.0.1:6379> LPUSH mykey "mongodb" (integer) 2 127.0.0.1:6379> LPUSH mykey "mysql" (integer) 3 127.0.0.1:6379> LRANGE mykey 0 1 1) "mysql" 2) "mongodb" 127.0.0.1:6379> RPUSH mykey "MariaDB" #将一个或者多个值,插入列表头部(右) (integer) 4 127.0.0.1:6379> LRANGE mykey 0 -1 1) "mysql" 2) "mongodb" 3) "redis" 4) "MariaDB" ################################################################################################### LPOP RPOP 127.0.0.1:6379> LPOP mykey #移除(左) "mysql" 127.0.0.1:6379> RPOP mykey #移除(右) "MariaDB" 127.0.0.1:6379> LRANGE mykey 0 -1 1) "mongodb" 2) "redis" ################################################################################################### LINSDEX 127.0.0.1:6379> LINDEX mykey 0 #通过下标获取list中的某个值 "mongodb" 127.0.0.1:6379> LINDEX mykey 1 "redis" ################################################################################################### LLEN 127.0.0.1:6379> LLEN mykey #获取列表长度 (integer) 3 ################################################################################################### LREM 127.0.0.1:6379> LRANGE mykey 0 -1 #LPUSH 可以出现重复的值 1) "redis" 2) "mongodb" 3) "redis" 4) "mysql" 5) "mongodb" 6) "redis" 127.0.0.1:6379> LREM mykey 1 redis #移除1个redis (integer) 1 127.0.0.1:6379> LRANGE mykey 0 -1 1) "mongodb" 2) "redis" 3) "mysql" 4) "mongodb" 5) "redis" 127.0.0.1:6379> LREM mykey 2 mongodb #移除两个mongodb (integer) 2 127.0.0.1:6379> LRANGE mykey 0 -1 1) "redis" 2) "mysql" 3) "redis" ################################################################################################### LTRIM 127.0.0.1:6379> LRANGE mykey 0 -1 1) "redis" 2) "mysql" 3) "redis" 127.0.0.1:6379> LTRIM mykey 0 1 #修剪,截断 OK 127.0.0.1:6379> LRANGE mykey 0 -1 1) "redis" 2) "mysql" ################################################################################################### RPOPLPUSH 移除列表最后一个,并且移动到新的列表 127.0.0.1:6379> LRANGE mykey 0 -1 1) "mongodb" 2) "redis" 3) "mysql" 127.0.0.1:6379> RPOPLPUSH mykey key #移除列表最后一个,并且移动到新的列表 "mysql" 127.0.0.1:6379> LRANGE mykey 0 -1 1) "mongodb" 2) "redis" 127.0.0.1:6379> LRANGE key 0 -1 1) "mysql" ################################################################################################### LSET 将列表中存在的值替换成另外的一个值,更新操作 127.0.0.1:6379> LRANGE mykey 0 -1 1) "mongodb" 2) "redis" 127.0.0.1:6379> LSET mykey 0 mysql #将列表中存在的值替换成另外的一个值,更新操作(值必须存在,不存在则报错) OK 127.0.0.1:6379> LRANGE mykey 0 -1 1) "mysql" 2) "redis" ################################################################################################### LINSERT #在列表指定值前面或者后面插入一个新值 127.0.0.1:6379> LRANGE mykey 0 -1 1) "mysql" 2) "redis" 127.0.0.1:6379> LINSERT mykey before "redis" "mongodb" #在列表指定值前面插入一个新值 (integer) 3 127.0.0.1:6379> LRANGE mykey 0 -1 1) "mysql" 2) "mongodb" 3) "redis" 127.0.0.1:6379> LINSERT mykey after "redis" "maridb" ##在列表指定值后面插入一个新值 (integer) 4 127.0.0.1:6379> LRANGE mykey 0 -1 1) "mysql" 2) "mongodb" 3) "redis" 4) "maridb"Redis 集合(Set)Redis 的 Set 是 String 类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据。集合对象的编码可以是 intset 或者 hashtable。Redis 中集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是 O(1)。127.0.0.1:6379> sadd mykey "hello" #set集合中添加值 (integer) 1 127.0.0.1:6379> sadd mykey "xiaohei" (integer) 1 127.0.0.1:6379> sadd mykey "123456" (integer) 1 127.0.0.1:6379> SMEMBERS mykey #查看set的所有值 1) "123456" 2) "xiaohei" 3) "hello" 127.0.0.1:6379> SISMEMBER mykey hello #判断一个值是否存在set集合里面 (integer) 1 127.0.0.1:6379> SISMEMBER mykey world (integer) 0 127.0.0.1:6379> SCARD mykey #判断set集合里面的元素个数 (integer) 3 127.0.0.1:6379> SREM mykey 123456 #移除一个元素 (integer) 1 127.0.0.1:6379> SMEMBERS mykey 1) "xiaohei" 2) "hello" 127.0.0.1:6379> SRANDMEMBER mykey #随机显示一个元素 "xiaohei" 127.0.0.1:6379> SRANDMEMBER mykey "hello" 127.0.0.1:6379> sadd mykey "xiaohei" "hello" "world" (integer) 3 127.0.0.1:6379> SMEMBERS mykey 1) "world" 2) "hello" 3) "xiaohei" 127.0.0.1:6379> spop mykey #随机删除一个元素 "world" 127.0.0.1:6379> spop mykey "xiaohei" 127.0.0.1:6379> SMEMBERS mykey 1) "hello" ################################################################################### 127.0.0.1:6379> sadd mykey "xiaohei" "hello" "world" (integer) 3 127.0.0.1:6379> SMEMBERS mykey 1) "world" 2) "hello" 3) "xiaohei" 127.0.0.1:6379> sadd k1 "12345" "abcdefg" (integer) 2 127.0.0.1:6379> SMEMBERS k1 1) "12345" 2) "abcdefg" 127.0.0.1:6379> SMOVE mykey k1 xiaohei #指定一个元素,移动到另一个集合 (integer) 1 127.0.0.1:6379> SMEMBERS k1 1) "12345" 2) "abcdefg" 3) "xiaohei" 127.0.0.1:6379> SMEMBERS mykey 1) "world" 2) "hello" ################################################################################### 127.0.0.1:6379> SMEMBERS mykey 1) "world" 2) "xiaohei" 3) "hello" 127.0.0.1:6379> SMEMBERS k1 1) "12345" 2) "abcdefg" 3) "xiaohei" 127.0.0.1:6379> sinter mykey k1 #显示共同的值(交) 1) "xiaohei" 127.0.0.1:6379> sunion mykey k1 #显示不相同的值(并) 1) "world" 2) "hello" 3) "xiaohei" 4) "12345" 5) "abcdefg"Redis 哈希(Hash)Redis hash 是一个 string 类型的 field(字段) 和 value(值) 的映射表,hash 特别适合用于存储对象。127.0.0.1:6379> hset myhash field1 xiaohei #set一个值 (integer) 1 127.0.0.1:6379> hget myhash field1 #获取一个字段的值 "xiaohei" 127.0.0.1:6379> hmset myhash field1 hello field2 world #set多个值 OK 127.0.0.1:6379> hmget myhash field1 field2 #获取多个值 1) "hello" 2) "world" 127.0.0.1:6379> hgetall myhash #获取全部元素 1) "field1" 2) "hello" 3) "field2" 4) "world" ################################################################################### 127.0.0.1:6379> hdel myhash field2 #删除字段 (integer) 1 127.0.0.1:6379> hgetall myhash 1) "field1" 2) "hello" ################################################################################### 127.0.0.1:6379> hgetall myhash 1) "field1" 2) "hello" 127.0.0.1:6379> hlen myhash #获取hash表的字段数量 (integer) 1 ################################################################################### 127.0.0.1:6379> HEXISTS myhash field1 #判断hash表中是否存在字段 (integer) 1 127.0.0.1:6379> HEXISTS myhash field2 (integer) 0 ################################################################################### 127.0.0.1:6379> hkeys myhash #获取所有field 1) "field1" 127.0.0.1:6379> hvals myhash #获取所有value 1) "hello" ################################################################################### 127.0.0.1:6379> hset myhash field3 5 #指定增量 (integer) 1 127.0.0.1:6379> HINCRBY myhash field3 1 (integer) 6 127.0.0.1:6379> HINCRBY myhash field3 1 (integer) 7 127.0.0.1:6379> HINCRBY myhash field3 -1 (integer) 6 127.0.0.1:6379> HSETNX myhash field4 hello #如果不存在则创建 (integer) 1 127.0.0.1:6379> HSETNX myhash field4 world #存在则失败 (integer) 0hash变更的数据 user name age ,尤其是用户信息之类的,经常变动的信息hashsh更适合对象存储zset(有序集合)127.0.0.1:6379> zadd myset 1 one #添加值 (integer) 1 127.0.0.1:6379> zadd myset 2 two 3 tree #添加多个值 (integer) 2 127.0.0.1:6379> ZRANGE myset 0 -1 1) "one" 2) "two" 3) "tree" ################################################################################### 排序 127.0.0.1:6379> zadd salary 2500 xiaohong #创建三个用户 (integer) 1 127.0.0.1:6379> zadd salary 5000 zhangsan 500lisi (error) ERR syntax error 127.0.0.1:6379> zadd salary 5000 zhangsan (integer) 1 127.0.0.1:6379> zadd salary 500 lisi (integer) 1 127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf #显示全部,从小到大排序 1) "lisi" 2) "xiaohong" 3) "zhangsan" 127.0.0.1:6379> ZREVRANGE salary 0 -1 #从大到小 1) "zhangsan" 2) "lisi" 127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf withscores ##显示全部,并且附带 1) "lisi" 2) "500" 3) "xiaohong" 4) "2500" 5) "zhangsan" 6) "5000" 127.0.0.1:6379> ZRANGEBYSCORE salary -inf 2500 withscores ##显示到2500的用户 1) "lisi" 2) "500" 3) "xiaohong" 4) "2500" ################################################################################### 127.0.0.1:6379> ZRANGE salary 0 -1 1) "lisi" 2) "xiaohong" 3) "zhangsan" 127.0.0.1:6379> ZREM salary xiaohong #移除有序集合的指定元素 (integer) 1 127.0.0.1:6379> ZRANGE salary 0 -1 1) "lisi" 2) "zhangsan" 127.0.0.1:6379> ZCARD salary #获取有序集合的元素个数 (integer) 2 ################################################################################### 127.0.0.1:6379> zadd k1 1 hello 2 world 3 xiaohei (integer) 3 127.0.0.1:6379> ZCOUNT k1 1 3 #获取指定区间的成员数量 (integer) 3 127.0.0.1:6379> ZCOUNT k1 1 2 (integer) 2
2021年09月20日
267 阅读
0 评论
0 点赞