Article directory
-
- 1. Several ways to install Redis under Windows
- Two, Redis installation
-
- 1. msi installation package installation
- 2. zip installation package installation
-
- 1) Unzip the installation package
- 2) Start the Redis service
- 3) Connect to the Redis service
- 4) Set the service to start automatically
-
- a. Use the sc create command to view the help
- b. Use the sc create command to create a service item
- c. Use the sc query command to check the service status
- d. Use the sc start command to start the service
- e. Use the sc qc command to view the service configuration
- f. Use the reg query command to view registry information
- 5) Test the Redis service
- 6) Add the PATH environment variable
- 7) Stop or delete the Redis service
- 3. Redis configuration
1. Several ways to install Redis under Windows
Redis official website address: https://redis.io/
Redis source code address: https://github.com/redis/redis
Redis is not officially supported on the Windows platform, and Redis officially only provides source packages (zip, tar.gz format). Of course, it is still possible to install Redis on Windows.
1. Redis officially maintained by Microsoft
You can download the Redis installation package that supports the Windows platform officially maintained by Microsoft.
Download address: https://github.com/microsoftarchive/redis/releases
Note: There are only some versions of Redis2.0 and Redis3.0, and the last update was in 2016 , is currently down for maintenance. And as of June 2023, the latest version of Redis official website is Redis7.2
2. Redis maintained by tporadowski
tporadowski also provides a Redis installation package that supports the Windows platform, which is still being maintained. The latest version is 5.0.14, and the update speed is several versions behind the official Redis website.
Download address: https://github.com/tporadowski/redis/releases
3. Install Redis using WSL
WSL (Windows Subsystem for Linux), WSL is the Linux subsystem under Windows, which is also the official introduction of Redis to install Redis under Windows.
WSL2 needs to be started, and WSL2 requires Windows 10 version 2004+ or Windows 11. For details, see the official website introduction: install-redis-on-windows
4. Use gcc compiler to compile redis source code under Windows
Install MinGw (install gcc to compile c code) or Cygwin (provide a virtual Linux environment, similar to WSL)
MinGW address: https://sourceforge.net/projects/mingw/
Cygwin address: https://cygwin.com/install .html
Summary: Of the above four methods, method 3 has higher requirements for the Windows version (at least my system is not satisfied); method 4 requires the installation of third-party tools, which is also more troublesome, and I have been struggling for a long time without success; method 1 supports Redis The version is too low; although method 2 does not support the latest version, it is stronger than method 1.
Two, Redis installation
Here, Redis maintained by tporadowski is used for installation demonstration
. Download and install packages: Redis-x64-5.0.14.1.msi, Redis-x64-5.0.14.1.zip
1. msi installation package installation
1) Double-click the msi installation package
Double-click the msi installer to open the installation wizard, click next
2) Accept the End User Agreement
Accept the end user agreement, click next
3) Select the installation path
Select the installation path, and check the system PATH environment variable that will add the installation path
4) Set the service port
Set the Redis service port, the default is 6379, click next
5) Set the maximum memory limit
Set the maximum memory limit, click next
Note:
If the application scenario of redis is used as a database, it is not recommended to set the maximum memory limit, because the database cannot tolerate data loss; if it is used as a temporary cache, it depends on the actual need to set it.
If you specify the Redis maximum memory limit, Redis will load the data into the memory at startup. After reaching the maximum memory, Redis will first try to clear the Key that has expired or is about to expire. When this method is processed, the maximum memory setting is still reached , write operations will no longer be possible, but read operations will still be possible.
Of course, you can also configure specific policies to deal with the situation when the data reaches the maximum limit of memory, see redis official website for details: Eviction policies
6) Complete the installation
Click install to complete the installation
7) Verify Redis
Open the "Task Manager", you can see that the Redis service has been started under the service list
Check the "System Environment Variables", PATH variable, the path of Redis has been added
Open the cmd window, enter redis-cli to connect to the redis service, and do a simple verification
8) Uninstall Redis
In order to facilitate the installation demonstration of the zip installation package below, the Redis installed above is uninstalled here.
Find the Redis application in the "Settings" > "Apps and Features" list, click "Uninstall"
2. zip installation package installation
The zip installation is relatively simple, just unzip the zip installation package to a certain path. (First uninstall the installed Redis)
1) Unzip the installation package
2) Start the Redis service
Open the cmd window, switch to the Redis installation path, and enter redis-server to start the redis service
3) Connect to the Redis service
Open a new cmd window, switch to the Redis installation path, and enter redis-cli to connect to the Redis service
4) Set the service to start automatically
Set the service to start in the background, and start automatically when the system is turned on.
The service started by the cmd command redis-server in step 2), when the cmd window is closed, the service will be closed. If you want the Redis service to run in the background and start automatically when the system starts, you can use the sc command to complete it. The specific implementation process is as follows:
SC is a command-line program used to communicate with the service control manager and services, you can enter sc in the cmd window to view help information
Use the administrator identity to open the cmd window
(note that you must use the administrator identity, otherwise an error may be reported, such as: [SC] OpenSCManager failed 5)
a. Use the sc create command to view the help
sc create
b. Use the sc create command to create a service item
sc create Redis binPath= "D:\Chen\MySoft\Redis\redis-server.exe --service-run D:\Chen\MySoft\Redis\redis.windows-service.conf" start= auto
Please change the path in the binPath parameter value to your own Redis service path and service configuration path.
Start is set to auto, which means that the service will start automatically after the computer is restarted.
c. Use the sc query command to check the service status
sc query Redis
d. Use the sc start command to start the service
sc start Redis
e. Use the sc qc command to view the service configuration
sc qc Redis
f. Use the reg query command to view registry information
reg query HKLM\SYSTEM\CurrentControlSet\Services\Redis
5) Test the Redis service
In the redis decompression path, open cmd, enter the redis-cli command, and test whether redis can be used normally
(reg start Redis can only be connected after the service is started)
6) Add the PATH environment variable
In step 5), when connecting to the Redis service, each time you need to enter the Redis path to execute redis-cli, we can add the Redis path to the PATH environment variable to optimize the operation path.
In this way, you can use the redis-cli command in any path in the cmd window.
(Note: The cmd window that was opened before adding the environment variable still cannot find the redis-cli command, reopen a new cmd window for verification)
7) Stop or delete the Redis service
a. Use the sc stop command to stop (disable) the service
sc stop Redis
b. Use the sc delete command to delete the service
(Sc stop stops the service before performing the delete operation)
sc delete Redis
3. Redis configuration
1. View Redis configuration
The configuration file of Redis is located in the Redis installation directory. The file names are redis.windows.conf and redis.windows-service.conf. The
redis service installed through the msi installation package uses redis.windows-service.conf by default; it is
installed through zip The redis service installed by the package uses the redis configuration specified by yourself;
if you directly use redis-server.exe to start the service without specifying the path of the service configuration file, the default configuration is redis.windows.conf
1) Use the CONFIG GET command of Redis to view
格式:CONFIG GET CONFIG_SETTING_NAME
Note: In the returned result, the odd number is the configuration item name, and the even number is the configuration item value, appearing in pairs, and the value is empty, then it is ""
2) Use the file editor to view configuration information directly
Note: In the configuration file, # acts as a comment, the uncommented part is the configuration information, and the commented part explains the configuration information in detail.
2. Modify Redis configuration
1) Use the CONFIG SET command of Redis to modify
Note:
The configuration queried through config get is the configuration information loaded into the memory when the Redis service starts. If the configuration information is modified through config set, it only modifies the configuration information in the memory and does not change the configuration file, and the service After restarting, the data in the configuration file will be reloaded to the running memory
2) Modify the configuration file and change the configuration information
Note:
The configuration information in the configuration file will be loaded into the running memory after the service is started. Therefore, after modifying the configuration file, if you want the modified configuration to take effect, you need to restart the Redis service
3. Description of Redis configuration items
The functions and usage methods of each configuration item are described in detail in the configuration file. Next, a table is used to briefly summarize each configuration item.
configuration item | illustrate |
---|---|
daemonize no | Redis does not run as a daemon by default, it can be modified through this configuration item, use yes to enable the daemon (Windows does not support the configuration of the daemon thread is no) |
pidfile /var/run/redis.pid | 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 |
port 6379 | Specify the Redis listening port. The default port is 6379. The author explained why he chose 6379 as the default port in his own blog post, because 6379 is the number corresponding to MERZ on the phone button, and MERZ is taken from the name of the Italian singer Alessia Merz |
bind 127.0.0.1 | Binding host address |
timeout 300 | How many seconds after the client is idle to close the connection, if it is specified as 0, it means that the function is closed |
loglevel notice | Specify the logging level, Redis supports four levels in total: debug, verbose, notice, warning, the default is notice |
logfile stdout | Logging mode, the default is standard output. If Redis is configured to run as a daemon process, and here it is configured to log as standard output, the log will be sent to /dev/null |
databases 16 | Set the number of databases, the default database is 0, you can use the SELECT command to specify the database id on the connection |
save <seconds> <changes> | Specify how long and how many update operations there are to synchronize the data to the data file, and multiple conditions can be matched. Three conditions are provided in the Redis default configuration file: save 900 1 save 300 10 save 60 10000 means 1 change within 900 seconds (15 minutes), 10 changes within 300 seconds (5 minutes) and 60 seconds 10000 changes. |
rdbcompression yes | Specifies 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 |
dbfilename dump.rdb | Specify the local database file name, the default value is dump.rdb |
dir ./ | Specify the local database storage directory |
slaveof <masterip> | Set when the machine is a slave service, set the IP address and port of the master service, and when Redis starts, it will automatically synchronize data from the master |
masterauth <master-password> | When the master service is password protected, the password for the slave service to connect to the master |
requirepass foobared | 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. It is disabled by default. |
maxclients 128 | 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 is set to 0, it means there is 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 |
maxmemory <bytes> | Specify the maximum memory limit of Redis. Redis will load data into the memory when it starts. After reaching the maximum memory, Redis will first try to clear the keys that have expired or are about to expire. After this method is processed, the maximum memory setting is still reached. Write operations will no longer be possible, but read operations will still be possible. The new vm mechanism of Redis will store the Key in the memory and the Value in the swap area |
appendonly no | Specify whether to perform log records after each update operation. By default, Redis writes data to disk asynchronously. If it is not enabled, it may cause data loss for a period of time when power is off. Because redis itself synchronizes data files according to the above save conditions, some data will only exist in memory for a period of time. The default is no |
appendfilename appendonly.aof | Specify the update log file name, the default is appendonly.aof |
appendfsync everysec | Specify update log conditions, there are 3 optional values:
|
vm-enabled no | Specify whether to enable the virtual memory mechanism. The default value is no. Briefly, the VM mechanism stores the data in pages, and Redis swaps the pages with less access, that is, cold data, to the disk, and the pages with more access are automatically swapped out by the disk. into memory (I will carefully analyze the VM mechanism of Redis in a later article) |
vm-swap-file /tmp/redis.swap | Virtual memory file path, the default value is /tmp/redis.swap, which cannot be shared by multiple Redis instances |
vm-max-memory 0 | Store all data larger than vm-max-memory into virtual memory, no matter how small vm-max-memory is set, all index data is stored in memory (redis index data is keys), that is, when vm-max When -memory is set to 0, all values actually exist on disk. The default value is 0 |
vm-page-size 32 | The Redis swap file is divided into many pages. An object can be stored on multiple pages, but a page cannot be shared by multiple objects. The vm-page-size should be set according to the size of the stored data. The author recommends that if To store many small objects, the page size is best set to 32 or 64bytes; if you store very large objects, you can use a larger page, if you are not sure, use the default value |
vm-pages 134217728 | Set the number of pages in the swap file. Since the page table (a bitmap that indicates the page is free or used) is placed in memory, every 8 pages on the disk will consume 1byte of memory. |
vm-max-threads 4 | Set the number of threads to access the swap file. It is best not to exceed the number of cores of the machine. If it is set to 0, all operations on the swap file will be serial, which may cause a long delay. The default value is 4 |
glueoutputbuf yes | Set whether to combine smaller packages into one package when responding to the client, the default is enabled |
hash-max-zipmap-entries 64 hash-max-zipmap-value 512 |
Specifies that a special hash algorithm is used when a certain number is exceeded or the largest element exceeds a certain threshold |
activerehashing yes | Specifies whether to activate the reset hash, which is enabled by default (it will be introduced later in the introduction of the hash algorithm of Redis) |
include /path/to/local.conf | Specify to include other configuration files, you can use the same configuration file among multiple Redis instances on the same host, while each instance has its own specific configuration file |