在学习Django数据库配置时,修改配置文件后遇到报错:
修改了Django项目的文件settings.py:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'TestModel', # 添加此项
)
执行:
python3 manage.py runserver 0.0.0.0:8000
报错:
ModuleNotFoundError: No module named 'TestModel'
文件位置错误:
TestModel的位置应该与第二个同级,我把它放到了第二个TestModel中!

-->这一步可能与此问题无关
参考资料:https://blog.csdn.net/mzbqhbc12/article/details/55505959
安装PyMySQL:
pip3 install PyMySQL
Collecting PyMySQL
Downloading https://files.pythonhosted.org/packages/a7/7d/682c4a7da195a678047c8f1c51bb7682aaedee1dca7547883c3993ca9282/PyMySQL-0.9.2-py2.py3-none-any.whl (47kB)
100% |████████████████████████████████| 51kB 9.3kB/s
Collecting cryptography (from PyMySQL)
Downloading https://files.pythonhosted.org/packages/40/87/acdcf84ce6d25a7db1c113f4b9b614fd8d707b7ab56fbf17cf18cd26a627/cryptography-2.2.2-cp34-abi3-macosx_10_6_intel.whl (1.5MB)
100% |████████████████████████████████| 1.5MB 64kB/s
Collecting asn1crypto>=0.21.0 (from cryptography->PyMySQL)
Downloading https://files.pythonhosted.org/packages/ea/cd/35485615f45f30a510576f1a56d1e0a7ad7bd8ab5ed7cdc600ef7cd06222/asn1crypto-0.24.0-py2.py3-none-any.whl (101kB)
100% |████████████████████████████████| 102kB 55kB/s
Collecting six>=1.4.1 (from cryptography->PyMySQL)
Downloading https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl
Collecting idna>=2.1 (from cryptography->PyMySQL)
Downloading https://files.pythonhosted.org/packages/4b/2a/0276479a4b3caeb8a8c1af2f8e4355746a97fab05a372e4a2c6a6b876165/idna-2.7-py2.py3-none-any.whl (58kB)
100% |████████████████████████████████| 61kB 86kB/s
Collecting cffi>=1.7; platform_python_implementation != "PyPy" (from cryptography->PyMySQL)
Downloading https://files.pythonhosted.org/packages/0b/ba/32835c9965d8a0090723e1d0b47373365525c4bd08c807b5efdc9fecbc99/cffi-1.11.5-cp37-cp37m-macosx_10_9_x86_64.whl (163kB)
100% |████████████████████████████████| 163kB 41kB/s
Collecting pycparser (from cffi>=1.7; platform_python_implementation != "PyPy"->cryptography->PyMySQL)
Downloading https://files.pythonhosted.org/packages/8c/2d/aad7f16146f4197a11f8e91fb81df177adcc2073d36a17b1491fd09df6ed/pycparser-2.18.tar.gz (245kB)
100% |████████████████████████████████| 256kB 19kB/s
Installing collected packages: asn1crypto, six, idna, pycparser, cffi, cryptography, PyMySQL
Running setup.py install for pycparser ... done
Successfully installed PyMySQL-0.9.2 asn1crypto-0.24.0 cffi-1.11.5 cryptography-2.2.2 idna-2.7 pycparser-2.18 six-1.11.0
-->end 这一步可能与此问题无关
python3 manage.py runserver
依然报错:
django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")
mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
在系统偏好设置里,开启mysql服务:


mysql
ERROR 1045 (28000): Access denied for user 'ypn-mac-02'@'localhost' (using password: NO)
mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 98
Server version: 5.7.17 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> exit
python3 manage.py migrate
django.db.utils.OperationalError: (1045, "Access denied for user 'test'@'localhost' (using password: YES)")
在Django项目中修改settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # 或者使用 mysql.connector.django
'NAME': 'test',
'USER': 'root',
'PASSWORD': '123qwe',
'HOST':'localhost',
'PORT':'3306',
}
}
python3 manage.py runserver
数据库报错:
django.db.utils.OperationalError: (1049, "Unknown database 'test'")
参考资料:https://blog.csdn.net/wsk312138147/article/details/79889460
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dedecms |
| my_db |
| mysql |
| performance_schema |
| sys |
| uckefu |
+--------------------+
7 rows in set (0.14 sec)
mysql> create database test character set utf8;
Query OK, 1 row affected (0.06 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dedecms |
| my_db |
| mysql |
| performance_schema |
| sys |
| test |
| uckefu |
+--------------------+
8 rows in set (0.00 sec)
mysql> exit
python3 manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 14 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.
July 06, 2018 - 08:40:13
Django version 2.0.7, using settings 'HelloWorld.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
参考资料:
https://blog.csdn.net/iamoldpan/article/details/78755515
https://blog.csdn.net/pipisorry/article/details/45727309

网友评论