實際工作經歷中,免不了有時候需要連接數據庫進行問題排查分析的場景,之前一直習慣通過 mysql -uxxx -hxxxx -P1234 ...
這樣的方式來啓動命令行形式的 MySQL 數據庫客戶端程序,只是用起來比較麻煩,每次都要拷貝各個配置參數,還要記得不要在命令裏顯式打印密碼。後來想起來在開發 Ruby on Rails 程序的時候,其提供了 rails dbconsole
的命令,可以方便直接啓動對應的數據庫客戶端命令行程序,聯想到 Django 理論上也有,所以找到了 python manage.py dbshell
這個命令,使用效果和自己手動敲 mysql 命令行是一樣的,省去繁瑣的參數設定步驟。
使用效果
用法
其用法可以直接查詢命令行幫助文檔:
# python manage.py dbshell -h
Usage: manage.py dbshell [options]
Runs the command-line client for specified database, or the default database if none is provided.
Options:
-v VERBOSITY, --verbosity=VERBOSITY
Verbosity level; 0=minimal output, 1=normal output,
2=verbose output, 3=very verbose output
--settings=SETTINGS The Python path to a settings module, e.g.
"myproject.settings.main". If this isn't provided, the
DJANGO_SETTINGS_MODULE environment variable will be
used.
--pythonpath=PYTHONPATH
A directory to add to the Python path, e.g.
"/home/djangoprojects/myproject".
--traceback Raise on exception
--database=DATABASE Nominates a database onto which to open a shell.
Defaults to the "default" database.
--version show program's version number and exit
-h, --help show this help message and exit