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

Typical database in Nosql

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

Share

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

Typical database in Nosql

Redis

Write to the website server

Traditional relational databases cannot be written too much.

Requirements for the database:

High concurrent read and write requirements of database

Solution:

(1: read-write separation

If two masters write at the same time, there will be a conflict.

(2) split horizon:

Relational database

There are operations between data.

The need for efficient storage and access of massive data

If a user queries a piece of data in a large amount of data

Record

High scalability and high availability of database

# # #

In any field, if not through one's own efforts

To gain or exceed the competitive advantage of others.

It's really difficult to win!

# # #

Relational database:

Relationship: can be understood as a two-dimensional table, each relationship has a relationship name, which is commonly referred to as the table name.

Tuple: can be understood as a row in a two-dimensional table, often referred to as a record in a database.

Attribute: can be understood as a column in a two-dimensional table and is often referred to as a field in a database.

Field: the value range of an attribute, that is, the value limit of a column in the database.

Keyword: a set of attributes that uniquely identify a tuple. A database is often called a primary key and consists of one or more columns.

Relationship schema: a description of a relationship in the format of: relationship name (attribute 1, attribute 2, … , attribute N). It is often called a table structure in a database.

Nosql version

Storage methods are different.

Redis

Exist in cache

Very stable, older qualifications, more mature programs.

MongoDB

Exist in the file

The community is active and renewal is active.

HBASE

Memcached: data is saved only in the cache

Cassandra

SequoiaDB: bundled with ibm only

Redis

Store data through key values

Key values correspond to data such as variables

Nosql is used in conjunction with relational database

Memcache

High speed, unable to back up, and supports more than 100,000 QPS

(processing for a server within a time frame

What is the amount of data)

MONGODB

Document-based database with powerful query function and support

Index, closest to the nosql of a relational database

The structure is relatively simple

REDIS

Cache-based, fast, and supports multiple data structures

Consumes a lot of memory and supports tens of thousands of QPS

Support local storage with other softwar

Redis installation configuration:

1: install gcc*

/ usr/src/

Redis-3.0.3.tar.gz

Tcl-8.5.7-6.e16.x86

Tar-zxvf redis-3.03

Cd redis-3.03

Make

Rpm-ivh Tcl-8.5.7-6.e16.x86

Make test

Make PREFIX=/usr/local/redis install

Redis-check-aof

Redis-check-dump

The above are two kinds of logs

Cd! $

Vim redis.config

/ daemonize

Daemonize yes running in the background

If I want to see what keys are in the database,

Keys *

Set age 19

Keys *

Set Settin

Get View

Test the specified concurrency type

Official website: http://www.redis.cn/

Redis.io

String string

A String string can have only one value in a key

Set setting key

Get View key

. / redis-cli

Prevent overwriting:

Setnx name zhangsan

Show as 0 unsuccessful

Setnx name1 zhangsan

If it is displayed as 1, it is created successfully.

Setex aa 30111 sets temporary keys

Ttl aa View Lifecycle

-1 is permanent.

-2 the target key does not exist

Hexists c b3

Set multiple keys in batch:

MSET a3 333 a4 444 a5 555

Return ok for success and 0 for failure

Mget batch View key

Create new batch settings for msetnx to prevent overwriting

MSETNX a5 555 a6 aaa

Replace operation

Set b1'hi zhangsan'

Get b1

Replace the key value of the specified key starting from 3

SETRANGE b1 3 lisi

(replaced content)

Get b1 view the original value and replace the new value

Replace the whole content with a different value and view the original value

Getset b1 'hello zhangsan' view new values and replace new values

Get b1

Getrange b1 0 5 View some key values

Find the first to fifth characters

Increase or decrease of key value

Set c1 1

Set c2 2

Get c1

INCR c1

Get c1

The above will increase if you don't want him to add 1 yici plus 10.

INCRBY C1 + 10 (must be an integer) increases by 10 in turn

DECRBY C1 10 is reduced by 10 per execution.

APPEND b1 lisi appends four characters of lisi

Strlen b1 View key length

Del b1 deletion b1 can only be deleted one at a time

Hash string

There can be multiple values in a key

Hset D1 name1 zhangsan creates a hash field

Key name field key value

HGET D1 name1 View the value of the name1 field in D1 key name

Two sets of data are stored in one key

Hsetnx setting New prevents it from overwriting

HSETNX D1 name3 xxx 1 is successful

Values of the Hmget D1 name1 name2 name3 batch view field

Hkeys to view the field names in the specified key:

HKEYS D1 only view names

Hgetall to view the value of the specified key

HDEL D1 name3 deletes the specified field

Hset d2 a 1

Field name a numeric 1

Hkeys d2

Hincrby D2 a 5 increases the value of the specified field

List bidirectional queue

Bidirectional queue is a special type of string.

It's just a sort, with a front and back order.

The service has a queue when establishing a connection

How to manage these messages in a row

There is a sequence of quality of service

Each data in the queue can be treated as a string data type

There are two kinds of order, one is from left to right 0 1 2 3

One is from right to left-1-2-3

0 1 2 3 data

Lpush list1 1 inserts from left to right

Lpush list1 2

Lrange list1 0-1 view from first on the left to first on the right

Rpush list2 one two three four inserts from right to left

Lrange list2 0-1 View

Linsert list2 before two 3 hi

After (after)

Insert into queue lisi2 insert hi before two

Lset list2 1 change hi to five in the second position from left to right in list2

Rpush list3 hi 1 hi 2 hi 3 hi 4

Lrange list3 0-1 view a total of eight elements

Lrem list3-2 hi removes duplicate elements from the specified queue

Delete the penultimate one and the second one

Lrange list1 0-1 View

Ltrim list1 1 2 delete the first and second in the list1 table

Lrange list1 0-1 View deleted

Lpush list8 11 12 13 14 15 16 New queue

Ltrim list8 0 3 keep the first three

Lrange list8 0-1 view all

Lpop list8 deletes the first element on the left of the specified queue

Rpop list8 deletes the first element on the right of the specified queue

If I want to delete this at the same time

Elements are placed in other queues

Lrange list8 0-1

Lrance list2 0-1

Rpoplpush list8 list2 sets the last element in the list8 queue

Move to list2 queue

Llen View queue length

Lien list2 1 uses subscripts to view the values of elements in the queue

Lindex list2 0

Zset ordered set

Sorted set (zset) is the upgraded version of set

It adds a sequential attribute to set.

This attribute can be specified when adding a modified element

After each assignment, zset automatically re-adjusts the order to the new values.

Can be understood as a mysql table with two columns

One column stores value, one column stores the order. In the operation, key is understood as the name of zset.

Set

Such as the good recommendation in qq.

Value score subscript

Sadd Q1 1 add Collection

Smembers Q1 view all elements of the collection

Sadd q1 234

Srandmember Q1 random call value

Spop Q1 randomly deletes a value

Srem Q1 4 specifies that a value is randomly deleted

Smembers Q1 view all elements of the collection

Scard Q1 view a total of several elements

Sismember Q1 3 to see if 3 is an element in the collection

Sadd q2 1 2 3 4

Sadd q3 3 4 5 6

Sdiff Q2 Q3 View Lookup is based on the first specified collection

Sdiffstore Q4 Q3 Q2 view and write the Q3 Q2 collection to Q4

Smembers q4

Smembers Q2 view all elements in the collection

Smembers q3

Sinter Q2 Q3 View intersection

Sinterstore Q8 Q2 Q3 needs the same to save to a new collection

View the intersection and save to a new collection

Sunion Q2 Q3 View Union

Sunionstore q9 q2 q3

Smove Q2 Q3 1 first write source collection move the value of Q2 to Q3 in the write target set

Move elements in a collection

Ordered set: one more score than the original set

Can achieve real-time synchronization

Zadd z1 1 aa 3 bb 2 cc 2 dd

Zrange Z1 0-1 scores are sorted from smallest to largest.

View elements in the collection

Zrange z1 0-1 withscores scores are sorted from smallest to largest.

Zrevrange z1 0-1 withscores

Will be arranged according to the score from the largest to the smallest.

Zrangebyscore z1 2 3 withscores

View scores from 2 to 3

Zrem z1 dd

Delete valu

Zrevrange z1 0-1 withscores

Zincrby z1 5 aa

Zrevrange z1 0-1 withscores

Zincrby Z1 4 dd created and set the score to 4

Add a score if it does not exist.

Zrange z1 0-1 withscores

Zrank z1 aa

Get the subscript of the specified value in the order of the score from small to large

Zrevrank Z1 aa gets the subscript of the specified value in order of score from smallest to largest

Zcard Z1 counts the number of elements in the collection

Zcount Z1 4 10 counts the number of elements within a specified range of scores

Zrange z1 0-1 withscores

Zrank z1 cc

Zremrangebyrank Z1 1 2 removes bb and dd

Delete elements with subscript within the specified range

Zrange z1 0-1 withscores

Zremrangebyscore z1 1 3

Delete between 1 and 3 based on the score

Delete elements with scores within the specified range

Zadd z2 1 one 2 two 3 three

Zadd z3 2 two 3 three 3 four

Zinterstore z4 2 z2 z3 do intersection

Using Z4 as a set, there are two sets, Z2 and Z3, respectively.

Zrange Z40-1 to check their intersection is right, both sides.

Zrange z40-1 withscores takes the intersection and saves it to the new collection

After the ordered set is taken out, the scores will be superimposed.

There is no point in inserting a set into an ordered set because there is no such command.

Union:

Zrange z2 0-1 withscores create one1 two2 three3

Zrange z3 0-1 withscores create two2 three3 four4

Zunionstore z5 2z2 z3 merge and save to a new set (fractional superposition)

Zrange z50-1 withscores view new collection

Global administrative commands

Keys *

Number of Dbsize statistics keys

Exists a2 check whether a key exists 0 does not exist 1 exists

-1 permanent-2 temporary

Ttl a2

Expire Q4 20 can be set as the effective time key

Set the effective time of Q4 to 20 seconds

Ttl Q4 will change

Rename D1 D2 rename D1 to D2

Type A1 View key data type

(e.g. string list set zset)

Info view version information

Keys *

Select 1 chooses which database to go to and defaults to 0 database

There are 0-15 16 databases.

Move D2 1 moves a key to another database

D2 cannot be seen in the current database!

You can see it in the Select 1 to 1 database!

Flushdb empties the current database

Flushall empties all databases (16 database variables)

Database application

Password authentication:

Yum-y install gcc*

Make

Make test

Cd / usr/local/redis

Vim redis.conf opens the main configuration file

Daemonize yes is set to start in the background

Start redis

Small P login

Vim redis.conf opens the main configuration file

/ requirepass search

Requirepass 123.com removes comments and writes password

Wq!

Pkill redis restart redis

. / bin/redis-server. / redis.conf

. / bin/redis-cli login

Keys * will not see anything.

Auth 123.com needs to provide authentication.

Or add the option to specify the password when logging in-a 123.com

Backup:

Nosql is a nosql that can save data.

How to preserve it!

Cd / usr/local/redis

/ usr/local/redis/bin/redis-server space

/ usr/local/redis/redis.conf activate the service

The above is one line.

/ usr/local/redis/bin/redis-cli-a 123.com login

Keys * and you can see it.

Save save

Exit

Pkill redis

Cd / root/

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

/ usr/local/redis/bin/redis-cli-a 123.com

Keys * is empty!

Vim / usr/local/redis/redis.conf

/ dir

/ usr/local/redis (modified to dir. /)

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

open

/ usr/local/redis/bin/redis-cli-a 123.com login

Keys * will see that you don't have to look for it in the configuration file.

Master-slave synchronization:

Experimental requirements: master and slave on a server

You need to create two working directories on one server

Mysql mysqld

1 from: slave server configuration (on one server)

Cd / usr/local/redis/ enters the working directory

Ls will see the main configuration file redis.conf

Cd / usr/local/

Cp-r redis/ redis-slave/

Ls will have two working directories

Vim redis-slave/redis.conf

Change Pidfile / usr/local/redis-slave/redis.pid to this file

Change the port of Port 6380

Dir / usr/local/redis-slave (modified to dir. /)

/ slaveof lookup /

Slaveof 127.0.0.1 6379 Master Server ip, Port

Masterauth 123.com

Wq!

/ usr/local/redis-slave/bin/redis-server space

/ usr/local/redis-slave/redis.conf

One line of the above two behaviors

Netstat-anpt view port has 6380 6379

Verify login to the master server (do it on a machine)

/ usr/local/redis/bin/redis-cli-a 123.com

Keys * has variables

Set bb 111Creates a bb

Keys *

Exit

Log in from the server by

Keys *

If you see bb, you can create it successfully.

If it is built on two servers, you only need to

Just point to the master from the server's ip address in the server master configuration file

Introduction to five data structures and usage scenarios of Redis

String-- string

Hash-- dictionary

List-- list

Set-- collection

Sorted Set-- ordered set

1. String-- string

The String data structure is a simple key-value type, and value can be either a String or a number (encoding is an integer when a numeric type can be represented by Long, and the rest is stored in sdshdr as a string). Using the Strings type, you can fully implement the functions of the current Memcached and be more efficient. You can also enjoy the timing persistence of Redis (you can choose RDB mode or AOF mode), operation log, Replication and other functions. In addition to providing the same get, set, incr, decr, and so on operations as Memcached, Redis also provides the following operations:

1.LEN niushuai:O (1) gets the string length 2.APPEND niushuai redis: to the string append content, and uses intelligent memory allocation (2 times each time) 3. 5. Set and get a segment of the string 4. Set and get a bit of the string (bit) 5. 0. Batch set the contents of a series of strings 6. An ingenious use of the atomic counter 7.GETSET command, please set a new value while clearing the old value, and use it with the atomic counter

1. Hashmuri-Dictionary

In Memcached, we often package some structured information into hashmap, which is serialized on the client side and stored as a string value (usually in JSON format), such as the user's nickname, age, gender, points, and so on. At this time, when you need to modify one of these items, you usually need to take out the string (JSON), then deserialize it, modify the value of an item, and then serialize it into a string (JSON) and store it back. Simply modify an attribute to do so many things, it must be very expensive, and it is not suitable for some situations where concurrent operations are possible (for example, two concurrent operations need to modify the integral). The Hash structure of Redis allows you to modify only one attribute value as if you were Update an attribute in the database.

Store, read, and modify user attributes

1. List of listings

To put it bluntly, List is a linked list (redis uses double-ended linked lists to implement List). It is believed that anyone who has learned the knowledge of data structure should be able to understand its structure. Using the List structure, we can easily achieve functions such as ranking the latest news (such as TimeLine on Sina Weibo). Another application of List is message queue, which can use the * PUSH operation of List to store the task in List, and then the worker thread uses the POP operation to fetch the task for execution. Redis also provides API for manipulating certain elements in List. You can directly query and delete elements in List.

1. Weibo TimeLine2. Message queue

1. SetMurt-set

Set is a collection, and the concept of a set is a combination of unrepeated values. Using the Set data structure provided by Redis, some collective data can be stored. For example, in the Weibo application, you can put all the followers of a user in a collection and all its fans in a collection. Because Redis is very humanized for the collection to provide intersection, union, difference and other operations, then it can be very convenient to achieve, such as common concern, common preferences, second friends and other functions, for all the above set operations, you can also use different commands to choose to return the results to the client or save the set to a new collection.

1. Common friend, second friend 2. Using uniqueness, you can count all the independent IP3 that visit the website. When recommending a friend, you can recommend it if it is greater than a certain threshold according to the intersection of tag.

1.Sorted Set-- ordered set

Compared with Sets, Sorted Sets adds a weight parameter score to the elements in Set, so that the elements in the collection can be arranged in order by score, such as a Sorted Sets that stores the scores of the whole class, whose collection value can be the student number of students, and score can be its examination score, so that when the data is inserted into the collection, it has been sorted naturally. In addition, Sorted Sets can be used to make weighted queues, such as the score of ordinary messages is 1 and the score of important messages is 2, and then the worker thread can choose to get work tasks in reverse order of score. Give priority to important tasks.

1. An element with weight, such as a game's ranking of user scores

two。 For more complex data structures, not too many scenarios are generally used.

II. Scenarios for the use of other functions of redis

1. Subscribe-publish system

Pub/Sub literally means Publish and Subscribe. In Redis, you can set a key value to publish and subscribe to a message. When a message is published on a key value, all clients subscribing to it will receive the corresponding message. The most obvious use of this function is as a real-time messaging system, such as ordinary real-time chat, group chat and other functions.

2: transaction-Transactions

Who says that NoSQL does not support transactions? although Redis's Transactions does not provide strict ACID transactions (such as a series of commands committed for execution with EXEC, and the server goes down during execution, then some of the commands will be executed and the rest will not be executed), this Transactions still provides the function of basic command package execution (if there is no problem with the server, it can ensure that a series of commands are executed sequentially. There will be other client commands plugged in and executed). Redis also provides a Watch function, you can Watch a key, and then execute Transactions, in the process, if the value of the Watched has been modified, then the Transactions will find and refuse to execute.

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