docker run -d -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=sunk -p 3307:3306 --name test-mysql --restart always -v /home/ct/mysql1/data:/var/lib/mysql mysql:8.0.12 --default-authentication-plugin=mysql_native_passwor
注意:python
1> 這裏的「數據庫名「須要和下一步setting中的數據庫名一致!!mysql
2> 咱們的MySQL數據庫對外部應用程序(非 docker 容器)開放的端口號爲: 3307sql
在項目下的 setting.pydocker
# Database # https://docs.djangoproject.com/en/1.11/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'sunk', 'USER':'root', 'PASSWORD':'123456', 'HOST':'127.0.0.1', 'PORT':'3307', } }
2.1 在 python2 中,使用 mysql-python 進行安裝鏈接MySQL的庫數據庫
pip install mysql-python
若是你們使用的是python2, 咱們的任務到此爲止啦!! django
若是是python3,請繼續看下文session
2.2 在 python3 中,使用鏈接庫爲 pymysql app
pip install pymysql
import pymysql pymysql.install_as_MySQLdb()
以下圖所示:ide
python manager.py runserver
輸出以下:ui
Performing system checks... System check identified no issues (0 silenced). You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions. Run 'python manage.py migrate' to apply them. August 21, 2018 - 13:06:03 Django version 1.11.15, using settings 'DjangoProject.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
Done,到此爲止 Django 啓動正常,而且鏈接到MySQL數據庫 ~~
https://blog.csdn.net/u014360817/article/details/55504331
https://blog.csdn.net/lhyzyp/article/details/70550683