Linux-Redis installation + actual combat simulation

Redis knowledge

1. Database classification

  • Relational: mysql, oracle, sqlserver, db2, postgresql
  • Non-relational: redis, mongo, ES

2. The importance of Redis

(1) Elegant code
written in C language with fast speed Single-threaded architecture (2) Supports multiple data structures such as strings, hashes, lists, collections, ordered collections (3) Rich functions, natural counters, key expiration functions, message queues (4) ) Support client language multi- php, java, python (5) Data persistence All data runs in memory Support 2 formats of persistent data AOF RDB AOF&RDB (6) Comes with multiple high-availability architectures Master-slave Sentinel cluster
















3. Redis application scenarios

(1) Cache-key expiration time
Save the session session in redis and delete it after expiration.
Cache user information and cache part of Mysql data. The user first accesses redis, redis does not access mysql, and then writes back to the redis
mall coupon expiration time
(2) Ranking List-List & Orderly Collection Hot
/ Hits Ranking List
Live Room Gift Points Ranking
(3) Counter-Natural Support Counter
Post Views Number of
Video Plays Number of
Comments
Like / Dislike
(4) Social Network-Collection
Fans
Common Friends
Interests
Label
(5) Message Queue-Publish and Subscribe
Logs collected with ELK cache

surroundings

Host IP CPU name
192.168.1.20 redis-master
192.168.1.19 redis-slave

Redis installation

1. Create installation and data directories

[root@redis-1 ~]# mkdir -p /data/soft
[root@redis-1 ~]# mkdir -p /opt/redis_cluster/redis_6379/{conf,logs,pid}

2. Download the redis installation package

[root@redis-1 ~]# cd /data/soft/
[root@redis-1 soft]# wget http://download.redis.io/releases/redis-5.0.7.tar.gz
--2020-07-27 14:02:06--  http://download.redis.io/releases/redis-5.0.7.tar.gz
正在解析主机 download.redis.io (download.redis.io)... 45.60.125.1
正在连接 download.redis.io (download.redis.io)|45.60.125.1|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1984203 (1.9M) [application/octet-stream]
正在保存至: “redis-5.0.7.tar.gz”

100%[==================>] 1,984,203   22.5KB/s 用时 95s    

2020-07-27 14:03:41 (20.4 KB/s) - 已保存 “redis-5.0.7.tar.gz” [1984203/1984203])

3. Unzip redis to the data directory

[root@redis-1 soft]# tar xf  redis-5.0.7.tar.gz -C /opt/redis_cluster/
[root@redis-1 soft]# ln -s /opt/redis_cluster/redis-5.0.7/ /opt/redis_cluster/redis   //创建软连接

4. Compile and install redis

[root@redis-1 soft]# cd /opt/redis_cluster/redis
[root@redis-1 redis]# make && make install 
cd src && make all
make[1]: 进入目录“/opt/redis_cluster/redis-5.0.7/src”
    CC Makefile.dep
make[1]: 离开目录“/opt/redis_cluster/redis-5.0.7/src”
make[1]: 进入目录“/opt/redis_cluster/redis-5.0.7/src”
.......

5. Write a configuration file

[root@redis-1 redis]# vim /opt/redis_cluster/redis_6379/conf/6379.conf
bind 127.0.0.1 192.168.1.20   
#填写本机的IP
port 6379
daemonize yes
pidfile /opt/redis_cluster/redis_6379/pid/redis_6379.pid
logfile /opt/redis_cluster/redis_6379/logs/redis_6379.log
databases 16
dbfilename redis.rdb
dir /opt/redis_cluster/redis_6379
[root@redis-1 redis]# redis-server /opt/redis_cluster/redis_6379/conf/6379.conf   // 启动服务
[root@redis-1 redis]# netstat -anpt | grep redis   //查看端口
tcp        0      0 192.168.1.20:6379       0.0.0.0:*               LISTEN      60980/redis-server  
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      60980/redis-server  

Redis basic commands

Enter the redis command line

[root@redis-1 ~]# redis-cli 
127.0.0.1:6379> 

1. Global commands

command Explanation
MSET k1 1 k2 2 k3 3 Create key values ​​in bulk
KEYS * List all key-value names, but prohibited use in corporate environments
DBSIZE See how many keys
EXISTS Check if the key value exists
EXPIRE k2 20 Set K2 expiration time to 20 seconds, k2 will be automatically cancelled after 20 seconds
PERSIST k2 Cancel the expiration time of k2
TTL k2 View the life cycle of k2
TYPE View data type
127.0.0.1:6379> MSET k1 1 k2 2 k3 3 k4 4
OK
127.0.0.1:6379> KEYS *
1) "k4"
2) "k2"
3) "k3"
4) "k1"
127.0.0.1:6379> EXISTS k1
(integer) 1
127.0.0.1:6379> EXISTS k2
(integer) 1
127.0.0.1:6379> EXPIRE k2 20
(integer) 1
127.0.0.1:6379> PERSIST k2
(integer) 1
127.0.0.1:6379> TTL k2
(integer) -1
127.0.0.1:6379> TYPE k2
string

2. String type (string)

command Explanation
SET k3 3 Create key value
GET k3 View key value
OF k2 Delete key value
INCR k3 The key value k3 is an integer, increasing by 1
INCRBY k3 10 Increase the value of k3 by 10
MSET k4 v4 k5 v5 k6 v6 k7 v7 Create key values ​​in bulk
MGET k4 k5 k6 k7 Batch view key values
127.0.0.1:6379> SET k3 3
OK
127.0.0.1:6379> GET k3
"3"
127.0.0.1:6379> DEL k3
(integer) 1
127.0.0.1:6379> INCR k3
(integer) 1
127.0.0.1:6379> INCRBY k3 10
(integer) 11
127.0.0.1:6379> MSET k1 2 k3 4 k5 6 
OK
127.0.0.1:6379> MGET k1 k3 k5 
1) "2"
2) "4"
3) "6"

3. List (list)

command Explanation
RPUSH list1 1 2 3 4 Create a list list1, the value is 1 2 3 4
RPUSH list1 5 6 7 8 Add 5 6 7 8 to the right of list1
LPUSH list1 0 Add 0 to the left of list1
LRANGE list1 0 -1 View all values ​​of list1
RPOP list1 Delete the last value on the right
LPOP list1 Delete the first value from the left
LTRIM list1 0 2 Only keep the first 3 digits, delete other values
127.0.0.1:6379> RPUSH list1 1 2 3 4
(integer) 4
127.0.0.1:6379> RPUSH list1 5 6 7 8
(integer) 8
127.0.0.1:6379> LPUSH list1 0
(integer) 9
127.0.0.1:6379> LRANGE list1 0 -1
1) "0"
2) "1"
3) "2"
4) "3"
5) "4"
6) "5"
7) "6"
8) "7"
9) "8"
127.0.0.1:6379> RPOP list1
"8"
127.0.0.1:6379> LPOP list1
"0"
127.0.0.1:6379> LTRIM list1 0 2
OK
127.0.0.1:6379> LRANGE list1 0 -1
1) "1"
2) "2"
3) "3"

4. Hash (hash)

command Explanation
HMSET user:1000 username zhangsan age 17 job it Create hash key user: 1000
HGET user:1000 username View the username parameter in the key value
HGET user:1000 age View the age parameter in the key value
HGET user:1000 job View job parameters in the key value
HMSET user:1000 tel 123456789 Add value tel
127.0.0.1:6379> HMSET user:1000 username zhangsan age 17 job it 
OK
127.0.0.1:6379> HGET user:1000 username
"zhangsan"
127.0.0.1:6379> HGET user:1000 age
"17"
127.0.0.1:6379> HGET user:1000 job
"it"
127.0.0.1:6379> hmset user:1000 tel 123456789
OK
127.0.0.1:6379> HGET user:1000 tel
"123456789"

5. Set

command Explanation
SADD set1 1 2 3 Create collection set1
SMEMBERS set1 View collection set1
SADD set1 1 4 Add the value 1 4 to the set set1, but the set feature is to remove duplication, so 1 cannot be added
SREM set1 1 4 Delete the value of the collection 1 4
sadd set2 1 4 5 Create the second set set2
SDIFF set1 set2 Difference set
SINTER set1 set2 Find the collection (intersection)
SUNION set1 set2 Find the union
127.0.0.1:6379> SMEMBERS set1
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> SADD set 1 4
(integer) 2
127.0.0.1:6379> SREM set1 1 4
(integer) 1
127.0.0.1:6379> SADD set2 1 4 5
(integer) 2
127.0.0.1:6379> sdiff set1 set2			
1) "2"
2) "3"
127.0.0.1:6379> sdiff set2 set1			
1) "1"
2) "4"
3) "5"
127.0.0.1:6379> sinter set1 set2			
(empty list or set)
127.0.0.1:6379> sunion set1 set2			
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"

Redis persistence

  • RDB: Generate a point-in-time snapshot and save it on the hard disk
    • Advantages: fast speed, suitable for backup, master-slave replication, single-open sub-process for RDB operation does not affect the main business
    • Disadvantages: some data will be lost
  • AOF: Record all write operation commands and restore data by executing these commands again
    • Advantages: to ensure maximum data is not lost
    • Disadvantages: too much logging

1. RDB configuration

[root@redis-1 ~]# redis-cli 
127.0.0.1:6379> bgsave       //RDB保存
Background saving started
127.0.0.1:6379> exit
[root@redis-1 ~]# vim /opt/redis_cluster/redis_6379/conf/6379.conf 
最后添加:
save 900 1
#在900秒(15分钟)之后,如果至少有1个key发生变化,则dump内存快照。
save 330 10	
#在300秒(5分钟)之后,如果至少有10个key发生变化,则dump内存快照。
save 60 10000
#在60秒(1分钟)之后,如果至少有10000个key发生变化,则dump内存快照

2.AOF configuration

[root@redis-1 ~]# vim /opt/redis_cluster/redis_6379/conf/6379.conf 
最后添加:
appendonly yes
#启用AOF持久化
appendfilename "redis.aof"
#指定AOF文件名
appendfsync everysec
#每秒同步一次

3. Restart the redis service

[root@redis-1 ~]# redis-cli shutdown  //关闭redis
[root@redis-1 ~]# redis-server /opt/redis_cluster/redis_6379/conf/6379.conf  //开启redis

Simulation actual combat: redis master-slave replication

The characteristics of redis master-slave replication:

​ 为解决单点故障把数据复制到一个或多个副本服务器(从服务器),实现故障恢复和负载均衡。

1.安装两台redis服务器

(1)第一台已安装好,把第一台的redis安装目录数据scp到第二台上。

[root@redis-1 ~]#  scp -rp /opt/redis_cluster/ [email protected]:/opt

(2)在第二台服务器上,编译安装redis

[root@redis-2 ~]# cd /opt/redis_cluster/redis
[root@redis-2 redis]# make && make install
cd src && make all
make[1]: 进入目录“/opt/redis_cluster/redis/src”

Hint: It's a good idea to run 'make test' ;)

make[1]: 离开目录“/opt/redis_cluster/redis/src”
cd src && make install
make[1]: 进入目录“/opt/redis_cluster/redis/src”

Hint: It's a good idea to run 'make test' ;)

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
make[1]: 离开目录“/opt/redis_cluster/redis/src”

(3)更改配置文件,启动服务

[root@redis-2 redis]# vim /opt/redis_cluster/redis_6379/conf/6379.conf 
bind 127.0.0.1 192.168.1.19
#添写本机IP(从服务器)
slaveof 192.168.1.20 6379
#添加主服务器IP和端口
......
[root@redis-2 redis]# redis-server /opt/redis_cluster/redis_6379/conf/6379.conf 
[root@redis-2 redis]# netstat -anpt | grep redis
tcp        0      0 192.168.1.19:6379       0.0.0.0:*               LISTEN      76323/redis-server  
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      76323/redis-server  
tcp        0      0 192.168.1.19:59726      192.168.1.20:6379       ESTABLISHED 76323/redis-server  

2.测试主从复制

(1)主服务器上新建键值,测试从服务器自动同步

主服务器:
[root@redis-1 ~]# redis-cli 
127.0.0.1:6379> SET k8 8
OK
127.0.0.1:6379> GET k8
"8"

从服务器:
[root@redis-2 ~]# redis-cli 
127.0.0.1:6379> GET k8
"8"

(2)从服务器在同步过程中,只能复制主数据库的数据,不能手动添加修改数据;如果从服务器非要修改数据,需要断开同步。

[root@redis-2 ~]# redis-cli   //登录
127.0.0.1:6379> GET k8   //查看同步的键值
"8"
127.0.0.1:6379> set k7 1    //创建 失败
(error) READONLY You can't write against a read only replica.
127.0.0.1:6379> exit   //退出
[root@redis-2 ~]# redis-cli slaveof no one      //断开同步
OK
[root@redis-2 ~]# redis-cli    //登录redis
127.0.0.1:6379> SET k7 1   //创建键值 k7
OK
127.0.0.1:6379> GET k7        //查看键值k7
"1"
127.0.0.1:6379> exit			//退出
[root@redis-2 ~]# redis-cli  slaveof 192.168.1.20 6379    //重新连接主服务器
OK
[root@redis-2 ~]# redis-cli  //登录redis
127.0.0.1:6379> GET k7  //查看键值 k7 查看不到
(nil)

Guess you like

Origin blog.csdn.net/weixin_45191791/article/details/107613667