Accessing a XAMPP mysql via Python

2024/11/18 10:43:27

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 Connectreturn 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 throughsocket '/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!

Answer

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")
https://en.xdnf.cn/q/71070.html

Related Q&A

How to use deep learning models for time-series forecasting?

I have signals recorded from machines (m1, m2, so on) for 28 days. (Note: each signal in each day is 360 length long).machine_num, day1, day2, ..., day28 m1, [12, 10, 5, 6, ...], [78, 85, 32, 12, ...],…

Python - Create Shortcut with arguments

Using win32com.client, Im attempting to create a simple shortcut in a folder. The shortcut however I would like to have arguments, except I keep getting the following error.Traceback (most recent call …

How to replace a range of values with NaN in Pandas data-frame?

I have a huge data-frame. How should I replace a range of values (-200, -100) with NaN?

django 1.10 Exception while resolving variable is_popup in template admin/login.html

I create a new django project with python3.5 and django1.10.0,I keep getting an error in the admin whenever I want access localhost:8000/admin, He`res the error:[DEBUG]- Exception while resolving varia…

Programmatically extract data from an Excel spreadsheet

Is there a simple way, using some common Unix scripting language (Perl/Python/Ruby) or command line utility, to convert an Excel Spreadsheet file to CSV? Specifically, this one:http://www.econ.yale.e…

Is it possible to specify the driver dll directly in the ODBC connection string?

Im trying to use pyodbc to connect to a SQL Server (MS SQL Server through FreeTDS) in a portable application; since its supposed to be standalone, I would like to avoid having to explicitly install the…

Escape New line character in Spark CSV read

Im working on Spark 2.2.1 version and using the below python code, I can able to escape special characters like @ : I want to escape the special characters like newline(\n) and carriage return(\r). I r…

How can a class that inherits from a NumPy array change its own values?

I have a simple class that inherits from the NumPy n-dimensional array. I want to have two methods of the class that can change the array values of an instance of the class. One of the methods should s…

Python/SQL Alchemy Migrate - ValueError: too many values to unpack when migrating changes in db

I have several models in SQLAlchemy written and I just started getting an exception when running my migrate scripts: ValueError: too many values to unpackHere are my models:from app import dbROLE_USER …

How to store Dataframe data to Firebase Storage?

Given a pandas Dataframe which contains some data, what is the best to store this data to Firebase?Should I convert the Dataframe to a local file (e.g. .csv, .txt) and then upload it on Firebase Stora…