• python2.7之MySQLdb模块 for linux安装


    1.下载:MySQL-python
    http://sourceforge.net/projects/mysql-python/files/mysql-python-test/1.2.3b1/MySQL-python-1.2.3b1.tar.gz/download
    tar -zxf MySQL-python-1.2.3b1.tar.gz
    cd MySQL-python-1.2.3b1
    python setup.py build
    ==>ImportError: No module named setuptools

    2.下载setuptools
    http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c8.tar.gz
    tar -zxf setuptools-0.6c8.tar.gz
    cd setuptools-0.6c8
    python setup.py build
    python setup.py install

    3.回到MySQL-python-1.2.3b目录
    cd MySQL-python-1.2.3b1
    在运行时python setup.py build && python setup.py install没有如下错误:

    有类似错误,处理方式如下:
    python setup.py build #若,报错:mysql_config not found
    找出mysql 安装目录:/opt/mysql/
    vi MySQL-python-1.2.3b1/setup_posix.py
    行     26 #mysql_config.path = "mysql_config"
    改成如下: 
    行     27 mysql_config.path = "/opt/mysql/bin/mysql_config"

    再次运行 
    python setup.py build #报错:/usr/bin/ld:cannot find -lmysqlclient_r
    root下:
    echo "/opt/mysql/lib/mysql" >> /etc/ld.so.conf #请主意这个/etc/ld.so.conf,
    这个是ld的默认配置文件,视系统不同而不同。 /opt/mysql/lib/mysql与mysql安装目录相关。

    刷新ld配置文件让其生效,使用:ldconfig

    接着运行 python setup.py install

    检验是否已经安装成功MySQLdb时,提示如下错误:
    >>> import MySQLdb
    /usr/lib/python2.4/site-packages/MySQL_python-1.2.3b1-py2.4-linux-i686.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /usr/lib/python2.4/site-packages/MySQL_python-1.2.3b1-py2.4-linux-i686.egg/_mysql.pyc, but /data/MySQL-python-1.2.3b1 is being added to sys.path
    Traceback (most recent call last):
      File "", line 1, in ?
      File "MySQLdb/__init__.py", line 19, in ?
        import _mysql
      File "build/bdist.linux-i686/egg/_mysql.py", line 7, in ?
      File "build/bdist.linux-i686/egg/_mysql.py", line 6, in __bootstrap__
    ImportError: libmysqlclient_r.so.16: cannot open shared object file: No such file or directory
    解决方法:
    vi /etc/profile下加:
    export LD_LIBRARY_PATH=/opt/mysql/lib/mysql:$LD_LIBRARY_PATH

    连接数据库错误:

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
    File "MySQLdb/connections.py", line 170, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
    _mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)")

    原因没有安装mysql-server

  • 相关阅读:
    python生成器和使用gevent操作协程
    python飞机大战
    python控制鼠标键盘+监听键盘
    python生成彩色二维码
    springboot+springcloud+maven相关父子项目创建
    c++求最大公约数
    java8 LocalDateTime
    nginx 代理wss
    vue 全局使用axios
    Vue3.0核心源码解读| 组件渲染:vnode 到真实 DOM 是如何转变的?
  • 原文地址:https://www.cnblogs.com/jtlin/p/5973500.html
Copyright © 2020-2023  润新知