当前位置: 首页 > 后端技术 > Python

pythonMySQLdb配置python链接MYSQL

时间:2023-03-26 15:12:10 Python

1、下载MySQLforPythonwget2,安装yuminstallpython-devel-2.7.5-48.el7.x86_64tarzxvfMySQL-python-1.2.3.tar.gz$cdMySQL-python-1.2.3修改setup_posix中的mysql_config。py.path="mysql_config"改成你mysql软件下对应的路径mysql_config.path="/home/mysql/soft/mysql5717/bin/mysql_config"$pythonsetup.pybuild$pythonsetup.pyinstall[root@node1lib]#pythontestconn.pyTraceback(最近调用last):文件“testconimportn.py”,第3行,在importMySQLdb文件“build/bdist.linux-x86_64/egg/MySQLdb/__init__.py“,第19行,在文件“build/bdist.linux-x86_64/egg/_mysql.py”中,第7行,在文件“build/bdist.linux-x86_64/egg/_mysql.py”中,第6行,在bootstrapImportError:libmysqlclient.so.20:cannotopensharedobjectfile:Nosuchfileordirectory[root@node1lib]#find/-namelibmysqlclient[root@node1lib]#find/-namelibmysqlclient.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@node1duanfj]#pythontestconn.pyTraceback(最近调用last):文件“testconn.py”,第6行,在conn=MySQLdb.connect(host="localhost",user="root",passwd="root",db="test",port=3306,charset="utf8")文件“build/bdist.linux-x86_64/egg/MySQLdb/__init__.py”,第81行,连接文件“build/bdist.linux-x86_64/egg/MySQLdb/connections.py”,行187,在init_mysql_exceptions.OperationalError:(2002,"Can'tconnecttolocalMySQLserverthroughsocket'/tmp/mysql.sock'(2)")这个简单的软链接完成ln-s/tmp/my3306.sock/tmp/mysql.sock[root@node1MySQL-python-1.2.3]#pythontestconn.py11row12row23row4row4[root@node1MySQL-python-1.2.3]#[root@node1MySQL-python-1.2.3]#cattestconn.py编码:utf-8-*-mysqldbimportMySQLdbconnectionconn=MySQLdb.connect(host="localhost",user="root",passwd="root",db="test",port=3306,charset="utf8")cursor=conn.cursor()writesql="insertintotest(a,b)values(%s,%s)"param=(4,"row4")n=cursor.execute(sql,param)printn查询n=cursor.execute("select*fromtest")forrowincursor.fetchall():forrinrow:printrdeletecloseconn.close()以上就是本次分享的全部内容,现在我要欢迎学习编程的朋友关注Python技术大本营获取更多技能和教程。