解決python MySQLdb import Error問題

chenfeng發表於2017-01-06
 
今天安裝MySQLmtop的時候報錯,發現沒有安裝python的MySQLdb模組,手動執行報如下錯:
[root@test1 mysqlmtop]# python
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  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.18: cannot open shared object file: No such file or directory
>>> quit
Use quit() or Ctrl-D (i.e. EOF) to exit
>>>

解決方法:
root@test1 mysqlmtop]# ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/
[root@test1 mysqlmtop]#

再次執行測試:
[root@test1 mysqlmtop]# python
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>>
>>> conn= MySQLdb.connect(
...         host='172.16.16.14',
...         port = 3306,
...         user='root',
...         passwd='123456',
...         db ='test',
...         )
>>> cur = conn.cursor()
>>>
>>> cur.execute("select * from chenfeng")
3L
>>>
>>> result = cur.fetchall()
>>> print result
(('aaaa',), ('bbbb',), ('bbbb',))
>>>
>>>


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/15498/viewspace-2132014/,如需轉載,請註明出處,否則將追究法律責任。

相關文章