小白都能秒懂的各數據庫在Django的配置

mysql在django的配置python

有沒有pymysql,沒有的話你還想連數據庫,最難的一步,下載pymysql mysql

直接輸入指令:pip install pymysqlredis

第一步:在settings.py中設置mysql配置sql

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'crm',
        'HOST': '127.0.0.1',
        'PORT': 3306,
        'USER': 'root',
        'PASSWORD': '321',
    }
}

第二步:mongodb

在settings.py 同級的init.py 中寫:數據庫

import pymysql
pymysql.install_as_MySQLdb()

 ok,mysql能夠在django中使用了django

Redis在django的配置,做爲一個做用於緩存的非關係型數據庫,redis咱們必須使用的很是6緩存

下面進行操做:code

首先先安裝redis:pip install redisblog

下面有三種建立redis鏈接的方式,根據應用選擇對應方案:

第一種:定義redis鏈接池,直接引用

import redis
POOL=redis.ConnectionPool(host='127.0.0.1',port=6379,decode_responses=True)#定義鏈接池
import redis
REDIS_CONN = redis.Redis(connection_pool=POOL)

all_keys = REDIS_CONN.scan_iter(shopping_car_key)

第二種:直接建立

from redis import Redis

RedisDB = Redis(host='127.0.0.1', port=6379, db=15)

第三種:redis做爲Django緩存使用

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
            "CONNECTION_POOL_KWARGS": {"max_connections": 100}
            # "PASSWORD": "密碼",
        }
    }
}

 

下面是mongodb了

首先安裝pymongo:pip install pymong

而後就暴力一點:

import pymongo

client = pymongo.MongoClient(host='127.0.0.1', port=27017)
MongoDB = client["autospeech"]
相關文章
相關標籤/搜索