3

I am having troubles connecting to remote MySQL server from Django. The setup uses multiple databases:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'ldatabase',                      # Or path to database file if using sqlite3.
        # The following settings are not used with sqlite3:
        'USER': 'luser',
        'PASSWORD': 'lpass',
        'HOST': '',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '',                      # Set to empty string for default.
    },
    'physics': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'rdatabase',                      # Or path to database file if using sqlite3.
        # The following settings are not used with sqlite3:
        'USER': 'ruser',
        'PASSWORD': 'rpass',
        'HOST': 'hostname',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '3306',                      # Set to empty string for default.
    }
}

the default DB is running on localhost (aka client) machine and physics is on remote server.

I can successfully connect to remote MySQL server with:

> mysql -u ruser -prpass -h hostname
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 34714
Server version: 5.5.29 MySQL Community Server (GPL)
...

or even from python

> python
Python 2.7.3 (default, Aug  9 2012, 17:23:57) 
[GCC 4.7.1 20120720 (Red Hat 4.7.1-5)] on linux2
...
>>> import MySQLdb
>>> con = MySQLdb.connect(host='hostname', port=3306, user='ruser', passwd='rpass', db='rdatabase')
>>> cursor = con.cursor()
>>> cursor.execute('select * from rdatabase.labs')
425L

However connection from Django fails with:

Exception Value: (2003, "Can't connect to MySQL server on 'hostname' (13)")

In fact, I've tested this setup on my laptop: laptop was client and remote MySQL server has the same settings. It works fine.

Please, help with this issue as I have run out of ideas. Maybe there some kind of hidden Django setting on the client machine that I am missing?

Find client and server configurations below.

Thanks.

client setup

> python
Python 2.7.3 (default, Aug  9 2012, 17:23:57) 
[GCC 4.7.1 20120720 (Red Hat 4.7.1-5)] on linux2
...

> pip list
Django (1.5.1)
MySQL-python (1.2.4)
...

> uname --all
Linux <hostname> 3.8.9-200.fc18.x86_64 #1 SMP Fri Apr 26 12:50:07 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

> mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 31
Server version: 5.5.31 MySQL Community Server (GPL)

server setup

> uname --all
Linux <hostname> 3.6.10-2.fc16.x86_64 #1 SMP Tue Dec 11 18:55:03 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

> mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 34712
Server version: 5.5.29 MySQL Community Server (GPL)

> cat /etc/mysql.cnf
[mysqld]
datadir =/var/lib/mysql
socket  =/var/lib/mysql/mysql.sock
#user   =mysql

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
#default-character-set=utf8
max_allowed_packet = 1048576000
[mysqld_safe]
log-error   =/var/log/mysqld.log
pid-file    =/var/run/mysqld/mysqld.pid

[client]
#default-character-set=utf8
Samvel
  • 182
  • 2
  • 6
  • Are you sure you aren't messing up with the names of the databases? Normally you should have `default` as the database you want to connect. Have you tried just leaving one of them? – Paulo Bu May 13 '13 at 18:46
  • Unfortunately, I can not leave only one DB b/c the code extracts entries from the two in the same views. – Samvel May 13 '13 at 21:37
  • Try to leave the PORT setting, the same in both entries. I have had issues with ports and MySql, or be positive sure about which port is listening both and hardcode it – Paulo Bu May 13 '13 at 21:59
  • Sorry, the port setting does not help. – Samvel May 13 '13 at 23:11
  • 1
    This looks like an SELinux issue. How are you running Django? If you're running this under Apache I guess SELinux prevents httpd to create outbound network connections. Try `getsebool -a | grep httpd_can_network_connect` and if that gives a 0/False then `setsebool -P httpd_can_network_connect=1` to enable it. – gertvdijk May 14 '13 at 00:24
  • Thanks gertvdijk for the answer. In fact it was the issue. I had to allow Apache connect to mysql. The issue is solved. More info is available at: http://stackoverflow.com/questions/1792918/weird-mysql-python-mod-wsgi-cant-connect-to-mysql-server-on-localhost-49-pr – Samvel May 14 '13 at 02:52

1 Answers1

3

The problem is that SELinux prevents Apache scripts to connect to network DB by default in Fedora 18 as explained here:

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

The solution is to enable httpd_can_network_connect via:

sudo setsebool -P httpd_can_network_connect_db on
Community
  • 1
  • 1
Samvel
  • 182
  • 2
  • 6