家裏的電腦使用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