Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis How to get current database name

Tags:

redis

Is there any command in Redis to get the database name that I am using right now? I know select is to switch the database. I am looking for "get database name" kind of a command.

like image 729
RG1 Avatar asked May 25 '18 17:05

RG1


People also ask

How do I find my Redis database name?

There is no command to do it (like you would do it with MySQL for instance). The number of Redis databases is fixed, and set in the configuration file. By default, you have 16 databases. Each database is identified by a number (not a name).

How do I select a database in Redis?

Redis databases are numbered from 0 to 15 and, by default, you connect to database 0 when you connect to your Redis instance. However, you can change the database you're using with the select command after you connect: select 15.

How do I find my Redis server?

The Redis configuration file is located at installdir/redis/etc/redis.

What is DB in Redis?

Databases. Out of the box, every Redis instance supports 16 databases. The database index is the number you see at the end of a Redis URL: redis://localhost:6379/0 . The default database is 0 but you can change that to any number from 0-15 (and you can configure Redis to support more databases, look in redis.


1 Answers

First of all, there's NO name for Redis database. Instead, it has an index.

You can use the CLIENT SETNAME and CLIENT LIST commands to get the database index dynamically:

  1. call CLIENT SETNAME a-unique-name to set a unique name for the current connection.
  2. call CLIENT LIST to get info of all clients that connecting to Redis.
  3. find the connection info with the unique name that we set in step 1.
  4. parse the client info the get the database index.

You can get the format of client info from the doc.

NOTE: If anyone has a simpler solution, please let me know :)

like image 189
for_stack Avatar answered Sep 18 '22 00:09

for_stack