6

I tried to get build an app on Django and I wanted to use MySQL as the database. After setting up the settings.py right, I tried to migrate. Then I got the obvious error saying that MySQL is not installed. So inorder to install MySQL, I ran:

sudo -H pip install MySQL-python

I get the following error:

    Collecting MySQL-python
  Using cached MySQL-python-1.2.5.zip
    Complete output from command python setup.py egg_info:
    sh: mysql_config: command not found
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/tmp/pip-build-zAMhWo/MySQL-python/setup.py", line 17, in <module>
        metadata, options = get_config()
      File "setup_posix.py", line 43, in get_config
        libs = mysql_config("libs_r")
      File "setup_posix.py", line 25, 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 /private/tmp/pip-build-zAMhWo/MySQL-python/

How do I fix this?

JGCW
  • 1,509
  • 1
  • 13
  • 25
  • Possible duplicate of [mysql\_config not found when installing mysqldb python interface](http://stackoverflow.com/questions/7475223/mysql-config-not-found-when-installing-mysqldb-python-interface) – GoingMyWay Feb 21 '17 at 03:05

5 Answers5

13

MySQL-python

MySQL-3.23 through 5.5 and Python-2.4 through 2.7 are currently supported. Python-3.0 will be supported in a future release.

You can install mysqlclient or pymysql,I prefer the latter:

pip install pymysql

And add this to your manage.py:

import pymysql
pymysql.install_as_MySQLdb()
McGrady
  • 10,869
  • 13
  • 47
  • 69
  • sudo and your command worked. But I cannot still run python manage.py migrate. – JGCW Feb 21 '17 at 03:18
  • 1
    @GeethW add this to your `manage.py`,`import pymysql` `pymysql.install_as_MySQLdb()` – McGrady Feb 21 '17 at 03:23
  • Thank you so much McGrady. I don't really understand what happened. Can you explain? I tried so many driver options. For example, pip install mysql-python and then manually downloading a .dmg from oracle. Nothing worked. Please explain. – JGCW Feb 21 '17 at 03:28
  • @GeethW Actually,I can't explain what happened when you install mysql-python, it seems that `pymysql` is more friendly than `mysql-python` to support python now. – McGrady Feb 21 '17 at 03:41
6

Did you install the SQL client dev pacakge on your system?

sudo apt install libmysqlclient-dev

if you are using mariadb, the drop in replacement for mysql, then run

sudo apt install libmariadbclient-dev

then try again

pip install mysql-connector
Vic
  • 161
  • 1
  • 4
2

Installations of mysql in some environments can place binaries in directory relative to environment. pip installation fails because setup.py does not find mysql_config at expected path. Steps for source installation -

  1. Download files here Edit mysql_config path mentioned in MySQL-python-1.2.5/site.cfg with correct path on your local environment. For example,
mysql_config = /usr/local/mysql/bin/mysql_config
  1. Run below commands inside directory MySQL-python-1.2.5 -
$ python setup.py build
$ sudo python setup.py install
1

Have you tried installing a MySQL db driver?

The following documentation tells you how to install one:

https://docs.djangoproject.com/en/1.10/ref/databases/#mysql-db-api-drivers

digitalnomd
  • 1,380
  • 12
  • 21
1

I faced the same problem trying to install mysqlclient, try to install mysql-devel. on RHEL/Centos for example:

yum install mysql-devel