共计 1979 个字符,预计需要花费 5 分钟才能阅读完成。
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 “testcon import n.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()
以上就是本次分享的全部内容,当初想要学习编程的小伙伴欢送关注 Python 技术大本营,获取更多技能与教程。