溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python MySQLdb如何配置python鏈接MYSQL

發(fā)布時(shí)間:2021-12-04 16:18:42 來源:億速云 閱讀:207 作者:柒染 欄目:數(shù)據(jù)庫

這篇文章給大家介紹python MySQLdb如何配置python鏈接MYSQL,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

1、下載 MySQL for Python
wget http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz

2、安裝
yum install python-devel-2.7.5-48.el7.x86_64
tar zxvf MySQL-python-1.2.3.tar.gz
$ cd MySQL-python-1.2.3

修改setup_posix.py中mysql_config.path = “mysql_config” 修改為你mysql軟件下對應(yīng)路徑 
mysql_config.path = "/home/mysql/soft/mysql5717/bin/mysql_config"
 
$ python setup.py build
$ python setup.py install

[root@node1 lib]# python testconn.py 
Traceback (most recent call last):
  File "testconn.py", line 3, in <module>
    import MySQLdb    
  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.20: cannot open shared object file: No such file or directory
[root@node1 lib]# find / -name libmysqlclient
[root@node1 lib]# find / -name libmysqlclient.so.20
/home/mysql/soft/mysql5717/lib/libmysqlclient.so.20

做一個(gè)軟連接到/usr/lib64 目錄(64為系統(tǒng))
 ln -s /home/mysql/soft/mysql5717/lib/libmysqlclient.so.20  /usr/lib64/libmysqlclient.so.20

還是有報(bào)錯(cuò)找不到socket

[root@node1 duanfj]# python testconn.py 
Traceback (most recent call last):
  File "testconn.py", line 6, in <module>
    conn=MySQLdb.connect(host="localhost",user="root",passwd="root",db="test",port=3306,charset="utf8")  
  File "build/bdist.linux-x86_64/egg/MySQLdb/__init__.py", line 81, in Connect
  File "build/bdist.linux-x86_64/egg/MySQLdb/connections.py", line 187, in __init__
_mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")

這個(gè)簡單  做個(gè)軟鏈接  大功告成
ln -s /tmp/my3306.sock /tmp/mysql.sock

[root@node1 MySQL-python-1.2.3]# python testconn.py 
1
1
row1
2
row2
3
row
4
row4
[root@node1 MySQL-python-1.2.3]# 
##############
[root@node1 MySQL-python-1.2.3]# cat testconn.py 
# -*- coding: utf-8 -*-     
#mysqldb    
import MySQLdb    
   
#連接    
conn=MySQLdb.connect(host="localhost",user="root",passwd="root",db="test",port=3306,charset="utf8")  
cursor = conn.cursor()    
   
#寫入    
sql = "insert into test(a,b) values(%s,%s)"   
param = (4,"row4")    
n = cursor.execute(sql,param)    
print n    

#查詢    
n = cursor.execute("select * from test")    
for row in cursor.fetchall():    
    for r in row:    
        print r    
   
#刪除    

#關(guān)閉    
conn.close()  

關(guān)于python MySQLdb如何配置python鏈接MYSQL就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI