Install MySQLdb on Ubuntu

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.