MySQLdb ImportError: libmysqlclient.so.18 solution

  • 2020-04-02 13:58:23
  • OfStack

After installing MySQLdb, the error in import MySQLdb is as follows:


[root@lizhong MySQL-python-1.2.3]# /usr/local/bin/python2.7
Python 2.7.6 (default, Apr 10 2014, 15:45:39)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
/usr/local/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-x86_64.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /usr/local/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-x86_64.egg/_mysql.pyc, but /soft/MySQL-python-1.2.3 is being added to sys.path
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "MySQLdb/__init__.py", line 19, in <module>
    import _mysql
  File "build/bdist.linux-x86_64/egg/_mysql.py", line 7, in <module>
  File "build/bdist.linux-x86_64/egg/_mysql.py", line 6, in __bootstrap__
ImportError: libmysqlclient.so.18: cannot open shared object file: No such file or directory

According to the final tip, you should not find a file for libmysqlclient.so.18, so go to the mysql installation directory to find the file and make a soft connection to /usr/lib

ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18

If 64 system:

ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18

Import MySQLdb again and it will be normal:

[root@lizhong MySQL-python-1.2.3]# /usr/local/bin/python2.7
Python 2.7.6 (default, Apr 10 2014, 15:45:39)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>>


Related articles: