MySQLdb and _mysql versions ncompatible: how to upgrade _mysql
Question: I'm running MySQLdb v1.2.3 and getting the following error:

    
    
    LookupError: unknown encoding: utf8mb4
    

[This answer](http://stackoverflow.com/questions/21517358/django-mysql-
unknown-encoding-utf8mb4) suggests updating MySQLdb to version 1.2.5. I
updated and am now getting this error:

    
    
    ImportError: this is MySQLdb version (1, 2, 5, 'final', 1), but _mysql is version (1, 2, 3, 'final', 0)
    

I'm not sure how to go about updating `_mysql` or how this will change my
setup. Is this just a python module or is it connected in some way to my MySQL
server?

**EDIT:** I've tried running the following three methods:

    
    
    sudo pip uninstall mysql-python
    sudo pip install mysql-python
    
    sudo pip uninstall mysql-python
    sudo pip install mysql-python==1.2.5
    
    sudo pip install mysql-python --upgrade
    

When uninstalling I get

    
    
    /usr/local/lib/python2.7/dist-packages/_mysql.so
    /usr/local/lib/python2.7/dist-packages/_mysql_exceptions.py
    /usr/local/lib/python2.7/dist-packages/_mysql_exceptions.pyc
    Proceed (y/n)? y
    Successfully uninstalled MySQL-python-1.2.3
    

After that I am unable to import either `MySQLdb` or `_mysql` but reinstalling
always gives me `_mysql` version 1.2.3.

**SECOND EDIT / SOLUTION:** Turns out `_mysql` was installed in two different
places on the server. Uninstalling/installing, as above, upgraded `_mysql` to
v1.2.5 but whenever I then imported `MySQLdb` precedence was given to the
other version of `_mysql` which was not being touched by pip.


Answer: According to the [user manual](http://mysql-
python.sourceforge.net/MySQLdb.html#id3):

> If you want to write applications which are portable across databases, use
> MySQLdb, and avoid using this module directly. _mysql provides an interface
> which mostly implements the MySQL C API. For more information, see the MySQL
> documentation. The documentation for this module is intentionally weak
> because you probably should use the higher-level MySQLdb module.

Basically, _mysql is an object-oriented wrapper for the MySQL C API.

[This post](http://stackoverflow.com/questions/4536103/how-can-i-upgrade-
specific-packages-using-pip-and-a-requirements-file) explains how to use pip
to upgrade one module, a module with all its dependencies, or any combination
thereof. I think that, given the statement, MySQLdb does not have a dependency
on _mysql, and they were not upgraded together. Please visit the link shared.

**EDIT:** After some digging, I found that Ubuntu does not support MySQL
nicely, and just pip doesn't work.

So I went to this [link](http://stackoverflow.com/a/29150749/4900327) and did:

`apt-get install python-dev libmysqlclient-dev`

before doing

`sudo pip install MySQL-python`

This worked nicely for me. For you, I think you may need to upgrade or even
apt-get remove and then reinstall the above two Ubuntu modules `python-dev`
and `libmysqlclient-dev`.

For me, it's working now when installing for the first time; go to a terminal
and enter the python interpreter, then type:

    
    
    import MySQLdb
    MySQLdb.__version__    #I got '1.2.5'
    import _mysql
    _myql.__version__    #Again, I got '1.2.5'
    

