Redis related notes

You’re unique, nothing can replace you
You are unique in the world, no one can replace

Record the installation of redis and use related notes to prevent forgetting. I personally think that redis compared with rabbitmq and mysql, it is relatively simple, of course, this is related to the use of redis, I also use the primary. In the future, I will record the problems related to redis in this article. This time, I will record the most basic installation process and use it.

redis installation

Installing redis on mac

1
brew install redis

installing redis on centos

1
2
3
4
$ wget http://download.redis.io/releases/redis-2.8.17.tar.gz
$ tar xzf redis-2.8.17.tar.gz
$ cd redis-2.8.17
$ make

Note: After the compilation is completed, the compiled redis service program redis-server and redis-cli will appear in the directory. Under the installation directory src, the system environment variables can be set.

ubuntu install redis

1
2
$sudo apt-get update
$sudo apt-get install redis-server

Starting the redis service

1
$ ./redis-server

or:

1
2
systemctl start redis
systemctl enable redis

The way to specify the configuration file starts:

1
$ ./redis-server redis.conf

Description: redis.conf is a default configuration file, we can use our own configuration file as needed.

View redis listening status

By default, redis listens on port 6379 and can be modified through the configuration file.

1
netstat -tlnp | grab "redis"

redis-cli

After the redis is installed by default, the server and client link tools will be installed, namely redis-cli.

Connecting to the redis server

Local connection will link to the local redis service by default.

1
$ redis-cli

The remote connection will connect to the redis service on the remote server. If there is no password, you do not need to write the -a parameter.

1
$ redis-cli -h host -p port -a password

Testing the redis server

Test if a password is set

1
2
3
CONFIG get requirepass
1) "requirepass"
2) ""

Note: The above output indicates that no password is set. Redis does not set a password by default.
set password:

1
CONFIG set requirepass "nmask"

verify password:

1
AUTH "nmask"

Test if the server is running:

1
2
redis 127.0.0.1:6379> PING
PONG

Note: Returning to PONG indicates that the operation is normal.

Get server information

1
redis 127.0.0.1:6379>INFO

Setting the key value (key)

Create a key:

1
2
redis 127.0.0.1:6379> SET nmask "cool"
OK

Get the value of the key:

1
2
redis 127.0.0.1:6379> GET nmask
cool

Delete key:

1
redesign 127.0.0.1:6379> DEL nmask

Description: The above three are the most commonly used commands (for string types, other types of please look down), of course, there are many advanced syntax.

Get all the keys:

1
redis 127.0.0.1:6379> keys *

Check if the given key exists:

1
redis 127.0.0.1:6379> EXISTS key

redisData Structure

Simply put, the data structure is the format of redis storage data, different data structures, and the commands used for operation are different.

string

1
2
3
4
redis 127.0.0.1:6379> SET url "https://thief.one"
OK
redis 127.0.0.1:6379> GET url
"https://thief.one"

hash value

1
2
3
4
5
6
7
8
9
10
redis 127.0.0.1:6379> HMSET user:1 username nmask password nmask points 200
OK
redis 127.0.0.1:6379> HGETALL user:1
1) "username"
2) "nmask"
3) "password"
4) "nmask"
5) "points"
6) "200"
redis 127.0.0.1:6379>

List

1
2
3
4
5
6
7
8
9
10
11
redis 127.0.0.1:6379> lpush ziqiangxuetang.com redis
(integer) 1
redis 127.0.0.1:6379> lpush ziqiangxuetang.com mongodb
(integer) 2
radish 127.0.0.1:6379> lposh zikianghuetang.kom rabittya
(integer) 3
redis 127.0.0.1:6379> lrange ziqiangxuetang.com 0 10
1) "Rabbit"
2) "mongodb"
3) "redis"
redis 127.0.0.1:6379>

set collection

1
2
3
4
5
6
7
8
9
10
11
12
13
redis 127.0.0.1:6379> sadd ziqiangxuetang.com redis
(integer) 1
redis 127.0.0.1:6379> sadd ziqiangxuetang.com mongodb
(integer) 1
redis 127.0.0.1:6379> sadd ziqiangxuetang.com rabitmq
(integer) 1
redis 127.0.0.1:6379> sadd ziqiangxuetang.com rabitmq
(integer) 0
redis 127.0.0.1:6379> smembers ziqiangxuetang.com
1) "Rabbit"
2) "mongodb"
3) "redis"

zset ordered collection

1
2
3
4
5
6
7
8
9
10
11
12
13
redis 127.0.0.1:6379> zadd ziqiangxuetang.com 0 redis
(integer) 1
redis 127.0.0.1:6379> zadd ziqiangxuetang.com 0 mongodb
(integer) 1
radish 127.0.0.1:6379> ass zikianghuetang.kom 0 rabittya
(integer) 1
radish 127.0.0.1:6379> ass zikianghuetang.kom 0 rabittya
(integer) 0
redis 127.0.0.1:6379> ZRANGEBYSCORE ziqiangxuetang.com 0 1000
1) "redis"
2) "mongodb"
(h) "Rabbit"

redis configuration

The Redis configuration file is located in the Redis installation directory and the file name is redis.conf. Modify the configuration by modifying the redis.conf file or modifying the configuration by using the command line.

Command line mode

Basic command format:

1
redis 127.0.0.1:6379> CONFIG GET CONFIG_SETTING_NAME

Get all configuration items:

1
redis 127.0.0.1:6379> CONFIG GET *

Set the log level:

1
2
3
4
5
redis 127.0.0.1:6379> CONFIG SET loglevel "notice"
OK
redis 127.0.0.1:6379> CONFIG GET loglevel
1) "loglevel"
2) "notice"

Configuration File

The redis.conf configuration item is described as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
1. Redis does not run as a daemon by default. It can be modified by this configuration item. Use yes to enable the daemon.
daemonize no
2. When Redis runs as a daemon, Redis will write the pid to the /var/run/redis.pid file by default, which can be specified by pidfile.
pidfile /var/run/redis.pid
3. Specify the Redis listening port. The default port is 6379. The author explains in a blog post why 6379 is used as the default port because 6379 is the number corresponding to the MERZ on the phone button, and MERZ is taken from the name of the Italian singer Alessia Merz.
port 6379
4. Bind host address
bind 127.0.0.1
5. When the client is idle for a long time, close the connection. If it is specified as 0, it means to disable the function.
timeout 300
6. Specify the logging level. Redis supports four levels: debug, verbose, notice, warning. The default is verbose.
loglevel verbose
7. Logging mode, the default is standard output. If Redis is configured to run in daemon mode, and here is configured as logging mode as standard output, the log will be sent to /dev/null.
logfile stdout
8. Set the number of databases, the default database is 0, you can use the SELECT <dbid> command to specify the database id on the connection
databases 16
9. Specify how long the update operation is, synchronize the data to the data file, and match multiple conditions.
save <seconds> <changes>
Three conditions are provided in the Redis default configuration file:
save 900 1
save 300 10
save 60 10000
It means that there is 1 change in 900 seconds (15 minutes), 10 changes in 300 seconds (5 minutes) and 10000 changes in 60 seconds.
10. Specify whether to compress data when storing to the local database. The default is yes. Redis uses LZF compression. If you want to save CPU time, you can turn off this option, but it will cause the database file to become huge.
rdbcompression yes
11. Specify the local database file name, the default value is dump.rdb
dbfilename dump.rdb
12. Specify the local database storage directory
to you ./
13. Set the IP address and port of the master service when the machine is slav service. When Redis starts, it will automatically synchronize data from the master.
slaveof <masterip> <masterport>
14. When the master service is password protected, the slav service connects to the master password.
masterauth <master-password>
15. Set the Redis connection password. If the connection password is configured, the client needs to provide the password through the AUTH <password> command when connecting to Redis. The default is off.
requirepass foobared
16. Set the maximum number of client connections at the same time. The default is unlimited. The number of client connections that Redis can open at the same time is the maximum number of file descriptors that the Redis process can open. If maxclients 0 is set, it means no limit. When the number of client connections reaches the limit, Redis will close the new connection and return the max number of clients reached error message to the client.
maxclients 128
17. Specify the maximum memory limit of Redis. Redis will load the data into memory at startup. After the maximum memory is reached, Redis will first try to clear the expired or expired Key. When this method is processed, it still reaches the maximum memory. With the settings, the write operation will no longer be possible, but the read operation will still be possible. Redis' new vm mechanism will store the Key in memory and the Value will be stored in the swap area.
maxmemory <bytes>
18. Specify whether to log after each update operation. Redis is asynchronous to write data to disk by default. If it is not turned on, it may cause data loss for a period of time during power failure. Because redis itself synchronizes data files to be synchronized according to the above save condition, some data will only exist in memory for a period of time. The default is no
appendonly no
19. Specify the update log file name, the default is appendonly.aof
appendfilename appendonly.aof
20. Specify update log conditions, a total of 3 optional values:
No: indicates that the operating system performs data cache synchronization to disk (fast)
Always: indicates that manually calling fsync() to write data to disk after each update operation (slow, secure)
Everysec: means to synchronize once per second (compromise, default)
appendfsync everysec
21. Specify whether to enable the virtual memory mechanism. The default value is no. For a brief introduction, the VM mechanism will store the data in a page. The Redis will transfer the pages with less traffic to the disk, and the pages accessed by the disk will be automatically Swap out to memory (I will carefully analyze the Redis VM mechanism in a later article)
vm-enabled no
22. Virtual memory file path, the default value is /tmp/redis.swap, not shared by multiple Redis instances.
vm-swap-file /tmp/redis.swap
vm-max-memory 0
24. Redis swap file is divided into a lot of pages, an object can be stored on multiple pages, but a page can not be shared by multiple objects, vm-page-size is set according to the size of the stored data, the author It is recommended to store a lot of small objects, the page size is best set to 32 or 64bytes; if you store large objects, you can use a larger page, if you are not sure, use the default value
vm-page-size 32
25. Set the number of pages in the swap file. Since the page table (a type of page that indicates that the page is free or used) is placed in memory, every 8 pages on the disk will consume 1 byte of memory.
vm-pages 134217728
26. Set the number of threads accessing the swap file. It is best not to exceed the number of cores of the machine. If set to 0, all operations on the swap file are serial, which may cause a long delay. The default is 4
vm-max-threads 4
27. Set whether to combine smaller packets into one packet when answering to the client. The default is to enable.
glueoutputbuf yes
28. Specify a special hash algorithm when a certain number or maximum element exceeds a certain threshold
hash-max-zipmap-entries 64
hash-max-zipmap-value 512
29. Specify whether to activate the reset hash. The default is on (this will be described later when introducing the Redis hash algorithm)
activerehashing yes
30. Specify to include other configuration files, you can use the same configuration file between multiple Redis instances on the same host, and each instance has its own specific configuration file.
include /path/to/local.conf

redis+python

Python has a third-party module dedicated to redis, project address: https://github.com/WoLpH/redis-py

Installing pyredis

1
pip install redis

Or github download source installation:

1
python setup.py install

Connecting redis service

1
2
3
4
5
6
>>> pool = redis.ConnectionPool(host='127.0.0.1', port=6379, password="")
>>> conn = redis.Redis(connection_pool=pool)
>>> conn.set('hello','world')
True
>>> conn.get('hello')
b'world'

For more python+redis usage, please go to: http://python.jobbole.com/87305/

redis security

Clear all data:

1
redis 127.0.0.1:6379> flushall

Note: Therefore, the redis password cannot be empty. Redis defaults to no password. As for setting the password, please look for it from above.

Reference article

http://python.jobbole.com/87305/
http://www.runoob.com/redis/redis-install.html

本文标题:Redis related notes

文章作者:nmask

发布时间:2017年11月15日 - 15:11

最后更新:2019年08月16日 - 15:08

原始链接:https://thief.one/2017/11/15/1/en/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

nmask wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!

热门文章推荐: