Redis has several commands that allow you to change the configuration settings of your Redis server on the fly. This tutorial will introduce some of these commands and explain how to make these configuration changes permanent.
How to use this guide
This guide is written as a cheat sheet with complete examples. We encourage you to skip to any section that is relevant to the task you want to complete.
The commands shown in this guide were tested on an Ubuntu 18.04 server running Redis version 4.0.9. To set up a similar environment, you can follow step 1 of our guide How to install and secure Redis on Ubuntu 18.04. We will demonstrate the behavior of these commands by running them using the Redis command line interface. Note that if you use another Redis interface (such as Redli), the exact output of some commands may vary.
Please note that hosted Redis databases generally do not allow users to change configuration files. If you are using DigitalOcean's managed database, the commands outlined in this guide will cause errors.
Change the configuration of Redis
The commands outlined in this section will only change the behavior of the Redis server during the current session or until you run config rewrite which will make them permanent. You can change it directly by opening and editing the Redis configuration file with your preferred text editor. For example, you can nano do this:
sudo nano /etc/redis/redis.conf
WARNING: This config set command is considered dangerous. By changing the Redis configuration files, it is possible to cause the Redis server to behave in an unexpected or undesirable manner. We recommend running the config set command only when testing the command's behavior or when you are absolutely sure you want to make changes to your Redis configuration.
You may want to rename this command to one that is less likely to be run accidentally.
The config set allows you to reconfigure Redis at runtime without restarting the service. It uses the following syntax:
config set parameter value
For example, if you want to change the name of the database dump file that Redis will produce after running the save command, you can run a command like this:
config set "dbfilename" "new_file.rdb"
If the configuration changes are valid, The command will return OK. Otherwise an error will be returned.
Note: Not every parameter in the redis.conf file can be changed through the config set operation. For example, you cannot change the authentication password defined by the requirepass parameter.
Making configuration changes permanently
The config set does not permanently change the configuration file of the Redis instance; it only changes the behavior of Redis at runtime. To edit redis.conf after running the config-set command and make the current session's configuration permanent, run config rewrite:
config rewrite
This command will do its best to preserve the comments and entirety of the original redis.conf file structure with minimal changes required to match the settings currently used by the server.
Just like config set, config rewrite will return OK if the rewrite is successful.
Check the configuration of Redis
To read the current configuration parameters of the Redis server, run the config get command. config get has only one parameter, which can be an exact match of either of the parameters used in redis.conf or glob pattern). For example:
config get repl*
Depending on your Redis configuration, this command may return:
Output 1) "repl-ping-slave-period" 2) "10" 3) "repl-timeout" 4) "60" 5) "repl-backlog-size" 6) "1048576" 7) "repl-backlog-ttl" 8) "3600" 9) "repl-diskless-sync-delay"10) "5"11) "repl-disable-tcp-nodelay"12) "no"13) "repl-diskless-sync"14) "no"
You can also return all supported config set by running Configuration parameters config get *.
Related references:Redis Tutorial
The above is the detailed content of How to modify the redis configuration through the command line. For more information, please follow other related articles on the PHP Chinese website!