Forums

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

I would like to use MySQL through web.py module and found this code to do this:

db = web.database(dbn = 'mysql', db = 'Ulysses6$default', user = 'Ulysses6', pw = 'NotRealPw')
db.query(some_query)

The thing is, no matter what the query is, code gets stuck on it and raises OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)"). I'm sorry to ask if this is stupid question because I'm begginer to python web programming, but I didn't find any useful information on google and the advised steps to deal with this either dont apply (I dont run server on localhost, I'm sure pythonanywhere doesn't have blocked ports for MySQL or that I would be able to open them, etc) or are more complex and I dont want to screw up my settings.

I think I could work around this by using python sqlite3 module, but thats not really what I want to do. Any help is appreciated

It's just a little disagreement between 2 ways of connecting to the database. Your example assumes that the database is running on the same machine as the web server. On PythonAnywhere, this is not the case. To use your database on PythonAnywhere, just change your db line to:

db = web.database(dbn = 'mysql', db = 'Ulysses6$default', user = 'Ulysses6', pw = 'NotRealPw', host='mysql.server', port=3306)

and everything should work.

Edited: In response to comment below

Thanks a lot! I will try that as soon as I can

Just for the record, the port must be integer, not string. Other than that it works fine, thanks again!

Cool. Thanks for that.

So glad you asked this 'stupid' question because it saved me having to ;)

:-)

Is everything working OK for you now, then?

Yes thanks.

i don't find web.py on app

If you create a web app using the "Manual configuration" option it will have a simple web.py app commented out in the generated WSGI file.

Hi there tried to connect with the below query but keep getting this error

syntax error near unexpected token `('

what Am I doing wrong ?

thks

Please post the entire SQL statement you are trying to execute

mysql> show variables like 'socket'; +---------------+-----------------+ | Variable_name | Value | +---------------+-----------------+ | socket | /tmp/mysql.sock | +---------------+-----------------+ 1 row in set (0.00 sec)

django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")

Use the connection details that are provided on your Databases page to connect to your database.