Linux下Python连接MySQL异

发布时间:2019-09-08 09:10:54编辑:auto阅读(1814)

    家里的电脑使用Linux操作系统,最近采集的数据需要存储到MySQL,本来使用web.py的时候使用MySQL是一切正常的,结果现在直接使用MySQLdb连接数据库时异常:

    /usr/lib/python2.7/dist-packages/pkg_resources.py:1031: UserWarning: /home/huayuan/.python-eggs is writable by group/others and vulnerable to attack when used with get_resource_filename. Consider a more secure location (set with .set_extraction_path or the PYTHON_EGG_CACHE environment variable).
      warnings.warn(msg, UserWarning)


    看提示应该是Linux系统权限问题,而且在XP电脑上验证过,连接MySQL的Python代码本身是没有问题的,代码如下:

    #coding=utf-8
    import MySQLdb
    
    try:
        conn = MySQLdb.connect(host = '127.0.0.1', user = 'root', passwd = '', db = 'sys')
        cur = conn.cursor()
        cur.execute("insert into book(description) select 'test' from dual where not exists (select 1 from book where id = 7)")
        conn.commit()
        cur.close()
        conn.close()
    except MySQLdb.Error, e:
        print 'MySQL Error: %d %s' % (e.arg[0], e.arg[1])


    一开始就很容易认为是权限不足,依照提示在Linux终端将/home/huayuan/.python-eggs增加读写权限:

    chmod a+rw /home/huayuan/.python-eggs

    结果问题依旧,后来经过反复搜索,终于找到答案,原来不是要增加权限,而是要减掉一些权限,最终有效答案如下:

    chmod g-wx, o-wx /home/huayuan/.python-eggs


关键字