当前位置:网站首页>Summary of redis basic knowledge points
Summary of redis basic knowledge points
2022-07-06 04:58:00 【Dream demon】
Redis Default port 6379
One 、 install
1. Go to redis.cn Check out the latest stable download address ,http://download.redis.io/releases/redis-6.0.6.tar.gz
2. download wget http://download.redis.io/releases/redis-6.0.6.tar.gz
3. decompression tar -zxvf redis-6.0.6.tar.gz
4. Move Put it in /usr/local/redis Under the table of contents sudo mv redis-6.0.6 /usr/local/redis/ because redis directory does not exist , So it is equivalent to renaming
5. Get into redis Catalog cd /usr/local/redis-6.0.6
6. Generate sudo make because redis yes C language-written , This step is equivalent to compiling
If something goes wrong , Probably not installed gcc and make, After installation make distclean In addition to compiling the content, recompile *
7. test sudo make test
8. install sudo make install take redis The command to install to /usr/local/bin/ Catalog
9. Get into /usr/local/bin View in cd /usr/local/bin ls -all
10. Copy the configuration file to /etc/ Under the table of contents The configuration file is /usr/local/redis/redis.conf
sudo cp /usr/local/redis/redis.conf /etc/redis/
Two 、 Basic use
Start server
redis-server Start server
ps aux|grep redis See if it starts
redis-server --help View help for startup
sudo redis-server /etc/redis/redis.conf Specify the loaded profile
Start client
redis-cli Start client
redis-cli --help View startup help , Parameters can be added ( host 、 Ports, etc )
redis-cli -h ip Address -p Port number
ping
After starting the client, enter ping If the result is PONG Indicates that the client has successfully connected redis Server side
select
Switch database
select Numbers The range of numbers is 0-databases Maximum minus 1
The default is in select 0
help
help Command name View help for using this command
Core configuration options
The configuration information is in /etc/redis/redis.conf
bind 127.0.0.1 If you need remote access , This line can be annotated or bound to a real ip
port 6379 Port number , Default 6379
daemonize yes Set to run as a daemon ,no Is not a daemon
If running as a daemons , Will not block on the command line , Similar to service , To ensure the redis The stable operation of the service in the background
If running as a non daemon , The current terminal is blocked
databases 16 redis The default is 16 A database
logfile Log path
dbfilename dump.rdb Data files , It is stored in the home directory by default
dir /var/lib/redis Data file storage path
slaveof Master slave copy , Similar to dual backup
3、 ... and 、 Basic data structure
redis yes key:value Data structure of , Each data is a key value pair
Character type of key type , And can't repeat
Value types can have certain data structures , It can be a string string、 list list、 Hash hash、 aggregate set、 Ordered set zset
Four 、 Additions and deletions
string type
stay redis Of string type value The maximum length of data that can be held is 512M
set Set up
set name LiKang
set age 22
get obtain
get name
get age
del Delete
del name
del age
Set when inserting key The expiration time of
setex key Number of seconds value
ttl key Check out this key The validity of the ,-1 For permanent existence
mset Set up multiple key-value
mset name Liang age 22 Be careful : If... Is set later name Before name The value of will be overridden
mget key1 key2 Get multiple values
append Additional
append key value
Key command
del key1 key2 Delete this key The key/value pair
keys * See what they have key
keys a* see a At the beginning key
exists key see key Whether there is ,1 There is ,0 There is no
type key Check out this key Of value The type of
expire key Number of seconds to key Set expiration time
hash type
key The value of is attribute : value , Such key value pairs , The property value is string type
hset key field value Set up
for example hset person name LiKang
hget key field obtain value value
for example hget person name
hmset key field1 value1 field2 value2 Set multiple values
hmget key field1 field2 Get multiple values
hgetall key Access to this key All key value pairs under
hkeys key Access to this key All properties under
hvals key Access to this key All attribute values under
hdel key field1 field2 Delete a property
del key Delete this key The key/value pair
If it is updated, please hset key field value Just insert , The same field will automatically overwrite the original data to realize the update
list type
The list element type is string, And the data is sorted according to the insertion order , Elements can be repeated
lpush Insert... From the left
lpush key value1 value2 The return value is key in value The number of
lrange key num1 num2 Get from left to right num1 To num2 The element values between ( Include num2),0 Represents the first element ,-1 Represents the last element ,l representative list, No left
rpush Insert... From the right
rpush key value1 value2 The return value is key in value The number of
nothing rrange command
lrem key count value Remove data ,l representative list, No left
count:0 On behalf of the delete key All values in are value The elements of
count>0 Delete the left value as value The elements of , The number of deletions is count
count<0 Delete the value on the right as value The elements of , The number of deletions is count
lset key Subscript value Update data
linsert key before(after) value1 value2 stay value1 In front of ( Back ) Insert value2
set type
unordered set , Element is string type , Element is unique , No repetition , There is no modification operation for the collection
sadd key member1 member2 Add data
smembers key get data
srem key member Remove data
zset type
Ordered set , Element type is string, Element is unique , No repetition , Each element is associated with a double Type of score, Said the weight , Sort elements from small to large by weight , And there is no set modification operation
zadd key score1 member1 score2 member2 Add data
If score The value of already exists , The new element will be inserted into the score After or before elements with the same value
If member The value of already exists , It will be modified socre The value of is the new value
zrange key num1 num2 obtain num1 To num2 Data between ( Include num2)
zrangeby score key min max obtain score Values in min To max Member between ( Include min and max)
zscore key member return member Of score value
zrem key member1 member2 Deletes the specified element
zremrangebyscore key min max Delete score Values in min To max Elements in scope ( Include min and max)
5、 ... and 、 Build master slave
One master You can have multiple slave, One slave You can have more than one slave, Go on like this , Form a powerful multi-level server cluster architecture .
master To write data ,slave To read data , Statistics , The read-write ratio of the website is 10:1, That is to say 11 Only one of the users is writing data .
Read and write separation can be realized through master-slave configuration .
Configure the primary server
View the current host's ip Address ifconfig
modify /etc/redis/redis.conf file bind Current host ip
restart redis service
sudo service redis stop
sudo redis-server redis.conf
Configure slave
Copy /etc/redis/redis.conf file sudo cp redis.conf ./slave.conf
modify slave.conf file
bind host ip ( If it is a different computer from the main server , Bind native ip that will do )
port 6378
Add a row slaveof Current host ip Primary server port ( Default 6379)
start-up redis service sudo redis-server slave.conf
View master-slave relationship redis-cli -h Current host ip info Replication
PS: The master server can read and write , You can only read from the server , And the master-slave server data can be displayed synchronously
start-up redis-cli -h host ip -p Port number
6、 ... and 、 Set up the cluster
Although one master can follow more , But if the number of simultaneous visits is too large (1000w), The main service will be dead for sure ; Access to natural disasters , The data service is down
Big companies have a lot of servers .
The concept of cluster
A cluster is a group of independent , A computer connected by a high-speed network , They form a group , And managed in a single system model . When a client interacts with a cluster , Clustering is like a stand-alone server , Cluster configuration is used to improve availability and scalability . When the request arrives, it is first handled by the load balancing server , Forward the request to another server .
classification
Software level : There's only one computer , On this computer, several redis service
Hardware level : Computers with multiple entities , There's one on every computer redis Or more redis service
Cluster building
At one ubuntu Build a cluster on the machine
Machine configuration 1
stay Desktop Create a folder in the directory “redis_cluster", Create... Under this folder 7000.conf, The contents are as follows
# Port number
port 7000
# host ip
bind 192.168.37.129
# Whether to guard ( backstage ) Run in process mode
daemonize yes
# pid file
pidfile 7000.pid
# Whether to use cluster
cluster-enabled yes
# Cluster files
cluster-config-file 7000_node.conf
# The timeout of the cluster
cluster-node-timeout 15000
# Backup related
appendonly yes
stay ”redis_cluster“ Create under folder 7001.conf、7002.conf, except 7000 Change it , The content is the same as above
Machine configuration 2
And configure the machine 1 equally , stay Desktop Create under directory redis_cluster Folder , It creates 7003.conf、7004.conf、7005.conf, In addition to putting the host ip and 7000 Change it , Everything else is the same .
But there is only one machine here , So it will also 7003.conf、7004.conf、7005.conf Two files on the machine 1 On
Create clusters
First in redis_cluster Start under folder redis service
sudo redis-server 7000.conf
sudo redis-server 7001.conf
sudo redis-server 7002.conf
sudo redis-server 7003.conf
sudo redis-server 7004.conf
sudo redis-server 7005.conf
redis The installation package for contains redis-trib.rb, For creating clusters
The next operation is on the machine 1 on
Copy the command , This allows you to call this command in any directory
sudo cp /usr/share/doc/redis-tools/examples/redis-trib.rb /usr/local/bin/
redis 5.0 Previous versions created clusters
install ruby Environmental Science , because redis-trib.rb Yes, it is ruby Developed
sudo apt-get install ruby
Create clusters ( Must be on first redis service )
–replicas 1 Indicates that the cluster mode created is “ A master from ”, There are three nodes (Node)
redis-trib.rb create --replicas 1 192.168.37.129:7000 192.168.37.129:7001 192.168.37.129:7002 192.168.37.129:7003 192.168.37.129:7004 192.168.37.129:7005
redis 5.0 Later versions create clusters
5.0 Version discards redis-trib
Run the following command directly ,–cluster-replicas 1 Indicates that the cluster mode created is “ A master from ”, There are three nodes (Node)
redis-cli --cluster create 192.168.37.129:7000 192.168.37.129:7001 192.168.37.129:7002 192.168.37.129:7003 192.168.37.129:7004 192.168.37.129:7005 --cluster-replicas 1
(0 slots) by slave service , No 0 For the master service (redis 5.0 In previous versions, no matter slave still master Are shown as master)
Connect to cluster
redis-cli -h host ip -p Port number -c
get key get data , If the data is not in the current node, it will be redirected automatically
Cluster considerations
When inserting data , If the connection is from the server , Will pass CRC16 The algorithm redirects to another master server for insertion .
If the primary server is connected , Also through CRC16 The algorithm redirects to another master server for insertion .
Every node in the cluster has an equal relationship , Each node saves its own data and the status of the entire cluster , Each node is connected to all other nodes , And those connections stay active , This ensures that we only need to connect to any node in the cluster , You can get data from other nodes .
The cluster will store the data in a master node , And then here master Corresponding to it slave Data synchronization between , Only when one master After hanging up , To start a corresponding slave node , act as master
There must be three or more master nodes , Otherwise, it will fail to create the cluster , And when the number of surviving primary nodes is less than half of the total nodes , The entire cluster will not be able to provide services .
边栏推荐
- RT thread analysis - object container implementation and function
- Selection of slow motion function
- 内核判断i2c地址上是否挂载外设
- The IPO of mesk Electronics was terminated: Henan assets, which was once intended to raise 800 million yuan, was a shareholder
- Sorting out the knowledge points of multicast and broadcasting
- 图论的扩展
- Biscuits (examination version)
- [buuctf.reverse] 159_ [watevrCTF 2019]Watshell
- Rce code and Command Execution Vulnerability
- MySQL time processing
猜你喜欢
Programmers' position in the Internet industry | daily anecdotes
Ue5 small knowledge points to enable the setting of lumen
ISP learning (2)
二叉树基本知识和例题
idea一键导包
Postman前置脚本-全局变量和环境变量
[05-1, 05-02, 05-03] network protocol
ISP学习(2)
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Visio draw fan
随机推荐
Class inheritance in yyds dry inventory C
[buuctf.reverse] 159_[watevrCTF 2019]Watshell
IPv6 comprehensive experiment
Extension of graph theory
Delete subsequence < daily question >
Platformio create libopencm3 + FreeRTOS project
Cve-2019-11043 (PHP Remote Code Execution Vulnerability)
Codeforces Round #804 (Div. 2)
DMA use of stm32
The web project imported the MySQL driver jar package but failed to load it into the driver
你需要知道的 TCP 三次握手
Zynq learning notes (3) - partial reconfiguration
集合详解之 Collection + 面试题
Idea one key guide package
Supreme Court, judgment standard of divorce cases
Postman前置脚本-全局变量和环境变量
Selection of slow motion function
【LGR-109】洛谷 5 月月赛 II & Windy Round 6
[05-1, 05-02, 05-03] network protocol
Simple understanding of interpreters and compilers