Django: remotely access a PythonAnywhere MySQL database
Question:
I have a Django app (Python 3.4, Django 1.7) on PythonAnywhere, along with a MySQL database.
The database is working fine on the deployed app.
However, I cannot get to connect it to the app on my local machine.
The following error is thrown when I run python manage.py runserver
:
django.db.utils.InterfaceError: (2003, “2003: Can’t connect to MySQL server on ‘mysql.server:3306’ (8 nodename nor servname provided, or not known)”, None)
These are the attributes I use:
DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': '<username>$<database_name>',
'USER': '<username>',
'PASSWORD': '<databse_password>',
'HOST': 'pythonanywhere.com'
}
}
I have also tried mysql.server
and mysql.server.pythonanywhere.com
as HOST
without any more luck.
Answers:
I think It’s not possible to connect directly to your mysqlserver instance from remote, for security reason, the port 3306 is blocked.
They suggest to connect through SSH Tunnel, follow this link.
I don’t know If you can do an ssh tunnelling within Django, You should probably write a custom configuration. It’s simpler to install an SSH Tunnel software on your PC and then connect your Django App to localhost on a port You have to choose.
Bye
As per PythonAnyWhere documentation :
-
Open a terminal and run below command.
ssh -L 3333:username.mysql.pythonanywhere-services.com:3306 [email protected]
provide your PAW account login password
replace username with your username.
-
Open another terminal and run below command.
mysql -h 127.0.0.1 –port 3333 -u username -p
provide your mysql password. Available in settings file.
keep terminal 1 open as long as you are working on terminal 2.
Only Paid account have permission to access remoteserver for mysql database
I have a Django app (Python 3.4, Django 1.7) on PythonAnywhere, along with a MySQL database.
The database is working fine on the deployed app.
However, I cannot get to connect it to the app on my local machine.
The following error is thrown when I run python manage.py runserver
:
django.db.utils.InterfaceError: (2003, “2003: Can’t connect to MySQL server on ‘mysql.server:3306’ (8 nodename nor servname provided, or not known)”, None)
These are the attributes I use:
DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': '<username>$<database_name>',
'USER': '<username>',
'PASSWORD': '<databse_password>',
'HOST': 'pythonanywhere.com'
}
}
I have also tried mysql.server
and mysql.server.pythonanywhere.com
as HOST
without any more luck.
I think It’s not possible to connect directly to your mysqlserver instance from remote, for security reason, the port 3306 is blocked.
They suggest to connect through SSH Tunnel, follow this link.
I don’t know If you can do an ssh tunnelling within Django, You should probably write a custom configuration. It’s simpler to install an SSH Tunnel software on your PC and then connect your Django App to localhost on a port You have to choose.
Bye
As per PythonAnyWhere documentation :
-
Open a terminal and run below command.
ssh -L 3333:username.mysql.pythonanywhere-services.com:3306 [email protected]
provide your PAW account login password
replace username with your username.
-
Open another terminal and run below command.
mysql -h 127.0.0.1 –port 3333 -u username -p
provide your mysql password. Available in settings file.
keep terminal 1 open as long as you are working on terminal 2.
Only Paid account have permission to access remoteserver for mysql database