Weird MySQL Python mod_wsgi Can't connect to MySQL server on 'localhost' (49) problem
Asked Answered
A

4

5

There have been similar questions on StackOverflow about this, but I haven't found quite the same situation. This is on a OS X Leopard machine using MySQL

Some starting information:

MySQL Server version        5.1.30
Apache/2.2.13 (Unix)
Python 2.5.1
mod_wsgi 3

mysqladmin also has skip-networking listed as OFF

I am able to connect to mysql from the python command line. But when I try to do it through mod_wsgi using code that is copy and pasted or via Django I receive the generic connection refusal

OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (49)")

I've looked at the mysql manual and tried its troubleshooting tips such as

telnet localhost 3306

and I do get a connection.

I am not trying to connect as root, either.

Any ideas on what else I could check? Thanks in advance!

Athiste answered 24/11, 2009 at 21:5 Comment(0)
O
13

I came across this error and it was due to an SELinux denial. /usr/bin/httpd didn't have permission to connect to port 3306. I corrected the issue with:

setsebool httpd_can_network_connect_db on

Seems to work great and should be more secure than just disabling SELinux. As Avinash Meetoo points out below, you can use:

setsebool -P httpd_can_network_connect_db

To make the selinux change persist across reboots.

Overrule answered 5/4, 2011 at 12:50 Comment(2)
Thanks a lot. I'm using MySQL on CentOS Linux 6 with SELinux running in Enforcing mode. The setsebool solved the issue perfectly.Degeneration
setsebool -P httpd_can_network_connect_db on is even better as it is persistent. Without -P, the setsebool does not persist across reboots.Degeneration
M
1

I was getting the exact same error message in Django:

OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (49)")

To fix it, I had to explicitly set the mysql port to 3306 in the django settings file.

Meimeibers answered 20/8, 2012 at 0:25 Comment(0)
R
0

Not too much out there on this. Just a random guess but try using:

DATABASE_HOST = 'localhost' instead of 127.0.0.1

and/or try commenting out in your my.ini:

bind-address 127.0.0.1

worth a shot.

Rizas answered 24/11, 2009 at 23:45 Comment(0)
L
0

Bit odd that the telnet connection works. Maybe some more ways to trouble shot:

shell> perror 49
OS error code  49:  Can't assign requested address

I would check the localhost interface first, check if it has IPv4 address. Far fetched maybe, but I had troubles ones when I didabled IPv6.

shell> ifconfig lo0

Maybe the name resolution doesn't work correctly from within Apache/mod_wsgi/etc..

import python
print socket.gethostbyname('localhost')
print socket.gethostbyaddr('127.0.0.1')

Maybe to get you going (something I contributed to Django) try the UNIX Socket in Django, it works setting the database host to the path (start with forward-slash):

DATABASE_HOST = '/tmp/mysql.sock'

Or where ever your socket file is.

Last, check the MySQL error log if there are any weird messages, like it failing to bind on the IP address or port.

Hope this helps a bit.

Lagan answered 27/1, 2010 at 14:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.