通过Python访问XAMPP mysql [英] Accessing a XAMPP mysql via Python

查看:257
本文介绍了通过Python访问XAMPP mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我过去只使用过sqlite,然后尝试使用mysql.

我已经在Linux(ubuntu)上安装了XAMPP,并且已运行mysql并可以正常运行(至少对于phpMyadmin来说是如此).但是,我在使MySQLdb(python库)正常工作方面遇到了麻烦({使用apt安装此工具}). 确切地说:

 
>>> import MySQLdb
>>> db = MySQLdb.connect(host="localhost",db="opfine")
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  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年,无法通过以下方式连接到本地MySQL服务器: 套接字'/var /run/mysqld/mysqld.sock'(2))

我在猜测

无法通过套接字'/var/run/mysqld/mysqld.sock连接到本地MySQL服务器

表示它期望某种本地安装(即不在XAMPP之内),但我不知道如何进行修改以使其与mysqlXAMMP风格一起工作. /p>

非常感谢您的帮助!

解决方案

记录下来(并感谢Igancio的指点),我发现下面的方法有用(可怕的是我之前没有想到这一点):

db=MySQLdb.connect(
   user="root"
  ,passwd=""
  ,db="my_db"
  ,unix_socket="/opt/lampp/var/mysql/mysql.sock")

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 "<input>", line 1, in <module>
  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")

这篇关于通过Python访问XAMPP mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆