Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the basic usage of ​ redis

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article is to share with you about the basic usage of redis. The editor thought it was very practical, so I shared it with you as a reference. Let's follow the editor and have a look.

The basic usage of redis will be introduced.

1. Basic part of Redis:

1. The introduction and installation of redis is 10 times faster than that of mysql

Redis applicable situation

1. The operation of taking the latest N pieces of data

two。 Ranking application, take TOP N operation

3. Applications that need to set expiration time precisely

4. Counter application

5.Uniq operation to obtain all data weight values for a certain period of time

6. Real-time system, anti-garbage system 7.Pub/Sub to build a real-time message system

7.Pub/Sub builds a real-time messaging system 8. Build a queue system

9. Caching

There are 110000 SET operations per second and 81000 GET operations per second. The server configuration is as follows:

Linux 2.6, Xeon X3320 2.5Ghz.

The stackoverflow website uses Redis as the caching server.

At the same time, the data will be written to the hard drive. So the data is secure (except for a sudden power outage, the restart service is written to the dump.rdb file)

1. Installation:

Tar zxvf redis-2.6.9.tar.gz

Cd redis-2.6.9

Make

Cd src & & make install

two。 Move the profile location (for ease of management)

Cd / usr/local/

Mkdir-p / usr/local/redis/bin

Mkdir-p / usr/local/redis/etc

Mv / lamp/redis-2.6.9/redis.conf / usr/local/redis/etc

Cd / lamp/redis-2.6.9/src

Mv mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-dump redis-cli redis-server / usr/local/redis/bin

3. Modify the configuration file

Vi / usr/local/redis/etc/redis.conf

Change no in daemonize no to yes [yes means running in the background]

4. Start / Random start:

Cd / usr/local/redis/bin

. / redis-server / usr/local/redis/etc/redis.conf# starts redis and specifies the configuration file.

# vi / etc/rc.local # sets random startup.

/ usr/local/redis/bin/redis-server / usr/local/redis/etc/redis.conf

5. Check to see if the startup is successful

Ps-ef | grep redis

Netstat-tunpl | grep 637ports to see if the port is occupied.

6. Enter client / exit

Cd / usr/local/redis/bin

. / redis-cli# entry

Quit# exit

7. Close redis

Pkill redis-server# shuts down

. / redis-cli shutdown# off

Redis security

The security of Redis? (in the following 4 ways)

1. Use ACL controller security.

two。 Add the following line to the redis.conf configuration file to bind the redis to a single interface (but not just accept the data from this network card).

Bind 127.0.0.1

3. Add a longer password to redis (no need to remember)

4. Enable authentication in the redis.conf configuration.

5.SSL Agent

6. Disables the specified command.

Redis configuration

If daemonize needs to run in the background, change this item to yes

Pidfile configures multiple pid addresses at / var/run/redis.pid by default

Bind binds ip, and only accepts requests from this ip after setting.

Port listening port. Default is 6379.

Timeout sets the timeout for client connection (in seconds)

Loglevel is divided into four levels, debug, verbose, notice, warning

Logfile configuration log file address

Databases sets the number of databases. The default database is 0.

Save sets the frequency of redis for database mirroring

Whether to compress rdbcompression when performing mirror backup

File name of the Dbfilename image backup file

File placement path for Dir database mirrored backup

Slaveof sets the database to be the slave database of other databases

Password authentication required for Masterauth primary database connection

Requirepass sets the password to be used when logging in

Maxclients limits the number of customers connected at the same time

Maxmemory sets the maximum memory that redis can use

Appendonly turns on append only mode

You can learn about the following:

Appendfsync sets the frequency of synchronization with appendonly.aof files

Whether to enable virtual memory support for vm-enabled

Vm-swap-file sets the swap file path for virtual memory

Vm-max-memory sets the maximum physical memory size used by redis

Vm-page-size sets the page size of virtual memory

Vm-pages sets the total number of page for swap files

Vm-max-threads sets the number of threads that VM IO uses simultaneously

Glueoutputbuf stores small output caches together

Hash-max-zipmap-entries sets the critical value of hash

Activerehashing re-hash

Five data types: string, hash, linked list, collection, ordered collection.

Support: push/pop, add/remove, take intersection, union, difference, sort.

Redismysql

At the same time, the data will be written to the hard drive. So the data is secure (except for a sudden power outage, the restart service is written to the dump.rdb file)

Select num# select library. Default is 0 library, with a total of 16 libraries.

The password required by the auth liweijie# authorized user (the password is the password configured in redis.conf)

Flushdb# empties the database.

String (string) type:

The value of set name lijie# setting key name is lijie

Get name# gets the value of name.

Keys * # queries all keys.

Setnx name liweijie# returns 0 if the key already exists, does not update, and prevents overwriting.

The value of the setex haircolor 10 red # setting key is valid for 10 seconds.

The value of the setrange email 6 lampbre.com# replacement key is changed to lampbre.com from the 6th character

Mset name1 Li Dawei name2 Li Xiaowei # sets the values of multiple keys.

Msetnxname1 Zhang San name3 Li Si # determines whether the key exists. If it does not exist, it will be set, otherwise it will not be set to return 0.

Mget name1 name2 name3# gets the values of multiple keys at a time.

Getset name1 Tom# resets the value of the key and returns the old key value.

Getrange email 6 'intercepts the value of the email key from the characters between bits 6 and 18.

Incr uid# increments by 1 at a time (if uid does not exist in key, set and start from 0, the same below)

Incrby uid increases by 5% each time.

Incrby uid-minus 5 at a time

Decr uid # minus 1 at a time

Decrby uid allowance minus 5% at a time

Appendname1 @ 126.com# to the value of name1, add the string @ 126.com

Strlenname1# returns the length of the value of the key name1.

Hashes (hash) type:

Hset user:001 name liweijie# hash sets the name key of user user:001 to liweijie

Hset user:001 age 2 similarly, add an age key to 21

Hsetnx user:001 age 2 is the same as above, but detects the presence of the key. If there is no creation.

Hmset user:002 name liweijie2 age 26 sex sets the values of multiple keys at the same time.

The hget user:001 name# hash gets the value of the name key of the user user:001.

Hget user:001 age # Ibid.

Hmget user:001 name age sex# gets the values of multiple specified keys.

Hgetall user:001# gets the values of all keys.

Hincrbyuser:001 age-assign adds the given value to the specified key.

Hexists user:001 sex# detects whether the specified key value exists.

Hlen user:001# returns the number of keys / fields for the specified hash.

Hdel user:001 sex# deletes the specified field or key value of the specified (user:001) hash.

Hkeys user:003# returns all fields or key values of the hash.

Lists (linked list) types and operations (lists or queues):

Lpush mylist "world" # inserts a string from the header

Lpush mylist "hello" # Ibid.

Lrange mylist 0-obtain from 0 to the last one such as [1) "hello" 2) "world"]

Rpush mylist "jiejie" # insert at the tail

Linsert mylist before "hello"this is linsert" # specifies the insertion location (insert before hello).

Lset mylist 0 "what" # setting modifies the value of the specified subscript.

Lrem mylist 1 "hello" # removes (1) an element with the value hello. (n)

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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report