mysqldb 安装心路 Ubuntu

06 Jun 2011


引用原文"注意:在你开始使用数据库之前,确保你已经安装了合适的数据库访问库。比如对于 MySQL 数据库,使用 MySQLdb,对于 Postgres 数据库使用 psycopg2。" Postgres 没听说过,故还是选择 MySQL。

根据链接去下载了 MySQL-python-1.2.3,打开 README,好长的安装前提条件,,,


+ Python 2.3.4 or higher

  • Versions lower than 2.3 WON’T WORK.
  • 2.4 is the primary test environment.
  • Red Hat Linux:
- Make sure you have the Python development headers and libraries (python-devel).

+ setuptools


+ MySQL 3.23.32 or higher

  • Versions lower than 3.22 definitely WON’T WORK.
  • Versions lower than 3.22.19 might not work.
  • MySQL-3.22 might work but isn’t supported anymore. It’s very old.
  • MySQL-3.23 ought to work, but it’s pretty elderly.
  • MySQL-4.0 is supported, but not tested and slightly discouraged.
  • MySQL-4.1 is supported. The prepared statements API is not
    supported, and won’t be until MySQLdb-1.3 or 2.0, if ever.
  • MySQL-5.0 is supported and tested, including stored procedures.
  • MySQL-5.1 is supported (currently a release candidate) but untested.
    It should work.
  • MySQL-6.0 is sorta-kinda-supported (currently alpha) but untested.
    It should work.
  • Drizzle <> is a fork of MySQL. So far
    the C API looks really similar except everything is renamed.
    Drizzle support probably won’t happen in 1.2. There may be have to
    be an entirely different module, but still using DB-API.
  • MaxDB, formerly known as SAP DB (and maybe Adabas D?), is a
    completely different animal. Use the sapdb.sql module that comes
    with MaxDB.
  • Red Hat Linux packages:
- mysql-devel to compile - mysql and/or mysql-devel to run
  • RPM packages:
- MySQL-devel to compile - MySQL-shared if you want to use their shared library. Otherwise you’ll get a statically-linked module, which may or may not be what you want. - MySQL-shared to run if you compiled with MySQL-shared installed
  • Transactions (particularly InnoDB tables) are supported for
    MySQL-3.23 and up. You may need a special package from your vendor
    with this support turned on.

+ zlib

  • Required for MySQL-3.23 and newer.
  • Red Hat Linux
- zlib-devel to compile - zlib to run

+ openssl

  • May be needed for MySQL-4.0 or newer, depending on compilation
    options. If you need it, you probably already have it.
- you may need openssl-devel on some platforms

+ C compiler

  • Most free software-based systems already have this, usually gcc.
  • Most commercial UNIX platforms also come with a C compiler, or
    you can also use gcc.
  • If you have some Windows flavor, you usually have to pay extra
    for this, or you can use Cygwin_.

.. _Cygwin:

重点在安装 setuptools 和 MySQL

Step1 安装 MySQL


1 $ sudo apt-get install mysql-server mysql-client
2 $ sudo start mysql
3 $ sudo stop mysql

当你修改了配置文件後,你需要重启 mysqld 才能使这些修改生效。

要想检查 mysqld 进程是否已经开启,可以使用下面的命令:

1 $ pgrep mysqld

如果进程开启,这个命令将会返回该进程的 id。


Step2 安装 setuptools


下面进入主题 mysqldb 的安装

1 $ tar xfz MySQL-python-1.2.1.tar.gz
2 $ cd MySQL-python-1.2.1
3 # edit site.cfg if necessary
4 $ python build
5 $ sudo python install


Problem1 没有 mysql_config 文件

mysql_config 是属于 MySQL 开发用的文件,而使用 apt-get 安装的 MySQL 是没有这个文件的,于是在包安装器里面寻找



Problem2 EnvironmentError: mysql_config not found

root@vpser:~# cd MySQL-python-1.2.3
root@vpser:~/MySQL-python-1.2.3# python install
sh: mysql_config: not found
Traceback (most recent call last):
  File "", line 15, in <module>
    metadata, options = get_config()
  File "/root/MySQL-python-1.2.3/", line 43, in get_config
    libs = mysql_config("libs_r")
  File "/root/MySQL-python-1.2.3/", line 24, in mysql_config
    raise EnvironmentError("%s not found" % (mysql_config.path,))
EnvironmentError: mysql_config not found

首先查找 mysql_config 的位置,使用 find / -name mysql_config,比如我的在 /usr/local/mysql/bin/mysql_config

修改 文件,在26行:
mysql_config.path = “mysql_config” 修改为:
mysql_config.path = “/usr/local/mysql/bin/mysql_config”

1 $ python build
2 $ sudo python install

说到这里其实非常的麻烦了,继续看 README 文档,下面其实有说明可以直接用新立得安装,尴尬,,,。。。

1 $ sudo apt-get install python-mysqldb

Fork me on GitHub