Operation Basic Operation redis redis

redis basic operations

Redis System Management

About experiments

On an experiment about the basic data types Redis of this experiment continues to explain Redis commands and related management operations.

In Redis, the command is not case sensitive.

First, for all types of commonly used commands

Services start redis redis-cli command interface and continue to follow-up experiment:

$ sudo service redis-server start
$ redis-cli

(1)EXISTS and DEL

EXISTS key to determine whether there is a key; there is return 1; otherwise it returns 0; DEL key to delete a key, or a series of key; DEL key1 key2 key3 key4. Successful return 1, else return 0 (key value does not exist).

> set mykey hello

> exists mykey

> del mykey > exists mykey 

Operating Screenshot:

 

(2)TYPE and KEYS

TYPE key: return data type of a key element of (none: does not exist, string: character, list, set, zset, hash), key does not return empty existence. KEYS key-pattern: returns a list of matching key (KEYS foo *: foo find the beginning of keys)

> set mykey x

> type mykey

>keys my*

> del mykey >keys my* > type mykey 

Operating Screenshot:

 

(3)RANDOMKEY and CLEAR

RANDOMKEY: random access to a key already exists, if the current database is empty, an empty string is returned

> randomkey

Operating Screenshot:

 

CLEAR: Clear interface.

> clear

(4)RENAME and RENAMENX

RENAME oldname newname: change the key name, if there is a new key will be overwritten RENAMENX oldname newname: Change the name of the key, if there is the name changes failed

I randomkey results mylist, this key value is renamed newlist.

> randomkey

> rename mylist newlist

> exists mylist > exists newlist 

Operating Screenshot:

 

(5) DBSIZE

DBSIZE: Returns the total number of the current key database

> dbsize

Operating Screenshot:

 

Two, Redis time related commands

(1) defines a key survival time

This is also a command to ignore data type, useful for temporary storage. DEL avoid a large number of operations.

EXPIRE: set the expiration time of a key (s), (EXPIRE bruce 1000: bruce set the system to automatically delete after key1000 seconds) Note: If the time has not yet expired, the values ​​are changed, then that value will be cleared .

> set key some-value

> expire key 10 > get key (马上执行此命令) > get key (10s后执行此命令) 

Operating Screenshot:

 

The results showed that, after executing the command EXPIRE immediately GET, display key presence. 10 seconds and then GET, key has been automatically deleted.

(2) query key remaining lifetime

Limit operation can then SET command to achieve, and the TTL command to query the key remaining lifetime. TTL: Find a key as well as how long expired, return time sec

> set key 100 ex 30

> ttl key

> ttl key 

Operating Screenshot:

 

(3) Clear key

FLUSHDB: Clear all keys in the current database

FLUSHALL: Clear all the keys in all databases

>flushdb

>flushall

Three, Redis set the relevant command

Redis has its configuration file, you can view or change the configuration through client-command window. Related command as follows:

(1)CONFIG GET and CONFIG SET

CONFIG GET: read configuration parameters for running Redis server. CONFIG SET: to change the running Redis server configuration parameters. AUTH: Redis password authentication password for the following example:

> config get requirepass (查看密码)

> config set requirepass test123 (设置密码为test123 )

> config get requirepass (报错,没有认证) > auth test123 > config get requirepass 

Operating Screenshot:

 

From the results, Reids did not set a password at the beginning, password query result is empty. Then set a password for test123, error query again. After certification auth command, can be found normal.

Redis.conf can change the password modified Redis configuration file.

CONFIG GET command is a list of key-value pairs displayed, such as the maximum query data entry types:

> config get *max-*-entries*

Operating Screenshot:

 

(2) Reset Report

CONFIG RESETSTAT: reset the statistics reports, usually the return value 'OK ".

> CONFIG RESETSTAT

Operating Screenshot:

 

Fourth, the query information

INFO [section]: Query Redis information. INFO command, you can almost all of the information Redis, its command options are as follows:

  1. server: General information about the Redis server
  2. clients: Client connectivity options
  3. memory: storing information related to occupation
  4. persistence: RDB and AOF information
  5. stats: General Statistics
  6. replication: Master / slave request information
  7. cpu: CPU occupancy statistical information
  8. cluster: Redis cluster information
  9. keyspace: Statistical Information Database
  10. all: All information return
  11. default: return to the normal setting information

If the command parameter is empty, info command returns all of the information.

> info keyspace

> info server

Operating Screenshot:

 

 

 

Redis advanced applications

About experiments

Redis front learned the basics and basic commands, then continue to explain Redis advanced applications, including: security settings, master-slave replication, transactions, persistence mechanism, virtual memory usage.

First, security

Client connection is provided at specified password required (due redis fairly fast, a second password attempts may 150K times, it is necessary to set a password great strength password).

There are two ways to set a password:

(1) using the parameters requirepass config set command, the specific format of config set requirepass "password". (2) Configuration redis.conf requirepass property set, the password later.

Enter the certification there are two ways:

(1) When you log can redis-cli -a password

(2) After logging in with auth password

(1) Set password

The first way to set the password has been mentioned in one experiment, (in the example CONFIG SET command to explain), where we look at the second way to set a password.

Redis first need to enter the installation directory, and then modify the configuration file redis.conf. According to the results of the grep command, use the vi editor to modify the "# requirepass foobared" as "requirepass test123", then save and exit.

$ grep -n requirepass /etc/redis/redis.conf
$ sudo vim /etc/redis/redis.conf

Edit redis.conf result of:

 

(2) Restart redis-server and redis-cli

Restart redis server.

$ sudo service redis-server restart

Into redis-cli interface authenticate

$ redis-cli

> info

> auth test123

> info > exit 

Operating Screenshot:

 

The results showed that for the first time info command failed to return to normal after the auth info command authentication. Last Exit redis-cli.

Another password authentication:


$ redis-cli -a test123

> info

Operating Screenshot:

 

Second, the master copy from

For environmental reasons, here we explain roughly master-slave replication workflows, do not do experiments.

Redis master copy from the use of relatively simple configuration and may allow the plurality of slave server and the master server has the same copy of the database from the master by replication.

Can only be read from the server, you can not write.

Redis master-slave replication features:

1, master can have multiple slave.

2, the same can be connected to a plurality of slave master, but may also be connected to other slave. (Down when the master, slave connected into master)

3, does not block the replication master from the master, when the re-synchronization data, Master can continue to process client requests.

4, to improve the scalability of the system.

Redis master-slave replication process:

1, Slave establish a connection with the master, sends a sync synchronization command.

2, Master starts a background process, database snapshots are saved to a file, while Master main process will begin collecting new write command and cache.

3, behind the scenes to save, you send this file to Slave.

4, Slave save this file to disk.

Third, the transaction

Redis transaction is relatively simple. Client initiated transaction can only guarantee the command is executed continuously, and does not insert another client command, when a client sends a command to the multi connection, the connection enters the context of a transaction, the subsequent commands are not connected It will be performed, but is stored in a queue, when an exec command execution queue order will redis all commands. If execution error occurs, it does not roll back the implementation of the right, different from transactional relational database.

> multi

> set name a

> set name b > exec > get name 

Operating Screenshot:

 

Fourth, the persistence mechanism

Redis is a database to support persistence of memory, Redis often need to synchronize the data in memory to disk to ensure persistence.

Redis supports two persistent ways:

1, snapshotting (snapshots), the stored data to a file, the default mode.

Data is written to the memory of the way to have a snapshot of the binary file, the default file dump.rdb, you can do a snapshot of persistent manner by the automatic configuration settings. Redis be disposed within n seconds, if more than m modification key are automatically saved snapshots.

save within 9001 # 900 seconds if more than one key is modified, then saved a snapshot launched

save 300 10 # 300 seconds If more than 10 key is modified, the snapshot saved

2, Append-only file (abbreviated aof), the read and write operations to a file store.

Because snapshots do a certain interval of time, so if Redis accidentally fall down, they would lose all changes after the last snapshot.

aof better than snapshots lasting resistance, due to the use aof, redis will each receive a write command are written by the write function to write the file when the file saved by re-executing the start when redis command to re-establish the contents of the entire database in memory.

Since the os will modify write cache to do in the kernel, it may not be immediately written to disk, the persistence of such aof way also still part of the data may be lost. Redis can tell by the profile we want to force os written to disk through fsync timing function.

The profile of configurable parameters:

appendonly   yes     //启用aof持久化方式

#appendfsync  always //收到写命令就立即写入磁盘,最慢,但是保证了数据的完整持久化 appendfsync everysec //每秒中写入磁盘一次,在性能和持久化方面做了很好的折中 #appendfsync no //完全依赖os,性能最好,持久化没有保证 

In the redis-cli command, SAVE command is to write data to disk.

> help save

>save

Operating Screenshot:

 

Fifth, the virtual memory use

Virtual Memory Manager 2.6 and above versions canceled the experiment in installation, choose the 2.8.9 version of redis, all experiments profile configuration option is no virtual memory management functions. Only explain here

Redis virtual memory is temporary data that is infrequently accessed from memory is swapped to disk, freeing up memory space for other data access, especially for redis-memory database such, memory is always not enough. In addition to separate multiple redis server, but to improve the capacity of the database approach is to use virtual memory, the data exchange to those infrequently accessed disk.

By configuring vm-related redis.config configuration:

vm-enable  yes                   #开启vm功能

vm-swap-file    /tmp/redis.swap  #交换出来的value保存的文件路径 vm-max-memory 10000000 #redis使用的最大内存上线 vm-page-size 32 #每个页面的大小32字节 vm-pages 123217729 #最多使用多小个页面 vm-max-threads 4 #用于执行value对象换入的工作线程数量

转自:https://www.cnblogs.com/wangshouchang/p/6130865.html

Redis System Management

About experiments

On an experiment about the basic data types Redis of this experiment continues to explain Redis commands and related management operations.

In Redis, the command is not case sensitive.

First, for all types of commonly used commands

Services start redis redis-cli command interface and continue to follow-up experiment:

$ sudo service redis-server start
$ redis-cli

(1)EXISTS and DEL

EXISTS key to determine whether there is a key; there is return 1; otherwise it returns 0; DEL key to delete a key, or a series of key; DEL key1 key2 key3 key4. Successful return 1, else return 0 (key value does not exist).

> set mykey hello

> exists mykey

> del mykey > exists mykey 

Operating Screenshot:

 

(2)TYPE and KEYS

TYPE key: return data type of a key element of (none: does not exist, string: character, list, set, zset, hash), key does not return empty existence. KEYS key-pattern: returns a list of matching key (KEYS foo *: foo find the beginning of keys)

> set mykey x

> type mykey

>keys my*

> del mykey >keys my* > type mykey 

Operating Screenshot:

 

(3)RANDOMKEY and CLEAR

RANDOMKEY: random access to a key already exists, if the current database is empty, an empty string is returned

> randomkey

Operating Screenshot:

 

CLEAR: Clear interface.

> clear

(4)RENAME and RENAMENX

RENAME oldname newname: change the key name, if there is a new key will be overwritten RENAMENX oldname newname: Change the name of the key, if there is the name changes failed

I randomkey results mylist, this key value is renamed newlist.

> randomkey

> rename mylist newlist

> exists mylist > exists newlist 

Operating Screenshot:

 

(5) DBSIZE

DBSIZE: Returns the total number of the current key database

> dbsize

Operating Screenshot:

 

Two, Redis time related commands

(1) defines a key survival time

This is also a command to ignore data type, useful for temporary storage. DEL avoid a large number of operations.

EXPIRE: set the expiration time of a key (s), (EXPIRE bruce 1000: bruce set the system to automatically delete after key1000 seconds) Note: If the time has not yet expired, the values ​​are changed, then that value will be cleared .

> set key some-value

> expire key 10 > get key (马上执行此命令) > get key (10s后执行此命令) 

Operating Screenshot:

 

The results showed that, after executing the command EXPIRE immediately GET, display key presence. 10 seconds and then GET, key has been automatically deleted.

(2) query key remaining lifetime

Limit operation can then SET command to achieve, and the TTL command to query the key remaining lifetime. TTL: Find a key as well as how long expired, return time sec

> set key 100 ex 30

> ttl key

> ttl key 

Operating Screenshot:

 

(3) Clear key

FLUSHDB: Clear all keys in the current database

FLUSHALL: Clear all the keys in all databases

>flushdb

>flushall

Three, Redis set the relevant command

Redis has its configuration file, you can view or change the configuration through client-command window. Related command as follows:

(1)CONFIG GET and CONFIG SET

CONFIG GET: read configuration parameters for running Redis server. CONFIG SET: to change the running Redis server configuration parameters. AUTH: Redis password authentication password for the following example:

> config get requirepass (查看密码)

> config set requirepass test123 (设置密码为test123 )

> config get requirepass (报错,没有认证) > auth test123 > config get requirepass 

Operating Screenshot:

 

From the results, Reids did not set a password at the beginning, password query result is empty. Then set a password for test123, error query again. After certification auth command, can be found normal.

Redis.conf can change the password modified Redis configuration file.

CONFIG GET command is a list of key-value pairs displayed, such as the maximum query data entry types:

> config get *max-*-entries*

Operating Screenshot:

 

(2) Reset Report

CONFIG RESETSTAT: reset the statistics reports, usually the return value 'OK ".

> CONFIG RESETSTAT

Operating Screenshot:

 

Fourth, the query information

INFO [section]: Query Redis information. INFO command, you can almost all of the information Redis, its command options are as follows:

  1. server: General information about the Redis server
  2. clients: Client connectivity options
  3. memory: storing information related to occupation
  4. persistence: RDB and AOF information
  5. stats: General Statistics
  6. replication: Master / slave request information
  7. cpu: CPU occupancy statistical information
  8. cluster: Redis cluster information
  9. keyspace: Statistical Information Database
  10. all: All information return
  11. default: return to the normal setting information

If the command parameter is empty, info command returns all of the information.

> info keyspace

> info server

Operating Screenshot:

 

 

 

Redis advanced applications

About experiments

Redis front learned the basics and basic commands, then continue to explain Redis advanced applications, including: security settings, master-slave replication, transactions, persistence mechanism, virtual memory usage.

First, security

Client connection is provided at specified password required (due redis fairly fast, a second password attempts may 150K times, it is necessary to set a password great strength password).

There are two ways to set a password:

(1) using the parameters requirepass config set command, the specific format of config set requirepass "password". (2) Configuration redis.conf requirepass property set, the password later.

Enter the certification there are two ways:

(1) When you log can redis-cli -a password

(2) After logging in with auth password

(1) Set password

The first way to set the password has been mentioned in one experiment, (in the example CONFIG SET command to explain), where we look at the second way to set a password.

Redis first need to enter the installation directory, and then modify the configuration file redis.conf. According to the results of the grep command, use the vi editor to modify the "# requirepass foobared" as "requirepass test123", then save and exit.

$ grep -n requirepass /etc/redis/redis.conf
$ sudo vim /etc/redis/redis.conf

Edit redis.conf result of:

 

(2) Restart redis-server and redis-cli

Restart redis server.

$ sudo service redis-server restart

Into redis-cli interface authenticate

$ redis-cli

> info

> auth test123

> info > exit 

Operating Screenshot:

 

The results showed that for the first time info command failed to return to normal after the auth info command authentication. Last Exit redis-cli.

Another password authentication:


$ redis-cli -a test123

> info

Operating Screenshot:

 

Second, the master copy from

For environmental reasons, here we explain roughly master-slave replication workflows, do not do experiments.

Redis master copy from the use of relatively simple configuration and may allow the plurality of slave server and the master server has the same copy of the database from the master by replication.

Can only be read from the server, you can not write.

Redis master-slave replication features:

1, master can have multiple slave.

2, the same can be connected to a plurality of slave master, but may also be connected to other slave. (Down when the master, slave connected into master)

3, does not block the replication master from the master, when the re-synchronization data, Master can continue to process client requests.

4, to improve the scalability of the system.

Redis master-slave replication process:

1, Slave establish a connection with the master, sends a sync synchronization command.

2, Master starts a background process, database snapshots are saved to a file, while Master main process will begin collecting new write command and cache.

3, behind the scenes to save, you send this file to Slave.

4, Slave save this file to disk.

Third, the transaction

Redis transaction is relatively simple. Client initiated transaction can only guarantee the command is executed continuously, and does not insert another client command, when a client sends a command to the multi connection, the connection enters the context of a transaction, the subsequent commands are not connected It will be performed, but is stored in a queue, when an exec command execution queue order will redis all commands. If execution error occurs, it does not roll back the implementation of the right, different from transactional relational database.

> multi

> set name a

> set name b > exec > get name 

Operating Screenshot:

 

Fourth, the persistence mechanism

Redis is a database to support persistence of memory, Redis often need to synchronize the data in memory to disk to ensure persistence.

Redis supports two persistent ways:

1, snapshotting (snapshots), the stored data to a file, the default mode.

Data is written to the memory of the way to have a snapshot of the binary file, the default file dump.rdb, you can do a snapshot of persistent manner by the automatic configuration settings. Redis be disposed within n seconds, if more than m modification key are automatically saved snapshots.

save within 9001 # 900 seconds if more than one key is modified, then saved a snapshot launched

save 300 10 # 300 seconds If more than 10 key is modified, the snapshot saved

2, Append-only file (abbreviated aof), the read and write operations to a file store.

Because snapshots do a certain interval of time, so if Redis accidentally fall down, they would lose all changes after the last snapshot.

aof better than snapshots lasting resistance, due to the use aof, redis will each receive a write command are written by the write function to write the file when the file saved by re-executing the start when redis command to re-establish the contents of the entire database in memory.

Since the os will modify write cache to do in the kernel, it may not be immediately written to disk, the persistence of such aof way also still part of the data may be lost. Redis can tell by the profile we want to force os written to disk through fsync timing function.

The profile of configurable parameters:

appendonly   yes     //启用aof持久化方式

#appendfsync  always //收到写命令就立即写入磁盘,最慢,但是保证了数据的完整持久化 appendfsync everysec //每秒中写入磁盘一次,在性能和持久化方面做了很好的折中 #appendfsync no //完全依赖os,性能最好,持久化没有保证 

In the redis-cli command, SAVE command is to write data to disk.

> help save

>save

Operating Screenshot:

 

Fifth, the virtual memory use

Virtual Memory Manager 2.6 and above versions canceled the experiment in installation, choose the 2.8.9 version of redis, all experiments profile configuration option is no virtual memory management functions. Only explain here

Redis virtual memory is temporary data that is infrequently accessed from memory is swapped to disk, freeing up memory space for other data access, especially for redis-memory database such, memory is always not enough. In addition to separate multiple redis server, but to improve the capacity of the database approach is to use virtual memory, the data exchange to those infrequently accessed disk.

By configuring vm-related redis.config configuration:

vm-enable  yes                   #开启vm功能

vm-swap-file    /tmp/redis.swap  #交换出来的value保存的文件路径 vm-max-memory 10000000 #redis使用的最大内存上线 vm-page-size 32 #每个页面的大小32字节 vm-pages 123217729 #最多使用多小个页面 vm-max-threads 4 #用于执行value对象换入的工作线程数量

转自:https://www.cnblogs.com/wangshouchang/p/6130865.html

Guess you like

Origin www.cnblogs.com/gllegolas/p/12124820.html