Django AWS Elastic Beanstalk 错误配置不当:加载 MySQLdb 模块时出错:没有名为 MySQLdb 的模块
Posted
技术标签:
【中文标题】Django AWS Elastic Beanstalk 错误配置不当:加载 MySQLdb 模块时出错:没有名为 MySQLdb 的模块【英文标题】:Django AWS Elastic Beanstalk error ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb 【发布时间】:2020-06-08 11:27:38 【问题描述】:我知道很多人都遇到过这个错误,我尝试了不同的解决方案,但都没有奏效。
我正在使用 aws eb cli。
我正在使用以下命令 eb deploy
将我的应用程序部署到服务器。
以下是我的 Django 的配置。
在.ebextensions
目录下,我有以下2个文件:
1:01_packages.config
packages:
yum:
git: []
python27-devel: []
mysql: []
mysql-devel: []
另一个文件是
2:02_django.conf
option_settings:
"aws:elasticbeanstalk:application:environment":
DJANGO_SETTINGS_MODULE: "settings.development"
"PYTHONPATH": "/opt/python/current/app/src:$PYTHONPATH"
"aws:elasticbeanstalk:container:python":
WSGIPath: src/wsgi.py
NumProcesses: 3
NumThreads: 20
"aws:elasticbeanstalk:container:python:staticfiles":
"/static/": "static/"
以下是我在本地虚拟环境中pip freeze
之后的 requirements.txt 文件。
requirements.txt
asn1crypto==0.24.0
awsebcli==3.17.1
backports.ssl-match-hostname==3.5.0.1
botocore==1.14.17
cement==2.8.2
cent==2.1.0
centrifuge==0.8.4
certifi==2017.11.5
cffi==1.11.2
chardet==3.0.4
colorama==0.3.9
cryptography==2.1.4
Django==1.8.18
django-colorfield==0.1.14
django-countries==5.0
django-debug-toolbar==1.9.1
django-environ==0.4.4
django-multiselectfield==0.1.8
django-simple-history==1.9.1
django-sslserver==0.20
docutils==0.15.2
enum34==1.1.6
future==0.16.0
google-api-python-client==1.6.4
hiredis==0.2.0
html5lib==1.0b8
httplib2==0.10.3
icalendar==4.0.0
idna==2.6
ipaddress==1.0.18
jmespath==0.9.4
jsonschema==2.4.0
mysqlclient==1.4.6
oauth2client==2.0.0
oauthclient==1.0.3
olefile==0.44
pathspec==0.5.9
paypalrestsdk==1.13.1
pdfcrowd==4.0.1
phonenumbers==8.8.6
Pillow==4.3.0
pyasn1==0.3.7
pyasn1-modules==0.1.5
pycparser==2.18
PyJWT==1.5.3
pyOpenSSL==17.5.0
PyPDF2==1.26.0
pypiwin32==219
pytesseract==0.1.7
python-dateutil==2.6.1
pytz==2017.3
PyYAML==5.2
reportlab==3.4.0
requests==2.18.4
rsa==3.4.2
semantic-version==2.5.0
six==1.11.0
sockjs-tornado==1.0.1
sqlparse==0.2.4
termcolor==1.1.0
toredis-fork==0.1.4
tornado==4.2.1
toro==0.8
twilio==6.9.1
uritemplate==3.0.0
urllib3==1.22
wcwidth==0.1.8
webencodings==0.5.1
xhtml2pdf==0.2.2
我把它保存在我的根目录中。
当我运行eb deploy
成功部署。
但是当我将浏览器运行到我的网址时。
我得到了这个Internal Server Error
页面。
所以我尝试查看/var/log/httpd/error_log
下服务器上的日志文件
我收到以下错误:
[Mon Feb 24 17:56:57.227427 2020] [:error] [pid 8054] [remote 101.50.93.65:188] mod_wsgi (pid=8054): Target WSGI script '/opt/python/current/app/src/wsgi.py' cannot be loaded as Python module.
[Mon Feb 24 17:56:57.227450 2020] [:error] [pid 8054] [remote 101.50.93.65:188] mod_wsgi (pid=8054): Exception occurred processing WSGI script '/opt/python/current/app/src/wsgi.py'.
[Mon Feb 24 17:56:57.227466 2020] [:error] [pid 8054] [remote 101.50.93.65:188] Traceback (most recent call last):
[Mon Feb 24 17:56:57.227483 2020] [:error] [pid 8054] [remote 101.50.93.65:188] File "/opt/python/current/app/src/wsgi.py", line 17, in <module>
[Mon Feb 24 17:56:57.227585 2020] [:error] [pid 8054] [remote 101.50.93.65:188] application = get_wsgi_application()
[Mon Feb 24 17:56:57.227599 2020] [:error] [pid 8054] [remote 101.50.93.65:188] File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Mon Feb 24 17:56:57.227627 2020] [:error] [pid 8054] [remote 101.50.93.65:188] django.setup()
[Mon Feb 24 17:56:57.227634 2020] [:error] [pid 8054] [remote 101.50.93.65:188] File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
[Mon Feb 24 17:56:57.227645 2020] [:error] [pid 8054] [remote 101.50.93.65:188] apps.populate(settings.INSTALLED_APPS)
[Mon Feb 24 17:56:57.227651 2020] [:error] [pid 8054] [remote 101.50.93.65:188] File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/apps/registry.py", line 78, in populate
[Mon Feb 24 17:56:57.227662 2020] [:error] [pid 8054] [remote 101.50.93.65:188] raise RuntimeError("populate() isn't reentrant")
[Mon Feb 24 17:56:57.227676 2020] [:error] [pid 8054] [remote 101.50.93.65:188] RuntimeError: populate() isn't reentrant
所以我一直在谷歌搜索,发现一个帖子建议更改 wsgi 文件,我这样做了,然后这个错误更新为以下代码行。
[Mon Feb 24 18:23:12.169850 2020] [mpm_prefork:notice] [pid 9081] AH00169: caught SIGTERM, shutting down
[Mon Feb 24 18:23:13.289449 2020] [suexec:notice] [pid 10286] AH01232: suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Mon Feb 24 18:23:13.305290 2020] [so:warn] [pid 10286] AH01574: module wsgi_module is already loaded, skipping
[Mon Feb 24 18:23:13.307373 2020] [http2:warn] [pid 10286] AH10034: The mpm module (prefork.c) is not supported by mod_http2. The mpm determines how things are processed in your server. HTTP/2 has more demands in this regard and the currently selected mpm will just not do. This is an advisory warning. Your server will continue to work, but the HTTP/2 protocol will be inactive.
[Mon Feb 24 18:23:13.307384 2020] [http2:warn] [pid 10286] AH02951: mod_ssl does not seem to be enabled
[Mon Feb 24 18:23:13.307990 2020] [lbmethod_heartbeat:notice] [pid 10286] AH02282: No slotmem from mod_heartmonitor
[Mon Feb 24 18:23:13.308050 2020] [:warn] [pid 10286] mod_wsgi: Compiled for Python/2.7.13.
[Mon Feb 24 18:23:13.308057 2020] [:warn] [pid 10286] mod_wsgi: Runtime using Python/2.7.16.
[Mon Feb 24 18:23:13.311200 2020] [mpm_prefork:notice] [pid 10286] AH00163: Apache/2.4.41 (Amazon) mod_wsgi/3.5 Python/2.7.16 configured -- resuming normal operations
[Mon Feb 24 18:23:13.311217 2020] [core:notice] [pid 10286] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
[Mon Feb 24 18:23:16.367182 2020] [:error] [pid 10293] [remote 127.0.0.1:0] mod_wsgi (pid=10293): Exception occurred processing WSGI script '/opt/python/current/app/src/wsgi.py'.
[Mon Feb 24 18:23:16.367240 2020] [:error] [pid 10293] [remote 127.0.0.1:0] RuntimeError: response has not been started
[Mon Feb 24 18:23:17.744228 2020] [:error] [pid 10291] [remote 127.0.0.1:0] mod_wsgi (pid=10291): Exception occurred processing WSGI script '/opt/python/current/app/src/wsgi.py'.
[Mon Feb 24 18:23:17.744288 2020] [:error] [pid 10291] [remote 127.0.0.1:0] RuntimeError: response has not been started
[Mon Feb 24 18:23:19.116825 2020] [:error] [pid 10292] [remote 127.0.0.1:0] mod_wsgi (pid=10292): Exception occurred processing WSGI script '/opt/python/current/app/src/wsgi.py'.
[Mon Feb 24 18:23:19.116892 2020] [:error] [pid 10292] [remote 127.0.0.1:0] RuntimeError: response has not been started
[Mon Feb 24 18:23:20.493432 2020] [:error] [pid 10418] [remote 127.0.0.1:0] mod_wsgi (pid=10418): Exception occurred processing WSGI script '/opt/python/current/app/src/wsgi.py'.
[Mon Feb 24 18:23:20.493489 2020] [:error] [pid 10418] [remote 127.0.0.1:0] RuntimeError: response has not been started
[Mon Feb 24 18:36:44.987693 2020] [:error] [pid 10443] [remote 95.105.12.68:0] mod_wsgi (pid=10443): Exception occurred processing WSGI script '/opt/python/current/app/src/wsgi.py'.
[Mon Feb 24 18:36:44.987766 2020] [:error] [pid 10443] [remote 95.105.12.68:0] RuntimeError: response has not been started
[Mon Feb 24 18:55:28.298121 2020] [:error] [pid 10468] [remote 101.50.93.65:0] mod_wsgi (pid=10468): Exception occurred processing WSGI script '/opt/python/current/app/src/wsgi.py'.
[Mon Feb 24 18:55:28.298180 2020] [:error] [pid 10468] [remote 101.50.93.65:0] RuntimeError: response has not been started
[Mon Feb 24 18:55:30.126198 2020] [:error] [pid 10499] [remote 101.50.93.65:0] mod_wsgi (pid=10499): Exception occurred processing WSGI script '/opt/python/current/app/src/wsgi.py'.
[Mon Feb 24 18:55:30.126260 2020] [:error] [pid 10499] [remote 101.50.93.65:0] RuntimeError: response has not been started
[Mon Feb 24 18:55:31.671293 2020] [:error] [pid 10973] [remote 101.50.93.65:0] mod_wsgi (pid=10973): Exception occurred processing WSGI script '/opt/python/current/app/src/wsgi.py'.
[Mon Feb 24 18:55:31.671358 2020] [:error] [pid 10973] [remote 101.50.93.65:0] RuntimeError: response has not been started
[Mon Feb 24 18:55:32.858757 2020] [:error] [pid 11606] [remote 101.50.93.65:0] mod_wsgi (pid=11606): Exception occurred processing WSGI script '/opt/python/current/app/src/wsgi.py'.
[Mon Feb 24 18:55:32.858821 2020] [:error] [pid 11606] [remote 101.50.93.65:0] RuntimeError: response has not been started
这是新的 wsgi.py 文件。
import os
import logging
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings.development")
from django.core.wsgi import get_wsgi_application
# application = get_wsgi_application()
env_variables_to_pass = ['DJANGO_SETTINGS_MODULE', ]
def application(environ, start_response):
if environ['mod_wsgi.process_group'] != '':
import signal
os.kill(os.getpid(), signal.SIGINT)
return ["killed"]
下面是旧的 wsgi.py 文件
import os
import logging
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings.development")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
env_variables_to_pass = ['DJANGO_SETTINGS_MODULE', ]
然后我尝试使用 python manage.py runserver
在服务器上运行应用程序,因为没有其他方法可以解决此问题,我不确定问题出在哪里。
所以我做了 ssh,我确实使用了 EB 创建的服务器的虚拟环境。
在运行 python manage.py runserver 之后。
我收到以下错误。
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 27, in <module>
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
因此,我尝试遵循我可以在 google 上看到的任何建议。
我试过pip install mysqlclient
我收到此错误。
Collecting mysqlclient
Using cached https://files.pythonhosted.org/packages/d0/97/7326248ac8d5049968bf4ec708a5d3d4806e412a42e74160d7f266a3e03a/mysqlclient-1.4.6.tar.gz
Complete output from command python setup.py egg_info:
sh: mysql_config: command not found
sh: mariadb_config: command not found
sh: mysql_config: command not found
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-build-MxFUOd/mysqlclient/setup.py", line 16, in <module>
metadata, options = get_config()
File "setup_posix.py", line 61, in get_config
libs = mysql_config("libs")
File "setup_posix.py", line 29, in mysql_config
raise EnvironmentError("%s not found" % (_mysql_config_path,))
EnvironmentError: mysql_config not found
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-MxFUOd/mysqlclient/
我都试过了
pip install mysqlclient --no-cache-dir
和
pip install mysqlclient
它是 Centos 服务器,所以我不能执行 sudo apt-get 命令。
只有 yum 命令有效。
我用 sudo 尝试了以下命令
sudo pip install mysql-connector-python
但我认为这将安装在全局而不是环境中。
所以我尝试不使用sudo,它给出了权限错误。
我使用其他命令安装 mysql 时使用 sudo 而不是 sudo。
pip install pymysql
sudo yum install python-mysqldb
无论我做什么,我都会收到这个 MySQL 错误。
我不想移动到其他数据库,因为我也必须移动数据。
更新
根据@Arun K
的给定建议,我运行了以下命令
which mysql_config
我收到了以下回复。
/usr/bin/which: no mysql_config in (/opt/python/run/venv/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/.local/bin:/home/ec2-user/bin)
【问题讨论】:
您好。在这里写帖子时请尝试使用正确的大小写 - 这使它们更容易阅读。请记住,Stack Overflow 是一组有用的问答集,将来会被许多开发人员使用,因此我们希望它们尽可能具有可读性。 【参考方案1】:就我而言,在 64 位 Amazon Linux 2/3.0.1 上 我必须通过 .ebextensions
中的 *.config 安装以下内容packages:
yum:
python3-devel: []
mariadb-devel: []
gcc: []
【讨论】:
【参考方案2】:为什么不使用 docker 并将 Django 应用程序作为容器运行?弹性豆茎还支持 docker 类型。网上有很多文章详细介绍了这些步骤。你不必担心centos。
参考: Django on Docler
【讨论】:
【参考方案3】:尝试运行
sudo yum install mysql-devel gcc python-devel
然后
pip install mysqlclient
【讨论】:
yum install 命令说已经安装了。但是当我运行 pip install 时,问题中解释了同样的旧错误。Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-7UHBND/mysqlclient/
在为 pip 运行第二个命令时再次出现同样的错误。 :-(
我认为 mysql 的库已安装但 mysqlclient 缺少。尝试运行sudo yum whatprovides libmysqlclient*
。应该有一个结果提到像mariadb-libs-<SOME-VERSION_NO>
这样的包。尝试使用sudo yum install mariadb-libs-<SOME-VERSION_NO>
安装该软件包。不要忘记复制包的确切名称。然后尝试用pip安装mysqlclient。
@SizzlingCode 我启动了一个新的centos 服务器,安装了mariadb-devel gcc python-devel
并且能够使用pip 安装mysqlclient。在这一点上,我认为您的服务器中配置错误。尝试在新实例上复制您的生产流程,看看会发生什么。
是的,我的错,我重建了 ec2 环境,一切都开始工作了。我不确定是什么问题,可能是包损坏或配置错误。但重建后一切正常。谢谢。以上是关于Django AWS Elastic Beanstalk 错误配置不当:加载 MySQLdb 模块时出错:没有名为 MySQLdb 的模块的主要内容,如果未能解决你的问题,请参考以下文章
Django AWS Elastic Beanstalk 迁移数据库
AWS Elastic BeansTalk Django cronjob 发布请求返回 403 错误
在 AWS Elastic Beanstalk 上设置 Django:未找到 WSGIPath
如何设置运行 Django 的 AWS Elastic Beanstalk Docker 平台?