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.
Redis How to get current database name
How about - redis.io/commands/info –
Containerize
@sricheta ThankYou! Is there any command to get #Keyspace from Info? Then parsing inside my application to fetch db name becomes simple –
Lupus
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:
- call
CLIENT SETNAME a-unique-name
to set a unique name for the current connection. - call
CLIENT LIST
to get info of all clients that connecting to Redis. - find the connection info with the unique name that we set in step 1.
- 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 :)
UPDATE: Since Redis 6.2.0, you can use CLIENT INFO
to get the information of current connection. So that you don't need to run step 1 - 3 mentioned above.
Since Redis version 6.2.0 you can use the CLIENT INFO command, the response of which contains both the name and the db number of the current client connection.
© 2022 - 2024 — McMap. All rights reserved.