当前位置:网站首页>Advanced redis applications [password protection, data persistence, master-slave synchronization, sentinel mode, transactions] [not completed yet (semi-finished products)]
Advanced redis applications [password protection, data persistence, master-slave synchronization, sentinel mode, transactions] [not completed yet (semi-finished products)]
2022-07-03 03:24:00 【Johnny. G】
redis Advanced applications 【 Password protection 、 Data persistence 、 Master slave synchronization 、 Sentinel mode 、 Business 】
1、 Password protection
to redis Server set password
Can pass redis The configuration file set password parameters , So the client connects to redis The service needs password verification , This will make your redis The service is safer .
Check whether password authentication is set :
127.0.0.1:6379> CONFIG GET requirepass
1) "requirepass"
2) ""
Modify this parameter with the following command
127.0.0.1:6379> CONFIG SET requirepass "123"
OK
127.0.0.1:6379> CONFIG GET requirepass
(error) NOAUTH Authentication required.
After setting password verification, you must verify it before you can perform other operations :
127.0.0.1:6379> AUTH 123
OK
127.0.0.1:6379> CONFIG GET requirepass
1) "requirepass"
2) "123"
Be careful : The command settings are only valid at this time , Failure after restarting the service .
127.0.0.1:6379> quit
[[email protected] ~]# systemctl restart redis
[[email protected] ~]# redis-cli
127.0.0.1:6379> CONFIG GET requirepass
1) "requirepass"
2) ""
To be permanent , You need to modify the configuration file and restart the service .
vim /etc/redis.conf
requirepass 123456
[[email protected] ~]# systemctl restart redis
3、 Client login
[[email protected] ~]# redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> CONFIG GET requirepass
1) "requirepass"
2) "123456"
or
Use in interactive mode 【auth password 】 command
[[email protected] ~]# redis-cli
127.0.0.1:6379> AUTH 123456
OK
127.0.0.1:6379> CONFIG GET requirepass
1) "requirepass"
2) "123456"
127.0.0.1:6379> quit
2、 Data persistence
1、 brief introduction
redis For the integrity and security of its own data ,redis The data in memory needs to be synchronized to disk frequently , This process is called a persistence operation . Start again next time redis The service , The data saved on the disk will be reloaded into memory .
Common persistence methods :
a、 Snapshot based approach :redis Synchronize the data in the memory to the disk file according to a certain period
b、 Append based on file :redis Will put right redis The command causing the change of data is recorded in the log file , Then restart again , In the execution log file redis The operation of writing , Achieve data restore .
2、 Snapshot based persistence
Modify the configuration file , Turn on the snapshot based option
save 900 1 #900 If it exceeds in seconds 1 individual key Be modified , Then initiate snapshot saving
save 300 10 #300 Second content if more than 10 individual key Be modified , Then initiate snapshot saving
save 60 10000 #60 Second content if more than 10000 individual key Be modified , Then initiate snapshot saving
# The above is the default configuration of the system
Files kept on disk
[[email protected] ~]# egrep "^(dbfilename|dir)" /etc/redis.conf
dbfilename dump.rdb # Keep the file name
dir /var/lib/redis # Keep your path
[[email protected] ~]# cd /var/lib/redis/
[[email protected] redis]# ls
dump.rdb
Simulate deleting files , Save manually , Check... After restart
[[email protected] redis]# systemctl stop redis
[[email protected] redis]# rm -f dump.rdb
[[email protected] redis]# systemctl start redis
[[email protected] redis]# ls
[[email protected] redis]# redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> set names tom
OK
127.0.0.1:6379> get names
"tom"
127.0.0.1:6379> bgsave
Background saving started
127.0.0.1:6379> quit
[[email protected] redis]# ls
dump.rdb
[[email protected] redis]# systemctl restart redis
[[email protected] redis]# redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> get names
"tom"
127.0.0.1:6379> quit
3、 Persistent based on file append
Be careful : Default not on
#appendonly # Enable Persistence Based on log file append
appendonly yes
appendfilename "appendonly.aof" # Log files
Backup file cycle
# appendfsync always
appendfsync everysec
# appendfsync no
appendfsync always // Force write to disk every time write command is received , The slowest , But make sure it's completely persistent , It is not recommended to use
appendfsync everysec // Force write to disk every second , A good compromise between performance and persistence , recommend
appendfsync no // Completely dependent on os, Best performance , Persistence is not guaranteed
Restart test
[[email protected] ~]# systemctl restart redis
[[email protected] ~]# ls /var/lib/redis/
appendonly.aof dump.rdb
3、 Master slave synchronization
The role of master-slave replication
1. data redundancy : Master-slave replication realizes hot backup of data , It's a way of data redundancy beyond persistence .
2. Fault recovery : When there is a problem with the master node , Can be served by a slave node , Fast fault recovery ; It's actually a redundancy of services .
3. Load balancing : On the basis of master-slave replication , Cooperate with the separation of reading and writing , Write service can be provided by the master node , Read service provided by slave node ( The write Redis Connect master node when data is applied , read Redis Apply connection from node when data ), Share server load ; Especially in the situation of less writing and more reading , Sharing read load through multiple slave nodes , Can be greatly improved Redis Concurrency of servers .
4. Read / write separation : Can be used to achieve read-write separation , Main library write 、 Read from library , Read write separation can not only improve the load capacity of the server , At the same time, according to the change of demand , Change the number of slave Libraries ;
5. High availability cornerstone : In addition to the above functions , Master slave replication is also the foundation for sentinels and clusters to implement , So master-slave replication is Redis High availability Foundation .
Redis Master slave replication process :
* Slave And master Establishing a connection , send out sync Synchronization command
* Master A background process will be started , Save the database snapshot to a file , meanwhile master The main process will start collecting new write commands and caching .
* After the background is saved , Send this file to slave
* Slave Save this file to your hard disk
- One master You can have multiple slave, One slave You can have more than one slave, Go on like this , Formed a powerful multi-level server cluster architecture
- such as , take ip by 192.168.1.10 Your machine acts as the master server , take ip by 192.168.1.11 The machine acts as a slave server
- Set the configuration of the master server
bind 192.168.1.10
- Set the configuration of the slave server
- Be careful : stay slaveof Write the host later ip, Then write the port , And the port must write
bind 192.168.1.11
slaveof 192.168.1.10 6379
- stay master and slave Separately info command , View the output :info replication
- stay master Write the data on
set hello world
- stay slave Up reading data
get hello
4、 Sentinel mode
High availability solution :
1、cluster colony
2、sentinel sentry
High availability Sentinel sentry
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-E4KeIvXZ-1654621540427)( material \NoSQL\sentinel Sentinel mode .jpg)]
Sentinel The sentry is redis Official high availability solution , It can be used to monitor multiple Redis The operation of the service instance .Redis Sentinel Is a program that runs in a special mode Redis The server .Redis Sentinel It's in multiple Sentinel Working together in a process environment .
Sentinel The system has three main tasks :
1. monitor :Sentinel Constantly check whether the master service and slave server work as expected .
2. remind : Monitored Redis When something goes wrong ,Sentinel The administrator or other applications will be notified .
3. Automatic failover : The master of monitoring Redis Not working properly ,Sentinel The failover operation will begin . Upgrade a slave server to a new master server . Let other slave servers hang to the new master server . At the same time, provide the client with a new master server address
High availability Sentinel Sentinel configuration
Sentry as a pair of redis Monitoring of instances , The robustness and high availability of sentinels are guaranteed by election algorithm , So sentinels should at least deploy 3 platform , Meet the half rule , need 5 perhaps 7, More than half , Not half of the time , To elect leader, To switch between master and slave .
redis service , At least one needs to survive , To ensure the normal operation of the service sentinel , Select new master The principle is that the most recently available and the latest data, the highest priority and the longest active
Sentinel high availability test : Connect the corresponding... Respectively redis Server side , Manually stop the sentry , Stop the master reids service , See whether the master-slave switch is successful .
Three sentinels :redis Hang up two instances , The remaining one can be the master , Automatic switch
The process of building the sentry system , There are several points to pay attention to :
(1) The master and slave nodes in the sentinel system , It is no different from the normal master-slave node , Fault detection and transfer are controlled and completed by sentinels .
(2) Sentinel nodes are essentially redis node .
(3) Every sentinel node , Just configure the monitoring master , You can automatically find other sentinel nodes and slave nodes .
(4) In the sentinel node startup and failover phase , The configuration file of each node will be rewritten (config rewrite).
(5) A sentinel can monitor only one master node ; actually , A sentinel can monitor multiple master nodes , By configuring multiple sentinel monitor That is to say .
Refer to the configuration file
# Whether it is a daemon
daemonize yes
pidfile "/var/run/redis/redis-sentinel.pid"
logfile "/var/log/redis/redis-sentinel.log"
bind 127.0.0.1
port 26379
# working directory
dir "/var/lib/redis"
# Declare that the Sentinel's main library is mymaster, The main library ip And ports are 127.0.0.1 and 6379
# the last one 2 The meaning is , At the sentinel leadership election , The sentry needs to get 2 Tickets can become leader
sentinel monitor mymaster 127.0.0.1 6379 2
# stay mymaster Downtime 30 Subjective offline in seconds
sentinel down-after-milliseconds mymaster 30000
# Specify when failover There can be at most 1 individual slave Simultaneously on the new master To synchronize
sentinel parallel-syncs mymaster 1
# Set the failover timeout to 180 second
# The meaning of this parameter is complex , For details, please refer to the official notes
sentinel failover-timeout mymaster 180000
# Found two slave nodes
sentinel known-slave mymaster 127.0.0.1 6380
sentinel known-slave mymaster 127.0.0.1 6381
#epoch Realize functions similar to version number
sentinel current-epoch 0
5、 Publish subscribe
- The publisher is not planning to send a message to a specific recipient ( subscriber ), It's about sending messages to different channels , There's no need to know what kind of subscriber to subscribe to
- Subscribers are interested in one or more channels , Just receive messages of interest , You don't need to know what kind of publisher
- The decoupling of publishers and subscribers can bring greater scalability and more dynamic network topology
- The message sent by the client to the channel , Will be pushed to all clients who subscribe to this channel
- The client does not need to take the initiative to get the message , Just subscribe to the channel , The content of this channel will be pushed
Format of message
- The format of push message consists of three parts
- part1: Message type , There are three types
- subscribe, Indicates the subscription is successful
- unsubscribe, Indicates that unsubscribe succeeded
- message, Indicates that other terminals publish messages
- If the value of the first part is subscribe, Then the second part is the channel , The third part is the number of channels subscribed now
- If the value of the first part is unsubscribe, Then the second part is the channel , The third part is the number of channels subscribed now , If 0 It means that no channel is subscribed to at present , When in Pub/Sub Outside state , The client can send any message redis command
- If the value of the first part is message, The second part is the name of the source channel , The third part is the content of the message
command
- subscribe
SUBSCRIBE Channel name [ Channel name ...]
- Unsubscribe
- If you don't write parameters , Means to cancel all subscriptions
UNSUBSCRIBE Channel name [ Channel name ...]
- Release
PUBLISH channel news
Example :
127.0.0.1:6379> PUBLISH chan1 "hello redis"
(integer) 1
127.0.0.1:6379> SUBSCRIBE chan1
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "chan1"
3) (integer) 1
1) "message"
2) "chan1"
3) "hello redis"
6、 Business
Redis Transactions can execute more than one command at a time , And with the following two important guarantees :
A transaction is a separate isolation operation : All commands in the transaction are serialized 、 To execute in order . Transaction is in the process of execution , Will not be interrupted by command requests from other clients .
A transaction is an atomic operation : The commands in the transaction are either all executed , Or none of it .
A transaction goes through the following three stages from the beginning to the execution :
Start business .multi
Order to join the team .
Perform transactions .
Serial number | Command and description |
---|---|
1 | DISCARD Cancel the business , Give up executing all commands within the transaction block . |
2 | EXEC Execute commands within all transaction blocks . |
3 | MULTI Mark the beginning of a transaction block . |
4 | UNWATCH Cancel WATCH Command to all key Surveillance . |
5 | WATCH key [key …] Watch one ( Or more ) key , If before the transaction is executed ( Or these ) key Altered by other orders , Then the business will be interrupted . |
7、 Many database
redis There is also a database , The default has been created , Altogether 16 individual , Respectively 0,1,2,…15
redis The default data operation is in 0 No. database .
Key value pairs cannot be shared between databases .
Switch database
select 1 // Switch to 1 The database
Move the key value to the specified database
move address 0 // Assume that the current is 1 The database , take 1 The database address Move to 0 The database
Clear the current database :flushdb
Empty all databases on the server :flushall
Be careful : Empty the database and use it with caution !!!
边栏推荐
- File rename
- docker安装redis
- Edit and preview in the back pipe to get the value writing method of the form
- Limit of one question per day
- 解决高並發下System.currentTimeMillis卡頓
- Agile certification (professional scrum Master) simulation exercise-2
- Parameter index out of range (1 > number of parameters, which is 0)
- Lvgl usage experience
- VS 2019安装及配置opencv
- TCP handshake three times and wave four times. Why does TCP need handshake three times and wave four times? TCP connection establishes a failure processing mechanism
猜你喜欢
Summary of electromagnetic spectrum
I2C subsystem (I): I2C spec
MySql实战45讲【索引】
Nasvit: neural architecture search of efficient visual converter with gradient conflict perception hypernetwork training
Docker install redis
Bid farewell to artificial mental retardation: Mengzi open source project team received RMB 100 million financing to help NLP develop
机械臂速成小指南(八):运动学建模(标准DH法)
Summary of matrix knowledge points in Chapter 2 of Linear Algebra (Jeff's self perception)
LVGL使用心得
MongoDB安装 & 部署
随机推荐
labelimg生成的xml文件转换为voc格式
Positioning (relative positioning, absolute positioning, fixed positioning, Z-index) 2022-2-11
Edit and preview in the back pipe to get the value writing method of the form
On the adjacency matrix and adjacency table of graph storage
Do you really understand relays?
Latest version of NPM: the "NPM" item cannot be recognized as the name of a cmdlet, function, script file, or runnable program. Please check
MongoDB安装 & 部署
L'index des paramètres d'erreur est sorti de la plage pour les requêtes floues (1 > Nombre de paramètres, qui est 0)
VS 2019安装及配置opencv
MongoDB簡介
MongoDB主配置文件
MySQL practice 45 lecture [transaction isolation]
模糊查询时报错Parameter index out of range (1 > number of parameters, which is 0)
敏捷认证(Professional Scrum Master)模拟练习题
程序员新人上午使用 isXxx 形式定义布尔类型,下午就被劝退?
LVGL使用心得
Parameter index out of range (1 > number of parameters, which is 0)
QT based tensorrt accelerated yolov5
The difference between componentscan and componentscans
监听对象中值变化及访问