python xampp mysql,通过Python访问XAMPP mysql

I'm attempting to use mysql after only having worked with sqlite in the past.

I've installed XAMPP on Linux (ubuntu) and have mysql up and running fine (seems like that with phpMyadmin at least). However, I'm having trouble getting the MySQLdb (the python lib) working {installed this using apt}.

to be exact:

>>> import MySQLdb

>>> db = MySQLdb.connect(host="localhost",db="opfine")

Traceback (most recent call last):

File "", line 1, in

File "/usr/lib/pymodules/python2.6/MySQLdb/__init__.py", line 81, in Connect

return Connection(*args, **kwargs)

File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 170, in __init_

...

super(Connection, self).__init__(*args, **kwargs2)

OperationalError: (2002, "Can't connect to local MySQL server through

socket '/var

/run/mysqld/mysqld.sock' (2)")

I'm guessing

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock

means its expecting some sort of local installation (i.e. not within XAMPP), but I can't figure out how to go about modding this to get it to work with the XAMMP flavor of mysql.

Help is much appreciated!

解决方案

For the record (and thanks to a pointer from Igancio), I found that the below works (terrible I didn't think of this before):

db=MySQLdb.connect(

user="root"

,passwd=""

,db="my_db"

,unix_socket="/opt/lampp/var/mysql/mysql.sock")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值