How do I change between redis database?

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?


Solution 1:

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)

Solution 2:

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).