How do I change between redis database?
Asked Answered
D

3

97

I am new with redis and I didn't figured out how to create and change to another redis database.

How do I do this?

Diamine answered 14/11, 2012 at 19:37 Comment(5)
See SELECT command.Branny
Thank you Sergio, and how do I create a new?Diamine
It already exists (16 databases by default, IIRC)Branny
Ok. Now I get it. I just use something like: SELECT [0-15] and I can't create a custom indexDiamine
Basically, yes. I shall ask, what do you want to use redis databases for? It seems to me that they are easily replaceable with namespacing or client-side sharding.Branny
C
158

By default there are 16 databases (indexed from 0 to 15) and you can navigate between them using select command. Number of databases can be changed in redis config file with databases setting.

By default, it selects the database 0. To select a specified one, use redis-cli -n 2 (selects db 2)

Cantabrigian answered 14/11, 2012 at 21:39 Comment(0)
J
58

Note: this is not a direct answer to the OP's question. However, this text is too long for a comment, and I thought I'd share it anyway, to clarify things to the OP. Hope I don't break too many SO rules by doing this...

Some extra info on multiple databases:

Please note that using multiple databases in one redis instance is discouraged.

It is a nice feature for playing around and getting to know redis.

In more serious setups, if you have multiple ports at your disposal, it's preferred and more performant to use separate instances. At our company, we run about 50 instances on the development/staging server, and about 5 on the production server.

The reason is, that redis transactions are only atomic within one db number anyway. Most (if not all) clients nicely seperate that for you in the connect() phase. And if you have to connect separately, it's just as easy to connect to a different port.

The core of redis is also single threaded. That's one of the things that makes redis so quick and simple. Everything is sequential. If you use multiple instances instead of just one, you gain the benefit of multi-processing (on multi-core machines).

Jameson answered 26/1, 2014 at 22:29 Comment(1)
But if i have installed redis in a server that run many websites that, and i want to use redis for cache, i need to use a different DB for one site? Or i need a single instance single db, for one site? Or i can simple use DB 0 for all my sites? Thanks!Dunaj
R
6
redis-cli //connect server firstly 
redis-cli info //show all existing database - at the bottom 
//exit
redis-cli -n 1 //1 is the name of database
Recede answered 26/4, 2022 at 2:8 Comment(1)
small remark: redis-cli info will show only the databases with keys present. Apart from that, great answer, should be much higher scoredSade

© 2022 - 2024 — McMap. All rights reserved.