Guide on the Redis Databases
Introduction
This tutorial will provide an overview of Redis databases and using the Redis command line interface. Every Redis database instance will support 16 databases. While the default database is “0,” this can be changed to any number from 0-15 and can also be configured to support additional databases. To help avoid confusion, each database provides a distinct keyspace that is independent from all of the other databases and the database index number is listed at the end of the Redis URL. Additionally, separate instances of Redis can be run on different ports, providing even greater flexibility.
Prerequisites
Redis must be properly installed and configured on the local operating system. Execute
redis-cli INFO
command to return the currently installed version of Redis and other data stored on the Redis server.A basic working knowledge of the Redis database and the interactive shell for Redis, which is the Redis command-line interface, or redis-cli tool, used for development is required to execute the examples provided in this tutorial.
Use Redis commands to list databases
Access must first be obtained to the Redis server to use the Redis commands to list databases. Execute the following network protocol command to log into the Redis server:
1 | telnet 127.0.0.1 6379 |
Now executing the CONFIG GET DATABASES
command should return the following results:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | linux@linux-NECq:~$ telnet 127.0.0.1 6379 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. config get databases *2 $9 databases $2 16 info keyspace $109 # Keyspace db0:keys=70,expires=0,avg_ttl=0 db1:keys=1,expires=0,avg_ttl=0 db2:keys=1,expires=0,avg_ttl=0 |
Note here, that by default, the total number of the databases in Redis is 16.
Use the Redis cli to list the databases
As shown in the following example, the client server, a tool for the interactive terminal in Redis, can be used to list a database instance:
1 | linux@linux-NECq:~$ redis-cli INFO | grep db |
The output from the above command should resemble the following:
1 2 3 4 5 6 7 8 9 10 11 | run_id:aa37313723c4b0c9ef3c05ff90db96ea8559ef4b rdb_changes_since_last_save:1 rdb_bgsave_in_progress:0 rdb_last_save_time:1580966765 rdb_last_bgsave_status:ok rdb_last_bgsave_time_sec:0 rdb_current_bgsave_time_sec:-1 rdb_last_cow_size:397312 db0:keys=70,expires=0,avg_ttl=0 db1:keys=1,expires=0,avg_ttl=0 db2:keys=1,expires=0,avg_ttl=0 |
This next command provides for viewing the number of databases currently stored in Redis on the local device:
1 2 | linux@linux-NECq:~$ redis-cli INFO | grep db | wc -l 11 |
Get the databases config which shows all the available databases in Redis
Executing the following CONFIG GET DATABASES
command will have the Redis tool show all of the available databases in Redis:
1 2 3 | 127.0.0.1:6379> CONFIG GET DATABASES 1) "databases" 2) "16 |
Show the Redis databases info with the keyspace parameter
Executing the following command with the keyspace parameter in the Redis client will display all of the Redis databases that have already been used and all of the keys that have been created inside the databases:
1 2 3 4 5 | 127.0.0.1:6379> INFO keyspace # Keyspace db0:keys=70,expires=0,avg_ttl=0 db1:keys=1,expires=0,avg_ttl=0 db2:keys=1,expires=0,avg_ttl=0 |
NOTE: Remember that the databases in Redis are identified as numbers, not by name.
Switch to a database in Redis
This command will switch to and configure another database in Redis:
1 | 127.0.0.1:6379> SELECT [index] |
As an example, the following command will bring up database “10:”
1 2 3 | 127.0.0.1:6379> SELECT 10 OK 127.0.0.1:6379[10]> |
Notice the brackets encasing the number 10
at the end of the last line. This indicates the switch to that Redis database was successful.
Conclusion
This tutorial provided an overview of Redis databases and using the Redis command line interface. The tutorial first provided an explanation of how to obtain access to the server and use the Redis commands and the Redis command line interface to list the databases. The article then covered how to obtain the databases configuration file that shows all the available databases in Redis, how to display the Redis databases information with the keyspace parameter and how to switch between different Redis databases. Remember that the databases in Redis are identified as numbers, not by name, and that the database index number is listed at the end of the Redis URL.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started