Redis configuration and common command introduction and troubleshooting

The principle and background of Redis will not be introduced in detail. This time, the window version of redis is used. Although it is based on window, the common configuration commands are consistent with linux.

One, Redis startup and configuration file configuration

  1. Redis supports local command line startup and windows service startup

    If you use the command line to start, enter CMD, enter the redis directory, and execute the following command:

    redis-server.exe redis.windows.conf, the following figure shows that the startup is successful.

    Steps: cmd>e: Enter, cd directory name, enter, assign the above command to start

    Some high version systems may need .\ in redis-server.exe redis.windows.conf to start.

    Some systems can start directly by double-clicking redis.exe in the folder.

  2. Two important configuration files redis.windows.conf and redis.windows-service.conf description

    There are two configuration files in the redis root directory: redis.windows.conf and redis.windows-service.conf. One of the two configuration files is configured for command line startup and the other is configured for the startup mode of the window service. It is recommended that the configuration information in the two configurations should be consistent and synchronized to configure otherwise it will cause an error to the original application connection when switching the startup mode.

    A. Configure Redis to allow remote connections.
    In the bind command of the configuration file, set multiple ips separated by spaces. Only the ips set with the bind command can be accessed remotely. Otherwise, it can only be accessed by 127.0.0.1 by default. The writing method is:

	bind 192.168.0.3  192.168.1.123  ...
使用bind命令设置多个ip可访问redis。
设置bind命令后,还需要关闭默认保护,在conf配置文件中将
protected-mode yes 
改成
protected-mode no
如果以上两步都设置了,当远程客户端连接redis时报错:

Error: When the disk is in use or locked by another process , the configuration has not been successfully enabled. You need to open redis-cli.exe locally. After opening the connection, the interface displays 127.0.0.1:6379> and then use the following command to configure and configure Show ok means success

	config set protected-mode no

B. Set password

配置文件中使用如下命令设置密码
	requirepass  abc23
如果设置密码重启redis后密码没有生效则,说明配置没有成功启用,需要在本地打开redis-cli.exe,打开连接后界面显示127.0.0.1:6379>后使用下面命令进行配置,配置显示ok就代表成功。
	config set requirepass  abc123

C. Use the client to connect to this machine or remote redis.
Client connection method:

	redis-cli -h localhost -p 6379
	提供host为localhost ip,端口为6379

	带密码的客户端连接方法一(localhost是要连接主机的ip):
	redis-cli -h localhost -p 6379 monitor -a 123456
	监控host为localhost,端口为 6379  -a 为连接密码

	密码验证方法二:
	先使用命令行输入(localhost是要连接主机的ip):
	redis-cli -h localhost -p 6379
再输入密码验证
	auth 123456   
123456是密码

具体命令输入参见: 

Case study

也可以使用windows下的客户端软件:Redis Desktop Manager

D. Reload the configuration

	重新载入配置时若一直是在redis界面,修改如下参数后重新载入即可
	daemonize yes

Two, Redis common commands

Common commands can be used directly on the client

  • keys
 KEYS pattern

Find all keys that match the given pattern.

KEYS * matches all keys in the database.
KEYS h?llo matches hello, hallo, hxllo, etc.
KEYS h*llo matches hllo, heeeeello, etc.
KEYS h[ae]llo matches hello and hallo, but not hillo.
Examples of keys command

  • set
	SET key value [EX seconds] [PX milliseconds] [NX|XX]

Associate the string value value to key.

If the key already holds another value, SET overwrites the old value, regardless of the type.

For a key with a time-to-live (TTL), when the SET command is successfully executed on this key, the original TTL of this key will be cleared.

Optional parameters

Starting from Redis 2.6.12, the behavior of the SET command can be modified through a series of parameters:

EX second: Set the expiration time of the key to second seconds. SET key value EX second has the same effect as SETEX key second value.
PX millisecond: Set the expiration time of the key to millisecond milliseconds. The effect of SET key value PX millisecond is equivalent to PSETEX key millisecond value.
NX: Only when the key does not exist, can the key be set. SET key value NX has the same effect as SETNX key value.
XX: Only when the key already exists, can the key be set.
set command example

  • get
	GET key

Returns the string value associated with key.

If the key does not exist, the special value nil is returned.

If the value stored in the key is not a string type, an error is returned, because GET can only be used to process string values.
Example of get command

For more Redis commands, please refer to specific commands

Three, configure Redis as a windows service

可以参见redis文件夹中的Windows Service Documentation.docx文件。
1、使用命令行进入redis的目录后,使用如下命令安装windows服务
	redis-server --service-install redis.windows-service.conf --loglevel verbose
注意必须安装redis.windows-service.conf这个配置文件,因为这个配置文件中比redis.windows.conf多了一个指定日志输出的命令:
	logfile "server_log.txt"
代表日志输出到server_log.txt文件中。
有些版本logfile命令指定输出到

Insert picture description here
Solution: Create a new directory Logs in the directory where redis.windows-service.conf is located, and then start the Redis service. Otherwise, start the service and report an error 1067.
In addition, use the windows service. After the bind command, only 127.0.0.1 and local ip can be used address. 1067 error may be displayed when binding other addresses on the LAN.
2. Uninstall [redis] service
redis-server --service-uninstall --service-name redistest
Note that the name of the uninstalled service is redistest
3. Start and stop the service
Start command:

	redis-server --service-start
停止命令:
	redis-server --service-stop
4、安装多个实例
	redis-server --service-install –service-name redisService1 –port 10001
	redis-server --service-start –service-name redisService1
	redis-server --service-install –service-name redisService2 –port 10002
	redis-server --service-start –service-name redisService2
	redis-server --service-install –service-name redisService3 –port 10003
	redis-server --service-start –service-name redisService3

For the use and installation of redis, it is best to refer to the two docx files that come with the folder.

Guess you like

Origin blog.csdn.net/u011930054/article/details/108994823