@ the Author: Runsen

Redis

Redis is an open source, network-enabled, memory-based and persistent logging, key-value database written in ANSI C, and provides multiple language apis.

As of March 15, 2010, the development of Redis is hosted by VMware. Development of Redis has been sponsored by Pivotal since May 2013.

Redis features

  • Redis supports data persistence, saving data in memory to disk, which can be reloaded for use upon restart.
  • Redis not only supports simple key-value data, but also stores values into list, set, zset, hash and other data structures.
  • Because Redis exchange data quickly, it is often used to store some frequently retrieved data in the server to improve efficiency.

Redis installation

Installing Redis on Linux is very simple, and the main commands are as follows:

The Redis database requires GCC compilation, so the first step is to check whether the GCC environment is installed

[root@VM_0_16_centos ~]# RPM - qa | grep GCC * / / no installation.[root@VM_0_16_centos ~]# yum install gcc-c++
Copy the code

Create a directory, download the source code (using Huawei image), and decompress the source code

[root@VM_0_16_centos redis]# mkdir /usr/lib/redis
[root@VM_0_16_centos redis]# cd /usr/lib/redis/
[root@VM_0_16_centos redis]# wget https://mirrors.huaweicloud.com/redis/redis-5.0.5.tar.gz
[root@VM_0_16_centos redis]# tar -zxvf redis-5.0.5.tar.gz 
Copy the code

Go to the folder and compile

[root@VM_0_16_centos redis]# cd ./redis-5.0.5/
[root @ VM_0_16_centos redis - 5.0.5]# make
Copy the code

If the above 4 commands are installed properly, it means that make is compiled successfully.

Modifying a Configuration File

1, the source directory redis redis configuration file. The conf copied to/usr/local/software/redis/directory. Cp redis. Conf/usr/local/software/redis / 2, modify the configuration items, according to the need; If you do not change the configuration, use the default configuration

Install, and verify that services are installed

[root @ VM_0_16_centos redis - 5.0.5]# make PREFIX=/usr/local/redis install //[root@VM_0_16_centos bin]# ls /usr/local/redis/bin redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server // Copy the configuration file from the decompressed directory to the installation directory[root@VM_0_16_centos usr]# cp /usr/lib/redis/redis-5.0.5/redis.conf/usr/local/redis/ / Because the front-end startup mode cannot be stopped (processes are disconnected), configure the back-end startup modeCopy the code

Next, configure Redis Internet access, modify backend startup (that is, daemons enabled), and cancel IP binding

[root@VM_0_16_centos ~]# vim /usr/local/redis/redis.confComment out the bind127.0.0.1Or to bind0.0.0.0
#bind 127.0.0.1Change protected-mode yes to # disable protected-mode no Change daemonize no to daemonize yesCopy the code

Set password requirePass to a long password

Start and specify the configuration file


[root@VM_0_16_centos ~]# cd /usr/local/redis/

[root@VM_0_16_centos redis]# ./bin/redis-server ./redis.conf

1675:C 15 Sep 2019 22:50:52.157 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo

1675:C 15 Sep 2019 22:50:52.157 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=1675, just started
1675:C 15 Sep 2019 22:50:52.157 # Configuration loaded
Copy the code

Check whether the service is started through port (6379)

[root@VM_0_16_centos redis]# ps -ef|grep redis root 1676 1 0 22:50 ? 00:00:00./bin/redis-server *:6379 root 1900 1219 0 22:52 PTS /6 00:00:00 grep color=auto redisCopy the code

The local client connection and redis service are closed

[root@VM_0_16_centos redis]#./bin/redis-cli 127.0.0.1:6379> eixt [root@VM_0_16_centos redis]#./bin/redis-cli shutdownCopy the code

External (IP) connection,(need to open the corresponding port of the cloud server)

[root@VM_0_16_centos redis]#./bin/redis-cli -h 49.ip.ip.2 -p 6379 -a Password Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.49.ip.ip.2:6379 >Copy the code

Refer to the above steps: tencent cloud server install redis, cloud.tencent.com/developer/a…

Redis data model

Redis supports five data types: string, hash, list, set and zset(sorted set).

  1. String ——> String
  2. Hash — — — — — – > Hash
  3. The List — — — — — – > List
  4. The set — — — — — – > set
  5. Zset ——> Ordered sets

  • Connect to Redis: redis-cli

  • Exit, exit

  • Run the service redis start/stop/restart command on the server

  • To switch a database: select n

Redis uses five data types

Global Key operation

Command for all five data types

Delete key pairs: del key Rename: rename key new_key Set expiration time: expire key secondsCopy the code

Type String

Strings is the most basic data type of Redis. One key corresponds to one value

Set data: Set key Value View data: get key Add data: Append key value Delete data: del key;Copy the code

List the type

Rpush key value [value...] Lpush key value/value... Header Add data View data: lrange key start stop Lindex Key Index View a certain data Modify data: lset key index Value Delete data: rPOP Key Delete data in the LPOP key headerCopy the code

Hash type

Add data: hset key field Value Query field value: hget key field hgetall Key Query all fields and Values Query all values: hVALS key Query all fields: hkeys keyCopy the code

The Set type

Sadd key member [member...] Smembers key Randomly deleted: spop key Specified deleted: srem key member [member...]Copy the code

Sorted the Set type

Zadd key score member [score2 member2... Zrem key member [member...] zrange key start stop zrangebyScore key min Max Delete multiple data by index: zremrangeByrank key min Max ZremrangebyScore key min Max -- Delete by scores valueCopy the code

Flushall Deletes all data

Redis visualization

I use Redis Desktop Manager. This is probably the most widely used visualization tool out there today. Rdm.dev /pricing.

Python connection redis

The library that Python connects to the Redis database is redis, without Pyredis.

PIP install redis

Before connecting Python to Redis, ensure that the redis Internet access is successfully configured.

Conn = redis.Redis(host="192.168.92.90", Port =6379,password="123456") conn.set("x","hello") val = conn.get("x") print(val) # hello import redis # connection pool = Redis. ConnectionPool (host = "192.168.23.166", port=6379,password="123456",max_connections=1024) conn = redis.Redis(connection_pool=pool) print(conn.get("x1"))Copy the code

GitHub, portal ~, GitHub, portal ~, GitHub, Portal ~