In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "the case analysis of the use of Redis". In the daily operation, I believe that many people have doubts about the analysis of the use of Redis. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "case analysis of the use of Redis"! Next, please follow the editor to study!
Redis1. Basic knowledge
Redis is a memory database, power outage, data loss, process restart, data loss
Data persistence of redis needs to be configured to prevent data loss
Redis supports ms replication, read-write separation, single point of failure prevention and data loss.
1.1. Installation
Rpm package installation
Yum automatic installation, in Ali's yum warehouse, there are redis software packages
Yum install redis-y
Source code compilation and installation
# 1. Download redis source code wget http://download.redis.io/releases/redis-4.0.10.tar.gz# 2. Extract tar-zxf redis-4.0.10.tar.gz# 3. Switch to the redis source directory cd redis-4.0.10.tar.gz# 4. Compile the source file make # 5. After compilation, the compiled redis directive # 6.make install is installed to the specified directory in the src/ directory, default to / usr/local/binmake install DESTDIR=/your/dir1.2. Configuration file
Modify redis.conf, change default port, set password, turn on safe mode, etc.
For redis installed with yum, the default configuration file is / etc/redis.conf
Profile parameter interpretation
# vim / etc/redis.conf # Open the following parameters # here is the startup address of binding redis, if you support remote connection, change it to 0.0.0.0bind 0.0.0.0 # change port port 650 settings redis password requirepass haohaio# turns on safe mode protected-mode yes # Open a parameter daemonize yes1.3 running in redis background. Why can't I connect when I start Redis# using systemctl start redis? # because this command is connected to port 6379 by default, we have changed the redis port, so we cannot connect. # Please use the following command to specify the configuration file to start [root@s25linux opt] # redis-server / etc/redis.conf
Check process status
# check the progress of redis [root@s25linux opt] # ps-ef | grep redisroot 6498 10 11:42? 00:00:00 redis-server 0.0.0.0 ps 65001.4. Start Redis client connection server # connect redis server, specify ip address and port, and password connection redis#-p specify port #-h specify ip address # auth instruction for password authentication [root@s25linux opt] # redis-cli-p 6500-h 192.168.178.143192.168.178.143redis# > ping (error) NOAUTH Authentication required.192.168.178.143:6500 > auth haohaioOK192.168.178.143:6500 > pingPONG1.5. The common command 1.keys * lists all redis key2.type key view key type 3.expire key seconds expiration time 4.ttl key view key expiration time-2 indicates that key no longer exists 5.persist cancels key expiration time-1 indicates that key exists No expiration time 6.exists key determines the existence of key returns 1 otherwise 07.del keys deletion key can delete multiple 8.dbsize calculation key quantity 2.RDB persistence
Redis provides the RDB persistence function, which can save the state of redis in memory to the hard disk, which can be executed manually.
It can also be configured in redis.conf and executed on a regular basis.
The RDB file produced by RDB persistence is a compressed binary file that is saved on the hard disk and through which redis can restore the database's current state.
Configure the data persistence of the rdb mechanism, the data file is an unintelligible binary file, and configure the trigger time mechanism.
Vim s25_rdb_redis.conf, write the following
Daemonize yes # background run port 6379 # port logfile / data/6379/redis.log # specify redis's operation log, store location dir / data/6379 # specify redis's data file, store path dbfilename s25_dump.rdb # specify file name of data persistence bind 127.0.0.1 # specify redis's running ip address # redis trigger save instruction Time mechanism for data persistence 1 modified command operation within # 900s, such as set .mset, delsave 9001 # 10 modified class operations save 300 1mm 60 seconds 10000 modified class operations save 60 10000
Create a data folder for redis
Mkdir-p / data/6379
Kill all previous redis to prevent disrupting the experiment
[root@s25linux s25redis] # pkill-9 redis
Specify the redis configuration file with rdb configured, and start
Redis-server s25_rdb_redis.conf
If there is no persistence time mechanism that triggers redis, the data file will not be generated and the data restart process will be lost.
You can write a script to let redis manually execute save commands to trigger persistence, which can be triggered by typing save directly into the redis command line.
127.0.0.1 age 6379 > set addr shaheOK127.0.0.1:6379 > 127.0.0.1 keys 6379 > set age 18OK127.0.0.1:6379 > 127.0.0.1 keys 6379 > 127.0.0.1 keys * 1) "age" 2) "addr" 3) "name" 127.0.0.1 Swiss 6379 > saveOK
After the rdb persistent file exists, restart the redis process and the data will not be lost. After restarting, redis will read the data in the dump.rdb file.
What is the disadvantage of rdb? if the persistence mechanism is not triggered, there will be machine downtime and data will be lost, so redis has a better aof mechanism.
3.AOF persistence
AOF (append-only log file)
Record all change operation commands executed by the server (such as set del, etc.) and restore the dataset by re-executing these commands when the server starts
All the commands in the AOF file are saved in the redis protocol format, and the new command is appended to the end of the file.
Advantages: maximum protection against data loss
Cons: logging is very large
Redis-client write data > redis-server synchronization command > AOF file
Create a new configuration file and configure the following parameters.
Vim s25_aof_redis.conf AOF persistent configuration. Two parameters appendonly yesappendfsync always always modify the operation of the class everysec to persist once per second no depends on the cache size mechanism that comes with the system
For example, the parameter is configured to
Daemonize yesport 6379logfile / data/6379aof/redis.logdir / data/6379dbfilename dbmp.rdbrequirepass redhatsave 900 1save 300 10save 60 10000appendonly yesappendfsync everysec
Create a data folder for aof
Mkdir-p / data/6379aof
Start aof's Redis database
Redis-server s25_aof_redis.conf
The database of the aof mechanism will generate the aof data file when it is started for the first time, as follows
[root@s25linux 6379aof] # lsappendonly.aof redis.log
Now that the configuration is over, here are the experimental steps
Log in to redis and write data
[root@s25linux s25redis] # redis-cli127.0.0.1:6379 > 127.0.0.1 keys * (empty list or set) 127.0.0.1 keys 6379 > set name zhunbeixiakechifanOK127.0.0.1:6379 > set name2 xinkudajialeOK127.0.0.1:6379 > keys * 1) "name2" 2) "name"
The write operation will be recorded in the aof file log
Kill all redis processes and restart
[root@s25linux s25redis] # pkill-9 redis [root@s25linux s25redis] # redis-server s25_aof_redis.conf
Redis's aof persistence mechanism is that when restarting, redis re-executes the commands in the aof file to reproduce the data.
If the aof log file is deleted, the data cannot be recovered
4. One master and one slave data synchronous replication 4.1. Description
The last step in the morning is to demonstrate that when we delete the aof file, or the rdb,aof file is abnormally corrupted, lost, and the data is gone, isn't it?
Running two or more redis on one machine is a function of redis that supports multiple instances. Based on the different port numbers, multiple independent redis databases can be run.
What is multiple instances is a machine running multiple redis independent processes do not interfere with each other independent database called multiple redis database instance, based on the configuration file can be distinguished
The figure is the multi-instance function of redis, and the master-slave synchronization is configured.
4.2. Configuration step
Prepare the configuration files for 2 redis and write them into the following
Vim s25-master-redis.conf
Port 6379 # Port daemonize yes # background run pidfile / s25/6379/redis.pid # loglevel noticelogfile "/ s25/6379/redis.log" # Log storage directory dbfilename dump.rdb # persistence file name dir / s25and6379 # path to save data file protected-mode no # turn on security full mode
Vim s25-slave-redis.conf
Port 6389daemonize yespidfile / s25/6389/redis.pidloglevel noticelogfile "/ s25/6389/redis.log" dbfilename dump.rdbdir / s25/6389protected-mode no# can define the replication relationship directly in the configuration file, and replication slaveof 127.0.0.1 6379 will be established immediately after startup.
Generate 2 redis data folders respectively
Mkdir-p / s25 / {6379
Start 2 redis databases respectively.
[root@s25linux s25redis] # redis-server s25-master-redis.conf [root@s25linux s25redis] # [root@s25linux s25redis] # [root@s25linux s25redis] # redis-server s25-slave-redis.conf
Check their processes and replication relationships separately.
[root@s25linux s25redis] # redis-cli-p 6379 info replication [root@s25linux s25redis] # redis-cli-p 6389 info replication# configure their replication relationship through a command. Note that this command only temporarily configures the replication relationship of redis. # if you want to modify it permanently, you have to modify the configuration file redis-cli-p 6389 slaveof 127.0.0.1 6379.
Instructions after configuration
At this time 6379 = master library 6389 = slave library can write data to 6379 at this time, can be synchronized to 6389 is a read-only database, can not write data 5. One-master, multi-slave and master-slave replication failover 5.1. One master and multiple slaves configuration
Create another configuration file, port is 6399, and add to the one-master-slave replication relationship.
# vim s25-salve2-redis.conf port 6399daemonize yespidfile / s25/6399/redis.pidloglevel noticelogfile "/ s25/6399/redis.log" dbfilename dump.rdbdir / s25/6399protected-mode noslaveof 127.0.0.1 6379
Create a data folder
Mkdir-p / s25amp 6399
At this point, you can start the database of 6399 and view his identity replication relationship.
[root@s25linux s25redis] # redis-cli-p 6399 info replication5.2. Fail-over
Fault simulation
Environment preparation, prepare three database instances of redis: 6379 (master), 6389 (slave 1), 6399 (slave 2), and configure the relationship between one master and two slaves
[root@s25linux s25redis] # ps-ef | grep redisroot 11294 10 15:19? 00:00:01 redis-server *: 6379root 11310 10 15:19? 00:00:01 redis-server *: 6389root 11620 10 15:33? 00:00:00 redis-server *: 6399
View replication relationships separately
[root@s25linux s25redis] # redis-cli-p 6379 info replication# Replicationrole:masterconnected_slaves:2slave0:ip=127.0.0.1,port=6389,state=online,offset=1883,lag=1slave1:ip=127.0.0.1,port=6399,state=online,offset=1883,lag=1
At this time, simulate the fault and directly kill the main library.
Kill-9 11294
At this time, there are two solitary slave libraries left, without a master, and the data has not been sent and written, so it is very uncomfortable.
At this time, one from the library (6399), not happy, turned to the serf as the master, and removed his identity from the library.
Without this yoke of obedience to the library, I am my own master.
[root@s25linux s25redis] # redis-cli-p 6399 slaveof no one
At this time, 6399 is already the main library. Change the replication information of 6389 to 6399.
[root@s25linux s25redis] # redis-cli-p 6389 slaveof 127.0.0.1 6399
Check their replication relationship at this time
[root@s25linux s25redis] # redis-cli-p 6389 info replication [root@s25linux s25redis] # redis-cli-p 6399 info replication
At this point, you can write data to the main library 6399, and 6389 can view the data.
The scene in which the main library does not hang up and dies from the library.
Hang up from the library, it doesn't matter, re-establish a slave library, and add master-slave copy. , .
You will find that it is very difficult to switch replication relationships manually. What should you do if the redis main library suddenly dies at 4: 00 a. M. at night? Is your wife willing to let you get up and work?
So what should you do? Do you need to learn some other skills? Do you wish there was anything that could help you?
If you have money, you get Jarvis.
I hope someone can help you keep an eye on the master-slave replication for 24 hours, and automatically help you switch between master and slave after finding that the master library is down.
6. Highly available Sentinel sentinel6.1. The working principle of configuring the redis sentinel process is generally to use three sentinels (security guards) to stare at the redis master database and constantly ask whether it is alive. If there is no response beyond 30s (the set time threshold), the three sentinels will judge that the master library is down and talk about the voting mechanism, because the three sentinels have to automatically select the slave library as the new master library, and each sentry may have different opinions. Therefore, it leads to the voting mechanism, and the minority is subordinate to the majority. When multiple Sentinels reach an agreement, select a slave library phase, automatically modify their configuration files, and switch to a new master library. If the master library is down at this time, the Sentinel will automatically add it to the cluster after it is restored. and automatically assigned to the new slave library these are automated, without human intervention, thieves fart 6.2. Architecture
6.3.Redis configuration
Prepare 3 redis nodes, 1 master and 2 slaves redis cluster
# redis supports multiple instances-based on multiple configuration files Run multiple redis independent processes s25-redis-6379.conf-master port 6379daemonize yeslogfile "6379.log" dbfilename "dump-6379.rdb" dir "/ var/redis/data/" s25 dbfilename dump-6380.rdb 6380.confs-slave 1port 6380daemonize yeslogfile "6380.log" dump-6380.rdb "dir" / var/redis/data/ "slaveof 127.0.0.1 6379s25Coli 6381.confs-slave 2port 6381daemonize yeslogfile" 6381 .log "dbfilename" dump-6381.rdb "dir" / var/redis/data/ "slaveof 127.0.0.1 637 view 3 configuration files Prepare to start the process [root@s25linux s25sentinel] # lss25-redis-6379.conf s25-redis-6380.conf s25-redis-6381.conf separately
Create a data store folder
Mkdir / etc/redis/data
After starting three processes respectively, check the process.
[root@s25linux s25sentinel] # redis-server s25-redis-6379.conf [root@s25linux s25sentinel] # redis-server s25-redis-6380.conf [root@s25linux s25sentinel] # redis-server s25-redis-6381.conf [root@s25linux s25sentinel] # ps-ef | grep redisroot 20413 10 08:45? 00:00:00 redis-server *: 6379root 20417 1 0 08:45? 00:00:00 redis-server *: 6380root 20422 1 0 08:45? 00:00:00 redis-server *: 6381
Determine the master-slave relationship of the three libraries
[root@s25linux s25sentinel] # redis-cli-p 6379 info replication# Replicationrole:masterconnected_slaves:2slave0:ip=127.0.0.1,port=6380,state=online,offset=183,lag=1slave1:ip=127.0.0.1,port=6381,state=online,offset=183,lag=16.4.sentinel configuration
Prepare the configuration files of the three sentinels respectively, and modify them as follows: the configuration files of the three sentinels are only different port numbers.
# vim s25-sentinel-26379.conf port 26379 dir / var/redis/data/logfile "26379.log" / / current Sentinel Node Monitoring 192.168.119.106379 this master node / / 2 means that at least two Sentinel nodes are required to agree / / mymaster is the alias sentinel monitor mymaster 127.0.0.1 6379 2 of the master node / each Sentinel node needs periodic PING commands to determine whether the Redis data node and the rest of the Sentinel nodes are reachable / / if it exceeds 30000 milliseconds for 30s and there is no reply, it is determined that the sentinel down-after-milliseconds s25msredis 30000 beat is unreachable / when the Sentinel node set agrees on the failure determination of the master node, the Sentinel leader node will perform a failover operation and select a new master node. / / the original slave node will initiate a copy operation to the new master node. Limit the number of slaves that initiate a replication operation to a new master node to 1sentinel parallel-syncs mymaster 1 1sentinel parallel-syncs mymaster / failover timeout 180000 milliseconds sentinel failover-timeout mymaster 180000daemonize yes# = = # vim s25-sentinel-26380.conf port 26380dir / var/redis/data/logfile "26380.log" sentinel monitor mymaster 127.0.0.1 6379 2sentinel down-after-milliseconds mymaster 30000sentinel parallel-syncs mymaster 1sentinel failover-timeout mymaster 180000daemonize yes# = # s25-sentinel-26381.conf port 26381dir / var / redis/data/logfile "26381.log" sentinel monitor mymaster 127.0.0.1 6379 2sentinel down-after-milliseconds mymaster 30000sentinel parallel-syncs mymaster 1sentinel failover-timeout mymaster 180000daemonize yes
Start 3 Sentinel processes and view process information
# 1. Start the Sentinel process [root@s25linux s25sentinel] # redis-sentinel s25-sentinel-26379.conf [root@s25linux s25sentinel] # redis-sentinel s25-sentinel-26380.conf [root@s25linux s25sentinel] # redis-sentinel s25-sentinel-26381.conf# 2. View process information [root@s25linux s25sentinel] # ps-ef | grep redisroot 20413 10 08:45? 00:00:00 redis-server *: 6379root 20417 10 08:45? 00:00:00 redis-server *: 6380root 20422 10 08:45? 00:00:00 redis-server *: 6381root 20614 10 08:55? 00:00:00 redis-sentinel *: 26379 [sentinel] root 20618 10 08:55? 00:00:00 redis-sentinel *: 26380 [sentinel] root 20622 10 08:55? 00:00:00 redis-sentinel *: 26381 [sentinel]
You can check the configuration file of the sentry, as well as the status of the sentry.
[root@s25linux s25sentinel] # redis-cli-p 26379 info sentinel# Sentinelsentinel_masters:1sentinel_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0sentinel_simulate_failure_flags:0master0:name=s25msredis,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=36.5. Simulated fault
After the Sentinel is built, the simulation kills the master library, and then waits for an automatic switch between the master and the slave.
Check the progress of 6379, and after killing, the sentry can vote automatically.
The remaining slave is the new master, and then reassign the master-slave relationship
Fix the failure, repair the 6379 redis database and check one of its replication relationships
6379 the database will be re-replicated to the master-slave and become a new slave library.
If you want to restore their master-slave relationship, drop all kill and restart, the master-slave relationship will be assigned by default with the configuration file.
7.redis-cluster build 7.1. Prepare the node
Prepare 6 horses, that is, 6 redis nodes, that is, 6 configuration files, and use at least 6 redis cluster nodes
These six configuration files are just different port numbers.
Create a s25rediscluster folder under the root directory for experiments
Mkdir / s25rediscluster
S25-redis-7000.conf
Port 7000daemonize yesdir "/ opt/redis/data" logfile "7000.log" dbfilename "dump-7000.rdb" cluster-enabled yes # enable cluster mode cluster-config-file nodes-7000.conf # configuration files inside the cluster # redis cluster requires 16384 slot to be normal to provide services, # in other words, as long as any one cluster exception, the entire cluster does not provide services. Therefore, the production environment is generally nocluster-require-full-coverage no
Use the sed command to quickly generate other configuration files
# s25-redis-7001.conf# s25-redis-7002.conf# s25-redis-7003.conf# s25-redis-7004.conf# s25-redis-7005.conf# use the following command to quickly create sed's s25-redis-7000.conf 7001 s25-redis-7000.conf > s25-redis-7001.confsed 's 7002 s25-redis-7000.conf > s25-redis-7002.confsed 's 7002 s25-redis-7000.conf-7003 s25-redis-7000.conf- Redis-7003.confsed 's s25-redis-7004.confsed 7004 s25-redis-7000.conf > s25-redis-7004.confsed 's 7000Compact 7005amp s25-redis-7000.conf > s25-redis-7005.conf
Create a data folder
Mkdir-p "/ opt/redis/data"
Start 6 redis nodes respectively and check the process
Redis-server s25-redis-7000.confredis-server s25-redis-7001.confredis-server s25-redis-7002.confredis-server s25-redis-7003.confredis-server s25-redis-7004.confredis-server s25-redis-7005.conf
Data cannot be written at this time
# at this point, you try to write data to see if you can write it in, you can't write data, and you haven't allocated virtual slots yet. # We just started six redis nodes, got six horses ready, and the baskets on the horses haven't been allocated yet. You want to configure 7.2.ruby.
Direct yum installs the ruby interpreter ruby is an interpretive programming language like python, developed by the Japanese god
# gem is ruby's package management tool # ruby = python # gem = pip3 # method 1:yum installation: yum install ruby-y # method 2: for compilation and installation, use: wget. Tar. / configure-- prefix=/opt/ruby/make & & make install
Check the environment of ruby and gem
[root@s25linux s25rediscluster] # ruby-vruby 2.0.0p648 (2015-12-16) [x86_64-linux] [root@s25linux s25rediscluster] # gem-v2.0.14.1
Download the ruby module for operating redis, which is used to create a cluster
Wget http://rubygems.org/downloads/redis-3.3.0.gem
Install this module with gem, and ruby can operate the redis database.
Gem install-l redis-3.3.0.gem # is like the pip3 install xxxx of python, it is not difficult to understand that # you can check which packages are available in gem gem list-- check redis gem
Search for the script for ruby to create a redis cluster and add it to the environment variable
How does # redis-trib.rb know its absolute path? # which is the absolute path to search for commands in PATH environment variables! # find is the file path on the search system! Find /-name "redis-trib.rb" # will by default add the script for creating the cluster to the environment variable under the compilation and installation path of the redis database, for example, your script location is / opt/redis/src/redis-trib.rb, and then copy it to any environment variable. Cp / opt/redis/src/redis-trib.rb / usr/local/bin/
One click to create a cluster, and automatically allocate slots, you can write data. The cluster automatically assigns master-slave relationships.
# for each master node, there is a slave node, which represents-- replicas 1redis-trib.rb create-- replicas 1 127.0.0.1VR 7000 127.0.0.1 7001 127.0.0.1VOR 7002 127.0.1VR 7003 127.0.1VOR 7004 127.0.1VOR 7005
You can view and check the status of the cluster at this time.
View statu
Redis-cli-p 7000 cluster info redis-cli-p 7000 cluster nodes # is equivalent to viewing nodes-7000.conf file node information # Cluster master node status redis-cli-p 7000 cluster nodes | grep master# cluster slave node status redis-cli-p 7000 cluster nodes | grep slave
Check statu
[root@yugo / opt/redis/src 18:42:14] # redis-cli-p 7000 cluster infocluster_state:okcluster_slots_assigned:16384cluster_slots_ok:16384cluster_slots_pfail:0cluster_slots_fail:0cluster_known_nodes:6cluster_size:3cluster_current_epoch:6cluster_my_epoch:1cluster_stats_messages_ping_sent:10468cluster_stats_messages_pong_sent:10558cluster_stats_messages_sent:21026cluster_stats_messages_ping_received:10553cluster_stats_messages_pong_ Received:10468cluster_stats_messages_meet_received:5cluster_stats_messages_received:21026
Enter the cluster to write data and view data redirection
# testing and writing cluster data You must use redis-cli-c-p 7000 to log in to the cluster. You must add-c parameter redis-cli-c-p 7000127.0.0.1 set name chao 7000 > set name chao-> Redirected to slot [5798] located at 127.0.1 set name chao 7001 OK127.0.0.1:7001 > exit [root@yugo / opt/redis/src 18:46:07] # redis-cli-c-p 7000127.0.0.1 keys 7000 > pingPONG127.0.0.1:7000 > keys * (empty list or set) 127.0.0.1 Redirected to slot 7000 > get name- > Redirected to slot [5798] located at 127.0.0.1 Redirected to slot 7001 "chao"
How it works:
The redis client accesses a redis instance arbitrarily. If the data is not in the instance, redirect the client to access the required redis instance.
At this point, the study of "case analysis of the use of Redis" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.