I have a python script which is designed to add things to a MYSQL database. The error I get is this:

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

I've checked the credentials of the database in the settings and they are correct and I can connect to the database via Sequel.

I installed the following packages:

apt-get -y install build-essential python-dev libxml2-dev libxslt1-dev python-pip libmysqlclient-dev
pip install -U MySQL-python

I'm not really sure how to diagnose this. Is it looking for the mysqld.sock in the right place and it isn't there, or should it instead be looking for it in a different location.

Any help or advice would be much appreciated so I can move forward.

closed as off-topic by dawud, Ward, Falcon Momot, mdpc, Tom O'Connor Aug 19 '13 at 21:49

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions must be relevant to professional system administration. Server Fault is a site dedicated to professionals; novice questions are off-topic. Please see the Help Center for more information on topicality. The best advice we can give you is to hire a professional to help you out." – Ward, Falcon Momot, mdpc, Tom O'Connor
If this question can be reworded to fit the rules in the help center, please edit the question.

  • any reason not to install the python-mysqldb package instead of building it yourself? – dawud Aug 17 '13 at 10:22
  • I'm not sure, it was from a tutorial. Are you saying I can install that as a replacement to "MySQL-python"? I don't want to run a mysql server on my server, I just want to be able to connect to it and add a schema to it – James Willson Aug 17 '13 at 10:33
  • The title of the question says "local MySQL server", yet you say you don't want to run a MySQL server on your server. – dawud Aug 17 '13 at 14:11

A. If mysql is running on same server, find socket file and specify it to the application:

lsof|grep mysql.sock

or connect to the server using IP, not hostname (hostname "localhost" reverts to default socket connection, instead of TCP, defaults to /var/lib/mysql/mysql.sock)

B. If mysql is running on the other server - connection parameters are not passed correctly to your script, check how it must run.

Also, some scripts may read [client] section from ~/.my.cnf

  • I have the MYSQL server externally so when I run lsof|grep mysql.sock it returns nothing. I will look into option B now to make sure. – James Willson Aug 17 '13 at 10:33
  • 1
    There is a way to change default string via ~/.my.cnf, but it's not recommended since all applications run with that user will be able to connect to this database by default, and if password is specified - they will be able to do even more than just "connect". Security. – GioMac Aug 17 '13 at 10:36
  • @JamesWillson What do you mean that you "have the MYSQL server externally"? – Michael Hampton Aug 17 '13 at 10:59
  • good question for clarification. "so when I run lsof|grep mysql.sock it returns nothing", for me keyword is "so", probably he's not running it on localhost. – GioMac Aug 17 '13 at 11:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.