python mysql安装历程

今天工作需要在Python里操作mysql数据库,于是就开始了python mysql的安装。两种方法:

一、方法一:
1.下载MySQL-python-1.2.3.tar.gz
http://sourceforge.net/projects/mysql-python/
2.手动安装
$ tar xfz MySQL-python-1.2.3.tar.gz
$ cd MySQL-python-1.2.3
$ vim site.cfg
修改mysql_config为mysql配置文件的路径,如/etc/mysql/my.cnf
$ python setup.py build
$ sudo python setup.py install

可能遇到的问题:
/etc/mysql/my.cnf: 19: [client]: not found
/etc/mysql/my.cnf: 20: port: not found
/etc/mysql/my.cnf: 21: socket: not found
/etc/mysql/my.cnf: 22: default-character-set=utf8: not found
/etc/mysql/my.cnf: 28: [mysqld_safe]: not found
/etc/mysql/my.cnf: 29: socket: not found
nice: =: No such file or directory
/etc/mysql/my.cnf: 32: [mysqld]: not found
/etc/mysql/my.cnf: 43: user: not found
/etc/mysql/my.cnf: 44: socket: not found
/etc/mysql/my.cnf: 45: port: not found
/etc/mysql/my.cnf: 46: basedir: not found
/etc/mysql/my.cnf: 47: datadir: not found
/etc/mysql/my.cnf: 48: tmpdir: not found
/etc/mysql/my.cnf: 49: default-character-set=utf8: not found
/etc/mysql/my.cnf: 51: skip-external-locking: not found
/etc/mysql/my.cnf: 55: bind-address: not found
/etc/mysql/my.cnf: 59: key_buffer: not found
/etc/mysql/my.cnf: 60: max_allowed_packet: not found
/etc/mysql/my.cnf: 61: thread_stack: not found
/etc/mysql/my.cnf: 62: thread_cache_size: not found
/etc/mysql/my.cnf: 65: myisam-recover: not found
/etc/mysql/my.cnf: 72: query_cache_limit: not found
/etc/mysql/my.cnf: 73: query_cache_size: not found
/etc/mysql/my.cnf: 83: log_error: not found
/etc/mysql/my.cnf: 95: expire_logs_days: not found
/etc/mysql/my.cnf: 96: max_binlog_size: not found
/etc/mysql/my.cnf: 118: [mysqldump]: not found
/etc/mysql/my.cnf: 119: quick: not found
/etc/mysql/my.cnf: 120: quote-names: not found
/etc/mysql/my.cnf: 121: max_allowed_packet: not found
/etc/mysql/my.cnf: 123: [mysql]: not found
/etc/mysql/my.cnf: 126: [isamchk]: not found
/etc/mysql/my.cnf: 127: key_buffer: not found
/etc/mysql/my.cnf: 133: !includedir: not found
Traceback (most recent call last):
File "setup.py", line 15, in
metadata, options = get_config()
File "/usr/local/MySQL-python-1.2.3/setup_posix.py", line 43, in get_config
libs = mysql_config("libs_r")
File "/usr/local/MySQL-python-1.2.3/setup_posix.py", line 24, in mysql_config
raise EnvironmentError("%s not found" % (mysql_config.path,))
EnvironmentError: /etc/mysql/my.cnf not found
此时最简的解决办法是用方法二处理。

二、方法二:
sudo apt-get install python-mysqldb
可能遇到的问题:
安装完后运行
>>> import MySQLdb
Traceback (most recent call last):
File "", line 1, in
File "MySQLdb/__init__.py", line 23, in
(version_info, _mysql.version_info))
ImportError: this is MySQLdb version (1, 2, 3, 'final', 0), but _mysql is version (1, 2, 2, 'final', 0)
解决办法是:删除已经编译的MySQL-python即可

最后运行python>>> import MySQLdb
>>>
没有返回错误信息,说明安装成功。

taxonomy: