溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

redis數(shù)據(jù)庫數(shù)量配置、切換及指定數(shù)據(jù)庫的方法

發(fā)布時(shí)間:2020-10-12 14:27:55 來源:億速云 閱讀:760 作者:小新 欄目:關(guān)系型數(shù)據(jù)庫

這篇文章主要介紹了redis數(shù)據(jù)庫數(shù)量配置、切換及指定數(shù)據(jù)庫的方法,具有一定借鑒價(jià)值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。

redis的數(shù)據(jù)庫個數(shù)是可以配置的,默認(rèn)為16個,見redis.windows.conf/redis.conf的databases 16。
對應(yīng)數(shù)據(jù)庫的索引值為0 - (databases -1),即16個數(shù)據(jù)庫,索引值為0-15。默認(rèn)存儲的數(shù)據(jù)庫為0。

1、命令行切換
redis-cli -a 123456
登陸redis,默認(rèn)選擇了數(shù)據(jù)庫0,如果需要切換到其它數(shù)據(jù)庫使用select 索引值,如select 1表示切換到索引值為1的數(shù)據(jù)庫。

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

切換之后就會一直在操作的是新數(shù)據(jù)庫,直到下次切換生效。

2、springboot指定redis數(shù)據(jù)庫

#redis
 spring.redis.host=localhost
 spring.redis.password=123456
 spring.redis.port=6380  //redis ssl端口
 spring.redis.database=2 //使用的數(shù)據(jù)庫索引
 spring.redis.ssl=true  //是否使用ssl,默認(rèn)為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時(shí)不指定數(shù)據(jù)庫則默認(rèn)就用0號數(shù)據(jù)庫,配置該值則會使用自己配置的數(shù)據(jù)庫。

3、python指定redis數(shù)據(jù)庫

通過db參數(shù)設(shè)置使用的數(shù)據(jù)庫。如db=1表示使用索引值為1的數(shù)據(jù)庫。

redis-py提供兩個類Redis和StrictRedis用于實(shí)現(xiàn)Redis的命令,StrictRedis用于實(shí)現(xiàn)大部分官方的命令,并使用官方的語法和命令(比如,SET命令對應(yīng)與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)。則在創(chuàng)建連接時(shí)使用SSLConnection。

連接池連接:

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

備注:
使用以上方法初始化連接池?zé)o法通過ssl參數(shù)啟用ssl連接:

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

此處連接用了Connection。

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

rediss://[:password]@localhost:6379/0 ,6379表示端口,0表示使用的數(shù)據(jù)庫索引值。
 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進(jìn)行管理

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享redis數(shù)據(jù)庫數(shù)量配置、切換及指定數(shù)據(jù)庫的方法內(nèi)容對大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,遇到問題就找億速云,詳細(xì)的解決方法等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI