How do I change between redis database?

Redis

Redis Problem Overview


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?

Redis Solutions


Solution 1 - Redis

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 - Redis

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

Solution 3 - Redis

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsilviomoretoView Question on Stackoverflow
Solution 1 - Redisyojimbo87View Answer on Stackoverflow
Solution 2 - RedisTw BertView Answer on Stackoverflow
Solution 3 - RedisLam.TruongView Answer on Stackoverflow