I'm going to use Python to write some scripts to train the trainer. So I thought it was very easy to install Python's MySQLdb on Ubuntu. I didn't expect to encounter a few small twists and turns, So I recorded it as a memo. First, install Python-dev. Otherwise, an error will be reported when MySQLdb is compiled later. The header file: building '_ mysql' extensiongcc-pthread-fno-strict-aliasing-DNDEBUG-g-fw is not found.
I'm going to use Python to write some scripts to train the trainer. So I thought it was very easy to install Python's MySQLdb on Ubuntu. I didn't expect to encounter a few small twists and turns, So I recorded it as a memo.
First, install Python-dev. Otherwise, an error will be reported when MySQLdb is compiled later. The header file cannot be found:
Building '_ mysql' extension
Gcc-pthread-fno-strict-aliasing-DNDEBUG-g-fwrapv-O2-Wall-Wstrict-prototypes-fPIC
-Dversion_info = (1, 2, 3, 'final', 0)-D _ version __= 1.2.3
-I/u01/mysql/include/mysql-I/usr/include/python2.6-c _ mysql. c
-O build/temp. linux-i686-2.6/_ mysql. o-DUNIV_LINUX
In file encoded ded from _ mysql. c: 29:
Pymemcompat. h: 10: fatal error: Python. h: the file or directory does not exist.
Compilation terminated.
Error: command 'gcc 'failed with exit status 1
Sudo apt-get install python-dev
Install setuptools first. Otherwise, MySQLdb cannot be compiled.
ImportError: No module named setuptools
Download setuptools from here
Python setup. py build
Sudo python setup. py install
Download MySQLdb from here
Modify site. cfg to point mysql_config to the correct location
Python setup. py build
Sudo python setup. py install
Finally, you must install libmysqlclient-dev. Otherwise, an error occurs during the import module.
ImportError: libmysqlclient_r.so.16: cannot open shared object file:
No such file or directory
Sudo apt-get install libmysqlclient-dev
After installation, a simple hello world query will be provided.
#! /Usr/bin/env python
Import MySQLdb
Db = MySQLdb. connect (host = "host_name", db = "mysql", user = "ningoo", passwd = "password ")
C = db. cursor ()
N=c.exe cute ("select user, host from user ")
For row in c. fetchall ():
For col in row:
Print col