644

I am trying to get a Python script to run on the linux server I'm connected to via ssh. The script uses mysqldb. I have all the other components I need, but when I try to install mySQLdb via setuptools like so:,

python setup.py install

I get the following error report related to the mysql_config command.

sh: mysql_config: command not found
Traceback (most recent call last):
  File "setup.py", line 15, in <module>
    metadata, options = get_config()
  File "/usr/lib/python2.5/MySQL-python-1.2.3/setup_posix.py", line 43, in get_config
    libs = mysql_config("libs_r")
  File "/usr/lib/python2.5/MySQL-python-1.2.3/setup_posix.py", line 24, in mysql_config
    raise EnvironmentError("%s not found" % (mysql_config.path,))
EnvironmentError: mysql_config not found

Has anyone else encountered this error and if so how did you resolve it/what can I do to successfully install mysqldb?

user904542
  • 6,965
  • 5
  • 20
  • 28
  • 1
    Hi @user904542 - I've edited your question to hopefully make it a little more concise and get you some answers :) If you don't like it, or want to provide additional details, there's an edit link directly under your question which you can use to either roll back to your original, or add to the changes I've made. –  Sep 19 '11 at 22:36
  • 4
    I ran into the exact same problem, because i installed mysql from source. if you do too, just run something like this to make mysql_config available: sudo ln -s /usr/local/mysql/bin/mysql_config /usr/bin/mysql_config or add /usr/local/mysql/bin/ to your path. – Thi Duong Nguyen Feb 10 '12 at 21:48
  • If you have already installed mysql, the above comment by Thi Duong Nguyen solves the problem by creating a symlink to the mysql_config file in the /usr/bin directory, which is in the path that python will search. – Alex Boschmans Jan 31 '13 at 22:04
  • 3
    just do this-------------apt-get install python-mysqldb – ns15 Jan 10 '17 at 12:06
  • @AlexBoschmans symlinking `/usr/bin` is wrong, it is `usr/local/my...` see Thi's answer. – Timo Mar 01 '18 at 20:53
  • Mac users, read this: https://pypi.org/project/mysqlclient/ – MarMat Jul 17 '19 at 12:58

36 Answers36

926

mySQLdb is a python interface for mysql, but it is not mysql itself. And apparently mySQLdb needs the command 'mysql_config', so you need to install that first.

Can you confirm that you did or did not install mysql itself, by running "mysql" from the shell? That should give you a response other than "mysql: command not found".

Which linux distribution are you using? Mysql is pre-packaged for most linux distributions. For example, for debian / ubuntu, installing mysql is as easy as

sudo apt-get install mysql-server

mysql-config is in a different package, which can be installed from (again, assuming debian / ubuntu):

sudo apt-get install libmysqlclient-dev

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

sudo apt-get install libmariadbclient-dev

Reference: https://github.com/JudgeGirl/Judge-sender/issues/4#issuecomment-186542797

almanegra
  • 653
  • 8
  • 21
amarillion
  • 24,487
  • 15
  • 68
  • 80
  • 1
    Reading package lists...Done Building dependency tree Reading state information...Done Note, selecting libmysqlclient15-dev instead of libmysqlclient-dev Some packages could not be installed. This may mean that you have requested an impossible situation or if you are using the unstable distribution that some required packages have not yet been created or been moved out of Incoming. The following information may help to resolve the situation: The following packages have unmet dependencies: libmysqlclient15-dev: Depends: zlib1g-dev but it is not going to be installed E: Broken packages – user904542 Sep 19 '11 at 18:32
  • The above is an error I got when running the "sudo apt-get install libmysqlclient-dev" command. The error was too long to allow me to put a statement explanning what I was talking about (and so I am doing that here instead). The key of that error seems to be this part: The following packages have unmet dependencies: libmysqlclient15-dev: Depends: zlib1g-dev but it is not going to be installed E: Broken packages But I ran this command: sudo apt-cache pkgnames > packages and then checked the packages file and I found the zlib1g-dev package in that list as well as libmysqlclient15-dev – user904542 Sep 19 '11 at 18:35
  • mysql is installed - but I need mySQLdb, because that is what my script uses. – user904542 Sep 19 '11 at 18:49
  • 3
    Should 'sudo apt-get install mysql' be 'sudo apt-get install mysql-server'? – Kohanz Apr 28 '15 at 14:01
  • 10
    Note that for Debian it's `sudo apt-get install mysql-server` and `sudo apt-get install mysql-client` – Marc Juchli Apr 12 '16 at 20:54
  • And for Fedora, it's `sudo yum install mysql-server` and `sudo yum install mysql-devel`. – Alex Aug 24 '17 at 16:47
  • 11
    Can we finally admit that 'pip' is a colossal failure? Errors like this are such a pain, and they seem to happen with the majority of packages. Python badly needs a new solution to package management. – Eddie Sullivan Sep 14 '17 at 22:30
  • 6
    For me on Debian it was libmysqlclient-dev. In fact I have installed both libmariadbclient-dev libmysqlclient-dev – mirek Apr 24 '19 at 20:24
  • Solved my problem on Debian 10 with python 3.7.3 – nsssayom Sep 22 '19 at 20:09
  • This comment is a copy of a github thread. Please consider adding the reference – almanegra Nov 07 '19 at 16:37
  • You really don't need mysql-server itself, just client-dev. – Port Mar 23 '20 at 05:38
  • And for fedora using dnf, use `dnf install -y community-mysql-devel`. Use `dnf provides mysql_config` to find out which packages have this file generally. – whatacold Apr 18 '20 at 04:31
  • 1
    Worked for me, and then I got an error about missing *Python.h*, which I solved installing python3-dev – Marcelo Assis Jul 15 '20 at 19:11
  • For ubunut/debian if not need mysql server just install the libmariadbclient-dev – Cornea Valentin Jan 29 '22 at 09:25
  • For centos 7, it's actually is "yum install mariadb-devel" – Hoang Minh Quang FX15045 Apr 12 '23 at 09:28
228

I was installing python-mysql on Ubuntu 12.04 using

pip install mysql-python

First I had the same problem:

Not Found "mysql_config"

This worked for me

$ sudo apt-get install libmysqlclient-dev

Then I had this problem:

...
_mysql.c:29:20: error fatal: Python.h: No existe el archivo o el directorio

compilación terminada.

error: command 'gcc' failed with exit status 1

Then I tried with

apt-get install python-dev

(If you're using python3, install python3-dev instead.)

And then I was happy :)

pip install mysql-python
    Installing collected packages: mysql-python
      Running setup.py install for mysql-python
        building '_mysql' extension
        gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -Dversion_info=(1,2,4,'beta',4) -D__version__=1.2.4b4 -I/usr/include/mysql -I/usr/include/python2.7 -c _mysql.c -o build/temp.linux-x86_64-2.7/_mysql.o -DBIG_JOINS=1 -fno-strict-aliasing -g
        In file included from _mysql.c:44:0:
        /usr/include/mysql/my_config.h:422:0: aviso: se redefinió "HAVE_WCSCOLL" [activado por defecto]
        /usr/include/python2.7/pyconfig.h:890:0: nota: esta es la ubicación de la definición previa
        gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro build/temp.linux-x86_64-2.7/_mysql.o -L/usr/lib/x86_64-linux-gnu -lmysqlclient_r -lpthread -lz -lm -lrt -ldl -o build/lib.linux-x86_64-2.7/_mysql.so
    
Successfully installed mysql-python
Cleaning up...
Eli Harold
  • 2,280
  • 1
  • 3
  • 22
xtornasol512
  • 2,399
  • 1
  • 12
  • 7
87

(Specific to Mac OS X)

I have tried a lot of things, but these set of commands finally worked for me.

  1. Install mysql
    brew install mysql
    
  2. brew unlink mysql
  3. brew install mysql-connector-c
  4. Add the mysql bin folder to PATH
    export PATH=/usr/local/Cellar/mysql/8.0.11/bin:$PATH
    
  5. mkdir /usr/local/Cellar/lib/
  6. Create a symlink
    sudo ln -s /usr/local/Cellar/mysql/8.0.11/lib/libmysqlclient.21.dylib /usr/local/Cellar/lib/libmysqlclient.21.dylib
    
  7. brew reinstall openssl (source)
  8. Finally, install mysql-client
    LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/ pip install mysqlclient
    

Update: In case this doesn't work, @vinyll suggests to run brew link mysql before step 8.

Pulkit Pahwa
  • 1,412
  • 11
  • 8
  • For Step 4, use `brew info mysql` to get the actual/correct path to the mysql installation. Then to make the `PATH` change persist, do the `export PATH..` in your ~/.bash_profile. – Gino Mempin Dec 26 '19 at 01:21
  • step 1-4 with `brew link --overwrite mysql-connector-c` works on my Mac – Hunger Feb 12 '20 at 03:59
  • Finally, it works on my Catalina. Use specify open ssl is very important. – Happier Mar 07 '20 at 08:25
  • You should not need `sudo` to work in /usr/local/Cellar. `ln -s /usr/local/Cellar…` should be enough. – vinyll Mar 10 '20 at 21:56
  • 15
    I had to do a `brew link mysql` before step 8 to get it running. – vinyll Mar 10 '20 at 22:03
  • @vinyll it might be the case. The answer was written for *High Sierra*. I no longer am using a mac OS. I'll still add to the updates of the answer, may be it can help someone else. – Pulkit Pahwa Mar 20 '20 at 17:39
  • if you follows this not work, you might check version of your mysql-connector-c, and change version number in step four to your version. – xiaojueguan May 17 '20 at 01:40
  • Ran only the step 8 after installing `brew install mysql` – Shobi Jun 07 '20 at 06:50
  • 1
    I added these to my `.bashrc`: `export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"` `export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"`, and only ran `brew install mysql`, and it worked. Also fixes all future executions of pip to make sure they use the right ssl libs. – coredumperror Oct 03 '20 at 02:54
  • 9
    it was enough for me to just do `brew install mysql` – christegho Apr 08 '21 at 11:20
54

On Red Hat I had to do

sudo yum install mysql-devel gcc gcc-devel python-devel
sudo easy_install mysql-python

Then it worked.

Elliott
  • 1,331
  • 12
  • 12
46

On python 3.5.2 + any future version

sudo apt-get install libmysqlclient-dev python-dev

Anil Koppula
  • 743
  • 6
  • 11
  • 1
    Also the mariadb package worked like a charm : `libmariadb-dev`. – Manuel Lazo Jan 03 '22 at 16:35
  • Perhaps note that `apt-get` is specific to Debian and derived Linux distros like Ubuntu, Mint, etc. This seems to duplicate earlier answers with more details and background. – tripleee Mar 16 '23 at 06:56
37

The below worked for me on Ubuntu 12.04 LTS:

apt-get install libmysqlclient-dev python-dev

All though it worked, i still went ahead to do the below:

export PATH=$PATH:/usr/local/mysql/bin/
pb2q
  • 58,613
  • 19
  • 146
  • 147
nasowah
  • 471
  • 4
  • 3
28

I got the same error while trying to install mysql-python.

This is how I fixed it.

sudo PATH=/usr/local/mysql/bin/:$PATH pip install mysql-python

The problem was that the installer could not find the mysql_config in the default path. Now it can ..and it worked..

 15 warnings generated.
    clang -bundle -undefined dynamic_lookup -Wl,-F. build/temp.macosx-10.8-intel-2.7/_mysql.o -L/usr/local/mysql/lib -lmysqlclient_r -lz -lm -lmygcc -o build/lib.macosx-10.8-intel-2.7/_mysql.so -arch x86_64

Successfully installed mysql-python
Cleaning up...

Hope this helps.

Thanks.

Mayank R Jain
  • 3,127
  • 1
  • 29
  • 43
  • I was having the same issue installing MySQL locally on a Mac running OSX 10.9.4 (Mavericks). This solution worked for me as well. – Deepend Aug 14 '14 at 13:13
25

I fixed this problem with the following steps:

sudo apt-get install libmysqlclient-dev
sudo apt-get install python-dev
sudo python setup.py install
Jack
  • 10,943
  • 13
  • 50
  • 65
zhaozhi
  • 1,491
  • 1
  • 16
  • 19
  • 2
    When running >= Debian Stretch make sure to install `default-libmysqlclient-dev`, since the regular `libmysqlclient-dev` package has no installation candidate. – Bono Jun 05 '19 at 13:05
21

The commands (mysql too) mPATH might be missing.

export PATH=$PATH:/usr/local/mysql/bin/

samwize
  • 25,675
  • 15
  • 141
  • 186
20

Step1:-Install Python3 & Python3-dev Both

sudo apt-get install python3 python3-dev

Step2:- Install Python & Mysql Connector

sudo apt-get install libmysqlclient-dev

step3:- Install python mysql client

sudo apt-get install mysqlclient

This will Solve your Problem

Akash
  • 2,795
  • 1
  • 7
  • 11
19

The package libmysqlclient-dev is deprecated, so use the below command to fix it.

Package libmysqlclient-dev is not available, but is referred to by another package. This may mean that the package is missing, has been obsoleted, or is only available from another source

sudo apt-get install default-libmysqlclient-dev
18

I think the most convenient way to solve this problem in 2020 is using another python package. We don't need install any other binary software.

Try this

pip install mysql-connector-python

and then

import mysql.connector

mydb = mysql.connector.connect(
          host="",
          user="",
          passwd="",
          database=""
          )      
cursor = mydb.cursor( buffered=True)
cursor.execute('show tables;')
cursor.execute('insert into test values (null, "a",10)')
mydb.commit()
mydb.disconnect()
xfdywy
  • 191
  • 1
  • 6
  • More information here: https://dev.mysql.com/doc/connector-python/en/connector-python-django-backend.html and here https://dev.mysql.com/downloads/connector/python/ – Gilbert May 22 '22 at 19:07
15

If you're on macOS and already installed mysql@5.7 via brew install:

  1. brew install mysql-connector-c
  2. brew unlink mysql@5.7
  3. brew link --overwrite --dry-run mysql@5.7 first, to see what symlinks are getting overwritten
  4. brew link --overwrite --force mysql@5.7 to actually overwrite mysql-related symlinks with mysql@5.7
  5. pip install mysqlclient
luthfianto
  • 1,746
  • 3
  • 22
  • 37
12

I fixed it by installing libmysqlclient:

sudo apt-get install libmysqlclient16-dev
thoslin
  • 6,659
  • 6
  • 27
  • 29
11

In centos 7 this works for me :

yum install mariadb-devel
pip install mysqlclient
Rajiv Sharma
  • 6,746
  • 1
  • 52
  • 54
10

The MySQL-python package is using the mysql_config command to learn about the mysql configuration on your host. Your host does not have the mysql_config command.

The MySQL development libraries package (MySQL-devel-xxx) from dev.mysql.com provides this command and the libraries needed by the MySQL-python package. The MySQL-devel packages are found in the download - community server area. The MySQL development library package names start with MySQL-devel and vary based MySQL version and linux platform (e.g. MySQL-devel-5.5.24-1.linux2.6.x86_64.rpm.)

Note that you do not need to install mysql server.

Gaur93
  • 685
  • 7
  • 19
John Livingston
  • 101
  • 1
  • 2
8

For Alpine Linux:

$ apk add mariadb-dev mariadb-client mariadb-libs

MariaDB is a drop-in replacement for MySQL and became the new standard as of Alpine 3.2. See https://bugs.alpinelinux.org/issues/4264

aboutaaron
  • 4,869
  • 3
  • 36
  • 30
7

On my Fedora 23 machine I had to run the following:

sudo dnf install mysql-devel
Ahsan
  • 3,845
  • 2
  • 36
  • 36
6

In CentOS 7 , the following things should be done:

#step1:install mysql 
https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/

#step2:
sudo yum install mysql-devel

or

sudo yum install mysql-community-devel
caot
  • 3,066
  • 35
  • 37
DachuanZhao
  • 1,181
  • 3
  • 15
  • 34
6

also, i fetch the same problem enter image description here I fixed this problem with the following steps: First I run this command

sudo apt-get install libmysqlclient-dev

then I install

pip install mysqlclient==2.1.0

this is worked for me

Mithun Rana
  • 1,334
  • 1
  • 9
  • 10
  • Please [don’t post images of code, error messages, or other textual data.](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors) – tripleee Mar 16 '23 at 06:55
4

I think, following lines can be executed on terminal

 sudo ln -s /usr/local/zend/mysql/bin/mysql_config /usr/sbin/

This mysql_config directory is for zend server on MacOSx. You can do it for linux like following lines

sudo ln -s /usr/local/mysql/bin/mysql_config /usr/sbin/

This is default linux mysql directory.

hkulekci
  • 1,894
  • 15
  • 27
4

I had this issues and solved if by adding a symlink to mysql_config.

I had installed mysql with homebrew and saw this in the output.

Error: The `brew link` step did not complete successfully

Depending on how you got mysql it will be in different places. In my case /usr/local/Cellar/mysql
Once you know where it is you should be able to ma a symbolic link to where python is looking for it. /usr/local/mysql

This worked for me.

ln -s /usr/local/Cellar/mysql/<< VERSION >>/bin/mysql_config   /usr/local/mysql/bin/mysql_config
Schechter
  • 224
  • 2
  • 5
4

I had the same problem. I solved it by following this tutorial to install Python with python3-dev on Ubuntu 16.04:

sudo apt-get update
sudo apt-get -y upgrade
sudo apt-get install -y python3-pip
sudo apt-get install build-essential libssl-dev libffi-dev python3-dev

And now you can set up your virtual environment:

sudo apt-get install -y python3-venv
pyvenv my_env
source my_env/bin/activate
Little Brain
  • 2,647
  • 1
  • 30
  • 54
3

You need to install the python-dev package:

sudo apt-get install python-dev
McGarnagle
  • 101,349
  • 31
  • 229
  • 260
ebarch
  • 89
  • 1
  • 7
2

This method is only for those who know that Mysql is installed but still mysql_config can't be find. This happens if python install can't find mysql_config in your system path, which mostly happens if you have done the installation via .dmg Mac Package or installed at some custom path. The easiest and documented way by MySqlDB is to change the site.cfg. Find the mysql_config which is probably in /usr/local/mysql/bin/ and change the variable namely mysql_config just like below and run the installation again. Don't forget to un-comment it by removing "#"

Change below line

"#mysql_config = /usr/local/bin/mysql_config"

to

"mysql_config = /usr/local/mysql/bin/mysql_config"

depending upon the path in your system.

By the way I used python install after changing the site.cfg

sudo /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python setup.py install

Ahsan.Amin
  • 736
  • 6
  • 14
2

So far, all solutions (Linux) require sudo or root rights to install . Here is a solution if you do not have root rights and without sudo. (no sudo apt install ...):

  1. Download the .deb file of the libmysqlclient-dev, e.g. from this mirror
  2. Navigate to the downloaded file and run dpkg -x libmysqlclient-dev_<version tag>.deb . This will extract a folder called usr.
  3. Symlink ./usr/bin/mysql_config to somewhere that is found on your $PATH:

    ln -s `pwd` /usr/bin/mysql_config FOLDER_IN_YOUR_PATH

  4. It should now be able to find mysql_config

Tested on Ubuntu 18.04.

RunOrVeith
  • 4,487
  • 4
  • 32
  • 50
2

For macOS Mojave , additional configuration was required, for compilers to find openssl you may need to set:

export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"
Haidar Zeineddine
  • 979
  • 1
  • 8
  • 20
2

On MacOS (OS/X) Catalina, I found that I needed to do this:

 export PATH=$PATH:/usr/local/bin/:/usr/local/mysql-5.7.16-osx10.11-x86_64/bin/

On my computer, the mysql command is in /usr/local/bin (from the standard MacOS installer, not "brew").

Mike Robinson
  • 8,490
  • 5
  • 28
  • 41
2

sudo apt-get install python-mysqldb

Python 2.5? Sounds like you are using a very old version of Ubuntu Server (Hardy 8.04?) - please confirm which Linux version the server uses.

python-mysql search on ubuntu package database

Some additional info:

From the README of mysql-python -

Red Hat Linux .............

MySQL-python is pre-packaged in Red Hat Linux 7.x and newer. This includes Fedora Core and Red Hat Enterprise Linux. You can also build your own RPM packages as described above.

Debian GNU/Linux ................

Packaged as python-mysqldb_::

# apt-get install python-mysqldb

Or use Synaptic.

.. _python-mysqldb: http://packages.debian.org/python-mysqldb

Ubuntu ......

Same as with Debian.

Footnote: If you really are using a server distribution older than Ubuntu 10.04 then you are out of official support, and should upgrade sooner rather than later.

Gary
  • 21
  • 1
1

I encountered the same problem, just added the path where *mysql_config* resided to the environment variable PATH and it worked for me.

flyer
  • 9,280
  • 11
  • 46
  • 62
1

sudo apt-get build-dep python-mysqldb will install all the dependencies to build the package from PIP/easy_install

Thomas Grainger
  • 2,271
  • 27
  • 34
1

As actual error is

gcc ... -I/usr/include/python2.7 ...

_mysql.c:29:20: error: Python.h: No such file or directory

and If you can't install python-dev or python-devel packages, you may download archive with needed version of python sources from http://hg.python.org/ and place headers files in proper folder for include

Oleg Neumyvakin
  • 9,706
  • 3
  • 58
  • 62
1

Just type:

$ sudo apt-get install python-dev
$ venv/bin/pip install MySQL-python

This will solve this problems.

1

In CentOS 7 instead of yum install mysql-devel do yum install mysql-community-devel

This does not require you to add the mysql repo.

cztchoice
  • 49
  • 8
1

Debian 11 install libmariadb-dev-compat to provide mysql_config binary for pip install mysqlclient

Hayden Thring
  • 1,718
  • 17
  • 25
-2

sudo apt-get install libmysqlclient-dev sudo apt-get install python-dev sudo apt-get install MySQL-python

NOtice you should install python-dev as well, the packages like MySQL-python are compiled from source. The pythonx.x-dev packages contain the necessary header files for linking against python. Why does installing numpy require python-dev in Kubuntu 12.04

Community
  • 1
  • 1
Ivan Wang
  • 460
  • 4
  • 3