简体   繁体   中英

django.db.utils.OperationalError: (2013, "Lost connection to MySQL server at 'handshake: reading inital communication packet', system error: 0")

I am developing an app in django to push it on Heroku, and while trying to migrate a database in mysql to heroku, I pip installed mysql-python and as I try to run the server, I got this error:

django.db.utils.OperationalError: (2013, "Lost connection to MySQL server at 'handshake: reading inital communication packet', system error: 0")

(I had my virtual environment on).
The strange thing is that the package installed python even if I had that already installed.
I found out that it unistalled django and several modules I previously installed, even pip!

I re-installed the modules but now as I run the server I get that error above.
If I run the server from another app directory, the database runs fine.
Is it maybe related to something I had installed in my directory and maybe was apparently overwritten by the strange package?

here is my pip freeze result:

certifi==2019.6.16
dj-database-url==0.5.0
Django==2.2.3
django-heroku==0.3.1
gunicorn==19.9.0
mysql-connector-python==8.0.16
mysqlclient==1.4.2.post1
protobuf==3.6.0
psycopg2==2.8.3
PyMySQL==0.9.3
python-decouple==3.1
pytz==2019.1
six==1.12.0
sqlparse==0.3.0
whitenoise==4.1.2
wincertstore==0.2

I will now delete the project and try to rebuild it anew, as long as I understand, what screw up the project was my attempt to install mysql-python.

Collecting mysql-python   Using cached https://files.pythonhosted.org/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip Building wheels for collected packages: mysql-python   Building wheel for mysql-python (setup.py) ... error   ERROR: Complete output from command 'C:\Applicazioni_Tommaso\Anaconda3\envs\met3\python.exe' -u -c 'import setuptools, tokenize;__file__='"'"'C:\\Users\\Tommaso\\AppData\\Local\\Temp\\pip-install-yr8h0ec1\\mysql-python\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d 'C:\Users\Tommaso\AppData\Local\Temp\pip-wheel-ywwxryy8' --python-tag cp37:   ERROR: running bdist_wheel   running build   running build_py  creating build   creating build\lib.win-amd64-3.7   copying
_mysql_exceptions.py -> build\lib.win-amd64-3.7   creating build\lib.win-amd64-3.7\MySQLdb   copying MySQLdb\__init__.py -> build\lib.win-amd64-3.7\MySQLdb   copying MySQLdb\converters.py -> build\lib.win-amd64-3.7\MySQLdb   copying MySQLdb\connections.py -> build\lib.win-amd64-3.7\MySQLdb   copying MySQLdb\cursors.py -> build\lib.win-amd64-3.7\MySQLdb   copying MySQLdb\release.py -> build\lib.win-amd64-3.7\MySQLdb   copying MySQLdb\times.py -> build\lib.win-amd64-3.7\MySQLdb   creating build\lib.win-amd64-3.7\MySQLdb\constants   copying MySQLdb\constants\__init__.py -> build\lib.win-amd64-3.7\MySQLdb\constants   copying MySQLdb\constants\CR.py -> build\lib.win-amd64-3.7\MySQLdb\constants   copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win-amd64-3.7\MySQLdb\constants   copying MySQLdb\constants\ER.py -> build\lib.win-amd64-3.7\MySQLdb\constants   copying MySQLdb\constants\FLAG.py -> build\lib.win-amd64-3.7\MySQLdb\constants   copying MySQLdb\constants\REFRESH.py -> build\lib.win-amd64-3.7\MySQLdb\constants   copying MySQLdb\constants\CLIENT.py -> build\lib.win-amd64-3.7\MySQLdb\constants   running build_ext   building '_mysql' extension   creating build\temp.win-amd64-3.7   creating build\temp.win-amd64-3.7\Release   C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.16.27023\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -Dversion_info=(1,2,5,'final',1)
-D__version__=1.2.5 "-IC:\Program Files (x86)\MySQL\MySQL Connector C 6.0.2\include" -IC:\Applicazioni_Tommaso\Anaconda3\envs\met3\include -IC:\Applicazioni_Tommaso\Anaconda3\envs\met3\include "-IC:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.16.27023\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\8.1\include\\shared" "-IC:\Program Files (x86)\Windows Kits\8.1\include\\um" "-IC:\Program Files (x86)\Windows Kits\8.1\include\\winrt" "-IC:\Program Files (x86)\Windows Kits\8.1\include\\cppwinrt" /Tc_mysql.c /Fobuild\temp.win-amd64-3.7\Release\_mysql.obj /Zl   _mysql.c  
_mysql.c(42): fatal error C1083: Non è possibile aprire il file inclusione: 'config-win.h': No such file or directory   error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\BuildTools\\VC\\Tools\\MSVC\\14.16.27023\\bin\\HostX86\\x64\\cl.exe' failed with exit status 2   ----------------------------------------   ERROR: Failed building wheel for mysql-python   Running setup.py clean for mysql-python Failed to build mysql-python Installing collected packages: mysql-python   Running setup.py install for mysql-python ... error
    ERROR: Complete output from command 'C:\Applicazioni_Tommaso\Anaconda3\envs\met3\python.exe' -u -c 'import setuptools, tokenize;__file__='"'"'C:\\Users\\Tommaso\\AppData\\Local\\Temp\\pip-install-yr8h0ec1\\mysql-python\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\Tommaso\AppData\Local\Temp\pip-record-dwqliwhp\install-record.txt'
--single-version-externally-managed --compile:
    ERROR: running install
    running build
    running build_py
    creating build
    creating build\lib.win-amd64-3.7
    copying _mysql_exceptions.py -> build\lib.win-amd64-3.7
    creating build\lib.win-amd64-3.7\MySQLdb
    copying MySQLdb\__init__.py -> build\lib.win-amd64-3.7\MySQLdb
    copying MySQLdb\converters.py -> build\lib.win-amd64-3.7\MySQLdb
    copying MySQLdb\connections.py -> build\lib.win-amd64-3.7\MySQLdb
    copying MySQLdb\cursors.py -> build\lib.win-amd64-3.7\MySQLdb
    copying MySQLdb\release.py -> build\lib.win-amd64-3.7\MySQLdb
    copying MySQLdb\times.py -> build\lib.win-amd64-3.7\MySQLdb
    creating build\lib.win-amd64-3.7\MySQLdb\constants
    copying MySQLdb\constants\__init__.py -> build\lib.win-amd64-3.7\MySQLdb\constants
    copying MySQLdb\constants\CR.py -> build\lib.win-amd64-3.7\MySQLdb\constants
    copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win-amd64-3.7\MySQLdb\constants
    copying MySQLdb\constants\ER.py -> build\lib.win-amd64-3.7\MySQLdb\constants
    copying MySQLdb\constants\FLAG.py -> build\lib.win-amd64-3.7\MySQLdb\constants
    copying MySQLdb\constants\REFRESH.py -> build\lib.win-amd64-3.7\MySQLdb\constants
    copying MySQLdb\constants\CLIENT.py -> build\lib.win-amd64-3.7\MySQLdb\constants
    running build_ext
    building '_mysql' extension
    creating build\temp.win-amd64-3.7
    creating build\temp.win-amd64-3.7\Release
    C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.16.27023\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -Dversion_info=(1,2,5,'final',1)
-D__version__=1.2.5 "-IC:\Program Files (x86)\MySQL\MySQL Connector C 6.0.2\include" -IC:\Applicazioni_Tommaso\Anaconda3\envs\met3\include -IC:\Applicazioni_Tommaso\Anaconda3\envs\met3\include "-IC:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.16.27023\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\8.1\include\\shared" "-IC:\Program Files (x86)\Windows Kits\8.1\include\\um" "-IC:\Program Files (x86)\Windows Kits\8.1\include\\winrt" "-IC:\Program Files (x86)\Windows Kits\8.1\include\\cppwinrt" /Tc_mysql.c /Fobuild\temp.win-amd64-3.7\Release\_mysql.obj /Zl
    _mysql.c
    _mysql.c(42): fatal error C1083: Non è possibile aprire il file inclusione: 'config-win.h': No such file or directory
    error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\BuildTools\\VC\\Tools\\MSVC\\14.16.27023\\bin\\HostX86\\x64\\cl.exe' failed with exit status 2
    ---------------------------------------- ERROR: Command "'C:\Applicazioni_Tommaso\Anaconda3\envs\met3\python.exe' -u -c 'import setuptools, tokenize;__file__='"'"'C:\\Users\\Tommaso\\AppData\\Local\\Temp\\pip-install-yr8h0ec1\\mysql-python\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\Tommaso\AppData\Local\Temp\pip-record-dwqliwhp\install-record.txt'
--single-version-externally-managed --compile" failed with error code 1 in C:\Users\Tommaso\AppData\Local\Temp\pip-install-yr8h0ec1\mysql-python\

UPDATE

The bad command, that is,

pip install mysql-python

had set my python default path to 2.7 instead of 3.7.

To fix it, I had to (Windows 10):

Cortana > search bar > type "env" > change environment variables for the system > environment variables > select "Path" > change >

create and move to the top (or simply move to the top) the path in which you have installed python 3.7.

Save > close anaconda. This made pyhton and its modules work again. I had to reinstall pip, now it works again. But when my virtual environment is on, I have problems with my connected database in mysql.

It might be a Firewall issue. Make sure the port you are running MySQL is open for inbound traffic from the IP where your App is running.

Update:

It turns out that trying to

pip install mysql-python

was the thing that completely screwed up my project I had all the installed modules removed from my computer: pip, conda, python, django...

Update: The bad command, that is,

pip install mysql-python

had set my python default path to 2.7 instead of 3.7.

To fix it, I had to (Windows 10):

Cortana > search bar > type "env" > change environment variables for the system > environment variables > select "Path" > change >

create and move to the top (or simply move to the top) the path in which you have installed python 3.7.

Save > close anaconda. This made pyhton and its modules work again. I had to reinstall pip, now it works again. But when my virtual environment is on, I have problems with my connected database in mysql.

Solved

After fixing thing as I decribed in the previous answer , I found out that Heroku has some problems to manage mysql database, even if connected with the add-on ClearDB. So I rebuilt the database in Postgresql, thus not needing the mysql python modules anymore. This made me not stumble anymore in the previous conflicts between Django and Heroku.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM