Redis is a high-performance in-memory database, which is light in size and high in performance, and is widely used in enterprises.
Install Redis
Windows installation
Redis is designed for Linux systems, but there are also teams porting it for Windows. We can download the Windows version of Redis here .
If you use NuGet or Chocolatey, you can also use these tools to install Redis for Windows.
# Nuget
PM> Install-Package Redis-64
# Chocolatey
choco install redis-64
However, I am not going to use the Windows ported version of Redis, because Redis is designed for Linux, and most of the servers used by companies are also Linux systems, so we might as well learn the installation and use of the Linux version of Redis directly.
Linux installation
In contrast, since Redis was designed to be used on Linux from the beginning, Linux installation is relatively simple. There should be Redis software in the official software repositories of mainstream Linux systems, so it can be installed directly using the corresponding package manager.
For example, in ArchLinux, you can install redis with the following command.
sudo pacman -S redis
For other Linux systems, just install Redis using their package manager.
Once installed, start Redis with the command below.
# 让redis开机自启
systemctl enable redis
# 启动redis
systemctl start redis
Docker installation
In fact, the best way is to use Docker to install Redis. Due to the containerization characteristics of Docker, we can package a configured Redis in the image. For example, Ruby, PostGreSQL, Redis and other software are directly packaged in the famous Gitlab Docker image. We can use the integrated Gitlab without any additional configuration.
First pull the Redis image.
docker pull redis
Then start the Redis image, where the name can be modified to the name you want.
docker run --name some-redis -d redis
If you need to persist data, you need to specify the data volume in the startup command. The following command stores the data in by default /data
. If you need to customize the location of the data volume, you can use the --volumes-from some-volume-container
or -v /docker/host/dir:/data
parameter.
docker run --name some-redis -d redis redis-server --appendonly yes
To redis-cli
connect to the Redis server, use the following command.
docker run -it --link some-redis:redis --rm redis redis-cli -h redis -p 6379
For detailed configuration, please refer to the official Docker documentation .
Configure Redis
redis.conf
Under Linux, the Redis configuration file is by default /etc/redis.conf
(may vary depending on the operating system). There are many comments in the configuration file. After reading it carefully, we can configure Redis. The complete sample file can be found here, techstay/redis.conf. The file looks very large, but in fact most of it are comments, and the actual configuration is not much.
Below is a brief introduction to some of the more important configurations. Since I have just started learning Redis, I will not introduce the advanced configuration that follows. Only the most basic and important configurations will be introduced here.
unit conversion
If you need to set the memory size and file size of Redis, you need to set specific values. The unit conversion is converted according to the following bases, without b
the 1000
base, with b
the 1024
base. The storage unit is not case-sensitive, so gb
, gB
, GB
etc. are all the same.
# 1k => 1000 bytes
# 1kb => 1024 bytes
# 1m => 1000000 bytes # 1mb => 1024*1024 bytes # 1g => 1000000000 bytes # 1gb => 1024*1024*1024 bytes
IP address
Use bind
the IP address bound to the configured Redis server, which is the local loopback address by default. If not specified, Redis will run on all network interfaces.
bind 127.0.0.1
Note, however, that binding to all interfaces may raise security concerns. Therefore, considering security issues, it is best to let Redis only bind to a few fixed interfaces.
protected mode
Redis can run in protected mode, which needs to be explicitly turned on.
protected-mode yes
bind
If protected mode is on, and: 1) no specific IP address is explicitly used to bind; 2) no password is set, then Redis will only listen on the local IPv4 and IPv6 loopback addresses ( 127.0.0.1
and ::1
) and Unix Domain Sockets.
The port number
The default port number is 6379
, you can customize the port number if you need higher security.
port 6379
client timeout
Redis will disconnect when the client does not do anything within the specified time (unit: seconds ). The default is 0, which means no disconnection.
timeout 0
TCP connection stock time
This parameter specifies how many seconds the TCP connection will be maintained, the default is 300 seconds.
tcp-keepalive 300
Guardian Mode
Specifies whether Redis runs in daemon mode.
daemonize no
log print
Let's talk about the log level first, there are four log levels, debug
, verbose
, notice
and , and the displayed information is from more to less. warning
If you need to debug, use the first two log levels. If you need to use it in a production environment, it is recommended to use the last two levels. The default value is notice
.
loglevel notice
You can also specify the location of the log file. If not specified, the log information will be output directly to the console by default. If Redis is running in daemon mode and no log file location is specified, then logs will be output to /dev/null
.
logfile ""
save to file
You can set Redis to periodically save memory data to a file to prevent the file from being lost after the server is shut down. Multiple values can be set here. For example, the first line below means that every 900 seconds, if at least 1 key changes, the data is saved; the third line means that every 60 seconds, if at least 10,000 keys change, the data is saved .
save 900 1
save 300 10
save 60 10000
password
Using a password improves the security of the Redis server. It should be noted that due to the high performance characteristics of Redis, a user can try up to 150,000 passwords per second. Therefore, in order to ensure security, the password needs to be set as long as possible.
requirepass foobared
There are other configurations in the configuration file, which I will not introduce. If you need more detailed configuration, you can directly view the comments of the configuration file, and each configuration item has a detailed introduction.
Docker configuration
Docker's Redis image has no redis.conf
file, if you need this file to configure Redis, you can package an included redis.conf
image yourself. This requires writing something like the following Dockerfile
.
FROM redis
COPY redis.conf /usr/local/etc/redis/redis.conf
CMD [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
Of course, there is no need to be so troublesome at all. You can specify the location of the configuration file directly when starting the Redis image. This method is more flexible and simple, so if there is no special need, this will be fine.
docker run -v /myredis/conf/redis.conf:/usr/local/etc/redis/redis.conf --name myredis redis redis-server /usr/local/etc/redis/redis.conf
Using Redis
redis-cli
First of all, let's introduce the command line tool redis-cli
of Redis. We mainly use it to operate the Redis server.
If you want to see help information, you can use the redis-cli --help
command, a part of the output is listed below.
# redis-cli --help
redis-cli 3.2.8
Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]
-h <hostname> Server hostname (default: 127.0.0.1). -p <port> Server port (default: 6379). -s <socket> Server socket (overrides hostname and port). -a <password> Password to use when connecting to the server.
If the host name and port number are both default values, you can enter them directly redis-cli
into the interactive interface. If you need to connect to a Redis server on another host, you can use -h
parameters to specify the host name, -p
parameters to specify the port number, and -a
parameters to specify the password.
# redis-cli
127.0.0.1:6379>
Redis commands
After entering the interactive interface, we can use the various commands provided by Redis to operate the server. There are many commands in Redis, and their functions are also different. You can go to Redis Command to see all commands and uses. If you don't like English, you can check the Redis command reference , which is the finished command documentation.
access data
Here I list some commonly used commands. The first is the access command, which is used set 键 值
to save a piece of data, and it will return after the save is successful OK
.
127.0.0.1:6379> set fuck fuck OK
After saving, you can use get 键
to get this value.
127.0.0.1:6379> get fuck "fuck"
query key
keys 模式
It is used to query the name of the key that matches the pattern. The supported query method is GLOB type , and wildcard characters such as *
, ?
, [a-b]
, etc. are supported.[^a]
127.0.0.1:6379> keys * 1) "fuck" 2) "food_num"
Configure Redis
Similar to the access command, config get
and is config set
used to get and set the configuration in the configuration file. For example, to get all the values of the configuration file, use the following command.
127.0.0.1:6379> config get *
For another example, if you want to set the login password of Redis, you can do so. Then exit redis-cli
and reconnect to take effect.
127.0.0.1:6379> config set requirepass 123456
Certification
If the Redis server has set a password, authentication is required before any operation is performed, otherwise it will be prompted NOAUTH
.
127.0.0.1:6379> get food_num (error) NOAUTH Authentication required.
At this time, you need to use the auth
command to authenticate before you can continue the operation.
127.0.0.1:6379> auth 123456
Of course, you can also specify the password directly redis-cli
using -a
parameters when connecting.
redis-cli -a 123456
timeout and persistence
You can use the expire
command to set a timeout value (unit: seconds) for a key, after which the value will be deleted.
127.0.0.1:6379> expire fuck 10 (integer) 1
If you look at the value again after 10 seconds, you will find that it no longer exists.
127.0.0.1:6379> get fuck (nil)
Correspondingly, there is also a persist
command that will cancel the timeout value of the data, so that the data will always exist as long as the database is still there.
127.0.0.1:6379> persist fuck (integer) 0
In addition to using the expire
command to set the timeout value for the existing value, you can also specify the timeout value directly when setting the data.
set key 100 ex 10
After specifying the timeout value, you can use the ttl
command to see how much time is left until the timeout value.
127.0.0.1:6379> ttl fuck (integer) 17
quit
Finally, the quit
command line interface can be exited with the command.
Original link: https://segmentfault.com/a/1190000009247586