python MySQLdb 配置 python連結MYSQL
1、下載 MySQL for Python
wget
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軟體下對應路徑
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
做一個軟連線到/usr/lib64 目錄(64為系統)
ln -s /home/mysql/soft/mysql5717/lib/libmysqlclient.so.20 /usr/lib64/libmysqlclient.so.20
還是有報錯找不到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)")
這個簡單 做個軟連結 大功告成
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
#刪除
#關閉
conn.close()
wget
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軟體下對應路徑
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
做一個軟連線到/usr/lib64 目錄(64為系統)
ln -s /home/mysql/soft/mysql5717/lib/libmysqlclient.so.20 /usr/lib64/libmysqlclient.so.20
還是有報錯找不到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)")
這個簡單 做個軟連結 大功告成
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
#刪除
#關閉
conn.close()
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26526320/viewspace-2142373/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- python & MySQLdb(one)PythonMySql
- Python 3安裝MySQLdbPythonMySql
- Linux:Python報錯:No module named MySQLdbLinuxPythonMySql
- Python 連線 MySQLPythonMySql
- python連線MySQLPythonMySql
- 連結串列(python)Python
- Python目錄連結Python
- 【Python】pymongo連結mongoPythonGo
- python怎麼連線mysqlPythonMySql
- Python3出現"No module named 'MySQLdb'"問題-以及使用PyMySQL連線資料庫PythonMySql資料庫
- Python實現MySQL連線池PythonMySql
- Python連線MySQL資料庫PythonMySql資料庫
- Python 連結串列實踐Python
- 關於 python3 不支援 MySQLdb 的說法PythonMySql
- 【Python】python連結串列應用原始碼示例Python原始碼
- LeetCode-Python-86. 分隔連結串列(連結串列)LeetCodePython
- Python連線MySQL的條條大路PythonMySql
- Python實現單連結串列Python
- python爬蟲之解析連結Python爬蟲
- python 怎麼連線 sql server,不是連線 mysqlPythonServerMySql
- 解決centos7 安裝MySQLdb-python 報錯 方案CentOSMySqlPython
- python+selenium 連線MySQL資料庫PythonMySql資料庫
- python操作MySQL資料庫連線(pymysql)PythonMySql資料庫
- python連線mysql資料庫步驟PythonMySql資料庫
- 連結串列找環(python實現)Python
- python資料結構——連結串列(無序列表)Python資料結構
- python資料插入連線MySQL資料庫PythonMySql資料庫
- Python 連線mysql資料庫進行操作PythonMySql資料庫
- 一個學習Python的好連結Python
- python環境連結Oracle資料庫PythonOracle資料庫
- LeetCode-Python-(206)反轉連結串列LeetCodePython
- Python爬蟲-獲得某一連結下的所有超連結Python爬蟲
- JPA配置mysql連線MySql
- 資料結構-雙向連結串列(Python實現)資料結構Python
- python基礎學習24—-使用pymysql連線mysqlPythonMySql
- 函式計算Python連線SQLServer小結函式PythonSQLServer
- linux 修改預設pip/python版本 軟連結LinuxPython
- Python實現環形連結串列詳解Python
- Python爬蟲批次下載電影連結Python爬蟲