下面由Redis教程欄目給大家介紹redis數(shù)據(jù)庫數(shù)量配置、切換及指定數(shù)據(jù)庫,希望對需要的朋友有所幫助!
redis的數(shù)據(jù)庫個數(shù)是可以配置的,默認為16個,見redis.windows.conf/redis.conf的databases 16。
對應(yīng)數(shù)據(jù)庫的索引值為0 – (databases -1),即16個數(shù)據(jù)庫,索引值為0-15。默認存儲的數(shù)據(jù)庫為0。
1、命令行切換
redis-cli -a 123456
登陸redis,默認選擇了數(shù)據(jù)庫0,如果需要切換到其它數(shù)據(jù)庫使用select 索引值,如select 1表示切換到索引值為1的數(shù)據(jù)庫。
D:softwareredis>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,默認為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ù)據(jù)庫則默認就用0號數(shù)據(jù)庫,配置該值則會使用自己配置的數(shù)據(jù)庫。
3、python指定redis數(shù)據(jù)庫
通過db參數(shù)設(shè)置使用的數(shù)據(jù)庫。如db=1表示使用索引值為1的數(shù)據(jù)庫。
redis-py提供兩個類Redis和StrictRedis用于實現(xiàn)Redis的命令,StrictRedis用于實現(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)建連接時使用SSLConnection。
連接池連接:
pool = redis.ConnectionPool(host='127.0.0.1', port=6379, password='123456', db=2) r = redis.Redis(connection_pool=pool)
備注:
使用以上方法初始化連接池無法通過ssl參數(shù)啟用ssl連接:
class ConnectionPool(object): def __init__(self, connection_class=Connection, max_connections=None, **connection_kwargs):
此處連接用了Connection。
如果需要使用ssl連接,則初始化連接池時使用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進行管理