Python Mysql module MySQLdb installation details

  • 2020-04-02 13:33:46
  • OfStack

By default, the MySQLdb package is not installed. You'll be convinced if you see something like this.
 -bash-3.2# /usr/local/python2.7.3/bin/python get_cnblogs_news.py 
Traceback (most recent call last):
  File "get_cnblogs_news.py", line 9, in <module>
    import MySQLdb
ImportError: No module named MySQLdb

At this point we have to install the MySQLdb package. Installation is actually quite simple, the specific steps are as follows:
  Download MySQL for Python
Address: http://sourceforge.net/projects/mysql-python/files/mysql-python/
I installed version 1.2.3 here
wget http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz

2, decompression
tar zxvf MySQL-python-1.2.3.tar.gz

3, installation,
$ cd MySQL-python-1.2.3
$ python setup.py build
$ python setup.py install

Note:
If you encounter the following error while executing: python setup.py build:
EnvironmentError: mysql_config not found

First, find the location of mysql_config, using
Find / -name mysql_config, such as my in/usr/local/mysql/bin/mysql_config
Modify the setup_posix.py file on line 26:
Mysql_config. path = "mysql_config"
mysql_config.path =  " /usr/local/mysql/bin/mysql_config " 

Save and then execute again:
python setup.py build
python setup.py install

OK, that's it.

Related articles: