Home > Database > Redis > body text

About redis database quantity configuration, switching and specifying database

藏色散人
Release: 2020-10-10 15:03:01
forward
2996 people have browsed it

The following column Redis Tutorial will introduce to you the configuration, switching and specifying the number of redis databases. I hope it will be helpful to friends in need!

About redis database quantity configuration, switching and specifying database

The number of redis databases can be configured, the default is 16, see databases 16 in redis.windows.conf/redis.conf.
The index value of the corresponding database is 0 - (databases -1), that is, 16 databases, the index value is 0-15. The default stored database is 0.

1. Command line switching
redis-cli -a 123456
Log in to redis, database 0 is selected by default. If you need to switch to other databases, use the select index value. For example, select 1 means switching to the index. Database with value 1.

D:\software\redis>redis-cli -a 123456
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]>
Copy after login

After the switch, the new database will be operated until the next switch takes effect.

2. Springboot specifies redis database

#redis
 spring.redis.host=localhost
 spring.redis.password=123456
 spring.redis.port=6380  //redis ssl端口
 spring.redis.database=2 //使用的数据库索引
 spring.redis.ssl=true  //是否使用ssl,默认为false
 spring.redis.pool.maxActive=100
 spring.redis.pool.maxWait=1000000
 spring.redis.pool.maxIdle=10
 spring.redis.pool.minIdle=0
 spring.redis.timeout=0
 spring.redis.testOnBorrow=true
 spring.redis.testOnReturn=true
 spring.redis.testWhileIdle=true
Copy after login

In the source code RedisProperties.java, the initial value of database is 0 (private int database = 0;), so when configuring redis in springboot If you do not specify a database, database No. 0 will be used by default. If you configure this value, your own configured database will be used.

3. Python specifies the redis database

Set the database to be used through the db parameter. For example, db=1 means using the database with index value 1.

redis-py provides two classes, Redis and StrictRedis, for implementing Redis commands. StrictRedis is used to implement most official commands and uses official syntax and commands (for example, the SET command corresponds to StrictRedis.set method).
Redis is a subclass of StrictRedis and is used for backward compatibility with older versions of redis-py. Simply put, the official recommendation is to use the StrictRedis method.

r = redis.StrictRedis(host='127.0.0.1', port=6379, password='123456', db=2, ssl=False)
 r = redis.Redis(host='127.0.0.1', port=6379, password='123456', db=2, ssl=False)
Copy after login

Note:
If redis enables ssl connection, add ssl=True to enable ssl connection.
Such as redis.StrictRedis(host='127.0.0.1', port=6380, password='123456', db=2, ssl=True). Then use SSLConnection when creating the connection.

Connection pool connection:

pool = redis.ConnectionPool(host='127.0.0.1', port=6379, password='123456', db=2)
r = redis.Redis(connection_pool=pool)
Copy after login

Remarks:
Using the above method to initialize the connection pool cannot enable ssl connection through the ssl parameter:

class ConnectionPool(object):
     def __init__(self, connection_class=Connection, max_connections=None,
                  **connection_kwargs):
Copy after login

Connection is used here.

If you need to use ssl connection, use the from_url method to initialize the connection pool when initializing the connection pool. The parameter format is as follows:

rediss://[:password]@localhost:6379/0 ,6379表示端口,0表示使用的数据库索引值。
 pool = redis.ConnectionPool.from_url('rediss://:123456@localhost:6380/2')
 r = redis.StrictRedis(connection_pool=pool)
 ret = r.get('test')
 pool.disconnect() //断开连接池的所有连接。
Copy after login

In addition, you can download the RedisDesktopManager visual UI tool to connect to redis for management.

The above is the detailed content of About redis database quantity configuration, switching and specifying database. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template