首頁  >  文章  >  資料庫  >  關於redis資料庫數量配置、切換及指定資料庫

關於redis資料庫數量配置、切換及指定資料庫

藏色散人
藏色散人轉載
2020-10-10 15:03:012911瀏覽

以下由Redis教學欄位介紹redis資料庫數量配置、切換及指定資料庫,希望對需要的朋友有幫助!

關於redis資料庫數量配置、切換及指定資料庫

redis的資料庫個數是可以設定的,預設為16個,見redis.windows.conf/redis.conf的databases 16。
對應資料庫的索引值為0 - (databases -1),即16個資料庫,索引值為0-15。預設儲存的資料庫為0。

1、命令列切換
redis-cli -a 123456
登陸redis,預設選擇了資料庫0,如果需要切換到其它資料庫使用select 索引值,如select 1表示切換到索引值為1的資料庫。

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

切換之後就會一直在操作的是新資料庫,直到下次切換生效。

2、springboot指定redis資料庫

#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

在原始碼RedisProperties.java中,database的初始值為0的(private int database = 0;),因此在springboot配置redis時不指定資料庫則預設就用0號資料庫,配置該值則會使用自己配置的資料庫。

3、python指定redis資料庫

透過db參數設定使用的資料庫。如db=1表示使用索引值為1的資料庫。

redis-py提供兩個類別Redis和StrictRedis用於實現Redis的命令,StrictRedis用於實現大部分官方的命令,並使用官方的語法和命令(比如,SET命令對應與StrictRedis.set方法)。
Redis是StrictRedis的子類別,用於向後相容舊版本的redis-py。簡單說,官方推薦使用StrictRedis方法。

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)

備註:
redis如果開啟了ssl連線方式,則增加ssl=True表示啟用ssl連線。
如 redis.StrictRedis(host='127.0.0.1', port=6380, password='123456', db=2, ssl=True)。則在建立連線時使用SSLConnection。

連線池連線:

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

備註:
使用上述方法初始化連線池無法透過ssl參數啟用ssl連線:

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

此處連線用了Connection。

如果需要使用ssl連接,則初始化連接池時使用from_url方法初始化連接池,參數格式如:

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() //断开连接池的所有连接。

另外,可下載RedisDesktopManager 視覺化UI工具連接redis進行管理

以上是關於redis資料庫數量配置、切換及指定資料庫的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:csdn.net。如有侵權,請聯絡admin@php.cn刪除