MySQLdb操作mysql数据库的两个细节

一、安装

下载最新的mysqlclient-1.3.10.tar.gz解压后:

如果是apt方式安装mysql(甚至不需下载源码直接pip install mysqlclient),只需执行即可:

sudo python setup.py install 

如果是源码编译,或二进制包安装,假设安装目录为:/opt/mysql

1、修改mysqlclient-1.3.10/site.cfg

# The path to mysql_config.
# Only use this if mysql_config is not on your PATH, or you have some weird
# setup that requires it.
#mysql_config = /usr/local/bin/mysql_config

改为:

# The path to mysql_config.
# Only use this if mysql_config is not on your PATH, or you have some weird
# setup that requires it.
#mysql_config = /usr/local/bin/mysql_config
mysql_config = /opt/mysql/bin/mysql_config

2.安装:

python setup.py build 
sudo python setup.py install 

二、环境中动态链接库搜索路径中要有/opt/mysql/lib不然会有如下错误:

$ python mysqltest1.py
Traceback (most recent call last):
  File "mysqltest1.py", line 2, in <module>
    import MySQLdb
  File "build/bdist.linux-x86_64/egg/MySQLdb/__init__.py", line 19, in <module>
  File "build/bdist.linux-x86_64/egg/_mysql.py", line 7, in <module>
  File "build/bdist.linux-x86_64/egg/_mysql.py", line 6, in __bootstrap__
ImportError: libmysqlclient.so.20: cannot open shared object file: No such file or directory

解决:

export  LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/mysql/lib
sudo ldconfig -v

如果是pycharm在运行/调试配置窗口设置environment variable,参考pycharm不能访问oracle(sybase)的问题

猜你喜欢

转载自my.oschina.net/u/2245781/blog/1823368