Technical Guide to Resolving mysql_config Not Found Error in MySQL-python Installation

Nov 24, 2025 · Programming · 9 views · 7.8

Keywords: MySQL-python | mysql_config | Ubuntu installation

Abstract: This article provides an in-depth analysis of the mysql_config not found error encountered during MySQL-python installation on Ubuntu/Linux systems. It offers two comprehensive solutions: installation via system package manager and pip installation with dependencies. The guide explores differences between MySQL-python and mysql-connector-python, includes complete dependency installation steps, troubleshooting methods, and practical code examples to help developers resolve MySQL database connectivity issues effectively.

Problem Background and Error Analysis

When installing MySQL database connectors using pip install MySQL-python command in Ubuntu/Linux environments, developers frequently encounter the sh: mysql_config: not found error. This error indicates that the system cannot locate the MySQL client development tools, which are essential components for compiling the MySQL-python extension module.

Root Cause Analysis

MySQL-python (also known as MySQLdb) is a C-extension based Python MySQL connector that requires the mysql_config tool during installation to obtain compilation and linking information for MySQL client libraries. When the MySQL client development package is not installed on the system, the mysql_config tool becomes unavailable, causing the installation to fail.

Solution 1: Installation via System Package Manager

For Ubuntu systems, the most straightforward and recommended solution is to install the pre-compiled Python MySQL connector using the system's package manager:

sudo apt-get install python-mysqldb

This approach avoids compilation dependency issues and provides a more stable and reliable installation process. After installation, developers can directly use import MySQLdb in Python to connect to MySQL databases.

Solution 2: Installing Required Dependencies via pip

If installation via pip is required in virtual environments or specific configurations, necessary development dependencies must be installed first:

sudo apt-get install build-essential python-dev libmysqlclient-dev

These packages provide compilation tools, Python development headers, and MySQL client development libraries. After installing these dependencies, running pip install MySQL-python again should complete successfully.

Alternative Solution: mysql-connector-python

As an alternative to MySQL-python, consider using the official mysql-connector-python, which is a pure Python implementation of MySQL connector with simpler installation:

pip install mysql-connector-python

This connector is fully compliant with Python Database API Specification v2.0 (PEP 249) and provides both classic API and XDevAPI interfaces. Below is a basic usage example:

import mysql.connector

# Connect to MySQL server
cnx = mysql.connector.connect(
    host="127.0.0.1",
    port=3306,
    user="username",
    password="password")

# Get a cursor
cur = cnx.cursor()

# Execute a query
cur.execute("SELECT CURDATE()")

# Fetch result
row = cur.fetchone()
print("Current date is: {0}".format(row[0]))

# Close connection
cnx.close()

Technical Details Deep Dive

During installation, MySQL-python invokes the get_config() function in setup_posix.py, which uses the mysql_config tool to obtain the following critical information:

When mysql_config is unavailable, these configuration details cannot be retrieved, resulting in compilation failure.

Best Practices Recommendations

For production environments, prioritize installation via system package manager to ensure stability and system compatibility. For development environments, choose between MySQL-python or mysql-connector-python based on specific requirements. When using virtual environments, ensure all development dependencies are properly configured before installing MySQL-python.

Troubleshooting Guidelines

If issues persist after installation, check the following aspects:

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.