簡體   English   中英

將MySQL DB憑據存儲在一個文件中,然后在主腳本中調用該文件

[英]Storing MySQL DB credentials in one file then calling that file in the main script

我想創建一個certificate.py文件,該文件將與保存我的MySQL DB連接憑據的主python腳本存儲在同一目錄中。

credentials.py

host = '11.11.81.7011'
user = 'dsufghsd'
password = 'tfjb2379gfweiu'
db = 'fwenf249ftgwieufgefw7f'

然后在連接到MySQL DB時

import credentials
con = pymysql.connect(host = credentials.host,
                      user = credentials.user,
                      password = credentials.password,
                      db = credentials.db,
                      charset = 'utf8mb4', 
                      cursorclass = pymysql.cursors.SSDictCursor)

但是我不斷收到一個導入錯誤,說沒有名為“憑據”的模塊

任何幫助將不勝感激

使用任何文本編輯器將變量放入~/.bash_profile

export SQL_HOST='11.11.81.7011'
export SQL_USER='dsufghsd'
export SQL_PWD='tfjb2379gfweiu'
export SQL_DB='fwenf249ftgwieufgefw7f'

不要忘了做source ~/.bash_profile

然后你這樣叫他們

import os

conf = {'host': os.environ['SQL_HOST'],
        'user': os.environ['SQL_USER'],
        'password': os.environ['SQL_PWD'],
        'db': os.environ['SQL_DB']}

conf.update({'charset': 'utf8mb4',
             'cursorclass': pymysql.cursors.SSDictCursor})

conn = pymysql.connect(**conf)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM