-1

I am trying to connect to mysql with python 3.6.9 and django 3.0.1 on linux lubuntu

mysql Ver 14.14 Distrib 5.7.28, for Linux (i686) using EditLine wrapper

Currently I'm getting this error message when I run the django server although I have tried to install a mysql client:

django.core.exceptions.ImproperlyConfigured: Error loading >MySQLdb module.
Did you install mysqlclient?
dpkg --get-selections | grep mysql

libmysqlclient20:i386           install
mysql-client-5.7                install
mysql-client-core-5.7           install
mysql-common                    install
mysql-server                    install
mysql-server-5.7                install
mysql-server-core-5.7           install
python-mysqldb                  install

MySQLdb is not it seems supported for python 3 according to this: https://pypi.org/project/MySQL-python/

Is mysql not supported for Django/python 3 yet? I am struggling to find relevant info. after much searching, I have seen many questions on stack overflow about similar issues but with earlier versions of all the software.

I have installed pymysql which successfully connects to mysql and retrieves data, but this doesn't seem to be useful for Django.

Edit: I tried pip3 install mysqlclient but got this output:

pip3 install mysqlclient
Collecting mysqlclient   Using cached https://files.pythonhosted.org/packages/d0/97/7326248ac8d5049968bf4ec708a5d3d4806e412a42e74160d7f266a3e03a/mysqlclient-1.4.6.tar.gz Building wheels for collected packages: mysqlclient   Running setup.py bdist_wheel for mysqlclient ... error   Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-2rl7qhom/mysqlclient/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 /tmp/tmpvr8f4qvqpip-wheel- --python-tag cp36:   running bdist_wheel   running build   running build_py   creating build   creating build/lib.linux-i686-3.6   creating build/lib.linux-i686-3.6/MySQLdb   copying MySQLdb/__init__.py -> build/lib.linux-i686-3.6/MySQLdb   copying MySQLdb/_exceptions.py -> build/lib.linux-i686-3.6/MySQLdb   copying MySQLdb/compat.py -> build/lib.linux-i686-3.6/MySQLdb   copying MySQLdb/connections.py -> build/lib.linux-i686-3.6/MySQLdb   copying MySQLdb/converters.py -> build/lib.linux-i686-3.6/MySQLdb   copying MySQLdb/cursors.py -> build/lib.linux-i686-3.6/MySQLdb   copying MySQLdb/release.py -> build/lib.linux-i686-3.6/MySQLdb   copying MySQLdb/times.py -> build/lib.linux-i686-3.6/MySQLdb   creating build/lib.linux-i686-3.6/MySQLdb/constants   copying MySQLdb/constants/__init__.py -> build/lib.linux-i686-3.6/MySQLdb/constants   copying MySQLdb/constants/CLIENT.py -> build/lib.linux-i686-3.6/MySQLdb/constants   copying MySQLdb/constants/CR.py -> build/lib.linux-i686-3.6/MySQLdb/constants  copying MySQLdb/constants/ER.py -> build/lib.linux-i686-3.6/MySQLdb/constants   copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-i686-3.6/MySQLdb/constants   copying MySQLdb/constants/FLAG.py -> build/lib.linux-i686-3.6/MySQLdb/constants   running build_ext   building 'MySQLdb._mysql' extension   creating build/temp.linux-i686-3.6   creating build/temp.linux-i686-3.6/MySQLdb i686-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -g
    -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -Dversion_info=(1,4,6,'final',0) -D__version__=1.4.6 -I/usr/include/mysql -I/usr/include/python3.6m -c MySQLdb/_mysql.c -o build/temp.linux-i686-3.6/MySQLdb/_mysql.o   i686-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions
    -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-i686-3.6/MySQLdb/_mysql.o -lmysqlclient -lpthread -lz
    -lm -lrt -latomic -lssl -lcrypto -ldl -o build/lib.linux-i686-3.6/MySQLdb/_mysql.cpython-36m-i386-linux-gnu.so  /usr/bin/ld: cannot find -lssl   /usr/bin/ld: cannot find -lcrypto   collect2: error: ld returned 1 exit status   error: command 'i686-linux-gnu-gcc' failed with exit status 1
         ----------------------------------------   Failed building wheel for mysqlclient   Running setup.py clean for mysqlclient Failed to build mysqlclient Installing collected packages: mysqlclient   Running setup.py install for mysqlclient ... error
        Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-2rl7qhom/mysqlclient/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install
    --record /tmp/pip-p7jgo6pz-record/install-record.txt --single-version-externally-managed --compile --user --prefix=:
        running install
        running build
        running build_py
        creating build
        creating build/lib.linux-i686-3.6
        creating build/lib.linux-i686-3.6/MySQLdb
        copying MySQLdb/__init__.py -> build/lib.linux-i686-3.6/MySQLdb
        copying MySQLdb/_exceptions.py -> build/lib.linux-i686-3.6/MySQLdb
        copying MySQLdb/compat.py -> build/lib.linux-i686-3.6/MySQLdb
        copying MySQLdb/connections.py -> build/lib.linux-i686-3.6/MySQLdb
        copying MySQLdb/converters.py -> build/lib.linux-i686-3.6/MySQLdb
        copying MySQLdb/cursors.py -> build/lib.linux-i686-3.6/MySQLdb
        copying MySQLdb/release.py -> build/lib.linux-i686-3.6/MySQLdb
        copying MySQLdb/times.py -> build/lib.linux-i686-3.6/MySQLdb
        creating build/lib.linux-i686-3.6/MySQLdb/constants
        copying MySQLdb/constants/__init__.py -> build/lib.linux-i686-3.6/MySQLdb/constants
        copying MySQLdb/constants/CLIENT.py -> build/lib.linux-i686-3.6/MySQLdb/constants
        copying MySQLdb/constants/CR.py -> build/lib.linux-i686-3.6/MySQLdb/constants
        copying MySQLdb/constants/ER.py -> build/lib.linux-i686-3.6/MySQLdb/constants
        copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-i686-3.6/MySQLdb/constants
        copying MySQLdb/constants/FLAG.py -> build/lib.linux-i686-3.6/MySQLdb/constants
        running build_ext
        building 'MySQLdb._mysql' extension
        creating build/temp.linux-i686-3.6
        creating build/temp.linux-i686-3.6/MySQLdb
        i686-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -Dversion_info=(1,4,6,'final',0) -D__version__=1.4.6 -I/usr/include/mysql -I/usr/include/python3.6m -c MySQLdb/_mysql.c -o build/temp.linux-i686-3.6/MySQLdb/_mysql.o
        i686-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-i686-3.6/MySQLdb/_mysql.o -lmysqlclient -lpthread -lz
    -lm -lrt -latomic -lssl -lcrypto -ldl -o build/lib.linux-i686-3.6/MySQLdb/_mysql.cpython-36m-i386-linux-gnu.so
        /usr/bin/ld: cannot find -lssl
        /usr/bin/ld: cannot find -lcrypto
        collect2: error: ld returned 1 exit status
        error: command 'i686-linux-gnu-gcc' failed with exit status 1

        ---------------------------------------- Command "/usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-2rl7qhom/mysqlclient/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install
    --record /tmp/pip-p7jgo6pz-record/install-record.txt --single-version-externally-managed --compile --user --prefix=" failed with error code 1 in /tmp/pip-build-2rl7qhom/mysqlclient/

Edit: I found a related ticket which enabled me to resolve this:

"cannot find -lssl; cannot find -lcrypto" when installing mysql-python?

sudo apt-get install libssl-dev

and then repeated:

'pip3 install mysqlclient'

this time with no errors. Not sure if extra steps preceding two steps in this link were necessary or not but for the benefit of others you might need to follow those steps:

https://pypi.org/project/mysqlclient/

Rik.Newman
  • 31
  • 8
  • 2
    I haven't tried with that particular version of Django and python3, but have you tried just installing mysqlclient with "pip install mysqlclient"? – Hayden Eastwood Jan 02 '20 at 22:37
  • see [here](https://docs.djangoproject.com/en/2.2/ref/databases/#mysql-db-api-drivers) - note in particular this: `Django requires mysqlclient 1.3.13 or later.`. So I would simply install that package with `pip`, as @HaydenEastwood suggests. – Robin Zigmond Jan 02 '20 at 22:40
  • it looks like you might not have gcc installed. What happens when you type gcc -V on your command line? – Hayden Eastwood Jan 02 '20 at 23:01
  • assuming your gcc is not installed, try: sudo apt-get install python3-dev, and then try installing mysqlclient once more – Hayden Eastwood Jan 02 '20 at 23:05
  • gcc --version gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0 – Rik.Newman Jan 02 '20 at 23:08
  • I had followed both the preceding steps: sudo apt-get install python-dev default-libmysqlclient-dev sudo apt-get install python3-dev – Rik.Newman Jan 02 '20 at 23:13
  • ok, now try installing this: apt-get install libkrb5-dev and repeat. – Hayden Eastwood Jan 03 '20 at 00:10
  • @HaydenEastwood I tried it but still getting the same error unfortunately 'sudo apt-get install libkrb5-dev' followed by 'pip3 install mysqlclient' – Rik.Newman Jan 03 '20 at 16:47
  • @HaydenEastwood I got past the error after finding this ticket, seems a different library was needed 'sudo apt-get install libssl-dev' which I installed and then repeated 'pip3 install mysqlclient' this time with no errors. Seems I'm in business now, thanks for your help. https://stackoverflow.com/questions/25979525/cannot-find-lssl-cannot-find-lcrypto-when-installing-mysql-python – Rik.Newman Jan 03 '20 at 16:54

1 Answers1

1

This issue should resolve itself if you install mysqlclient:

pip3 install mysqlclient
Hayden Eastwood
  • 928
  • 2
  • 10
  • 20