A-A+

Linux下Python连接MySQL异常的解决方法

2015年12月28日 站长资讯 暂无评论

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

  1. /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).  
  2.   warnings.warn(msg, UserWarning)  

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

  1. #coding=utf-8  
  2. import MySQLdb  
  3.    
  4. try:  
  5.     conn = MySQLdb.connect(host = '127.0.0.1'user = 'root'passwd = ''db = 'sys')  
  6.     cur = conn.cursor()  
  7.     cur.execute("insert into book(description) select 'test' from dual where not exists (select 1 from book where id = 7)")  
  8.     conn.commit()  
  9.     cur.close()  
  10.     conn.close()  
  11. except MySQLdb.Error, e:  
  12.     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

标签:

给我留言