I created a new EC2 Instance with Postgres on RDS. I confirmed that I can connect from the EC2 instance to the database using psql without any issue which means my security settings are fine.
However, when I try to run manage.py runserver or manage.py dbshell (from the virtualenv) Django hangs then eventually gives a timeout error:
psql: could not connect to server: Connection timed out Is the server
running on host "whatever.rds.amazonaws.com"
(172.xxx.xxx.xxx) and accepting TCP/IP connections on port 5342?
Traceback (most recent call last): File "manage.py", line 22, in
execute_from_command_line(sys.argv) File "/home/ubuntu/Env/xxxx/lib/python3.5/site-packages/django/core/management/init.py",
line 363, in execute_from_command_line
utility.execute()
File "/home/ubuntu/Env/ss2017/lib/python3.5/site-packages/django/core/management/init.py",
line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/ubuntu/Env/ss2017/lib/python3.5/site-packages/django/core/management/base.py",
line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/ubuntu/Env/ss2017/lib/python3.5/site-packages/django/core/management/base.py",
line 330, in execute
output = self.handle(*args, **options)
File "/home/ubuntu/Env/ss2017/lib/python3.5/site-packages/django/core/management/commands/dbshell.py",
line 22, in handle
connection.client.runshell()
File "/home/ubuntu/Env/ss2017/lib/python3.5/site-packages/django/db/backends/postgresql/client.py",
line 66, in runshell
DatabaseClient.runshell_db(self.connection.get_connection_params())
File "/home/ubuntu/Env/ss2017/lib/python3.5/site-packages/django/db/backends/postgresql/client.py",
line 58, in runshell_db
subprocess.check_call(args)
File "/usr/lib/python3.5/subprocess.py", line 581, in check_call
raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command '['psql', '-U', 'db_name',
'-h', 'whatever.rds.amazonaws.com', '-p', '5342', 'user_name']'
returned non-zero exit status 2
I tried creating a new copy of the Django app to see if there were perhaps corrupt files involved, and I played with some changes to my settings.py file, but no luck.
Any ideas?
Edit:
Settings.py (the important bits)
DEBUG = False
ALLOWED_HOSTS = ['localhost', '0.0.0.0', '127.0.0.1', 'compute.amazonaws.com']
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'database_name',
'USER': 'xxxxxxxx',
'PASSWORD': 'xxxxxxxx',
'HOST': 'whatever.us-west-2.rds.amazonaws.com',
'PORT': '5342',
}
}
I ended up creating a local postgres database on the EC2 server to see if any errors come up. When I tried to run makemigrations or migrate to set up the database I got a "relation does not exist" error. The stack trace pointed to a line in one of my views that looked something like this:
some_queryset = Model.objects.all()
some_queryset.delete() # <--- this line was the problem
Even though this is technically an acceptable way to delete all entries in a given table, Django (on the server) did not like the fact that the relationships of the given model did not exist.
I commented out the line and was able to connect to the database without any issues.
So for future readers - if your Django app hangs on runserver, but you can reach the RDS database from the EC2 instance, try setting up a local database to check for any issues.
exit status 2 usually means you've called psql with the wrong arguments. For me, a psql authentication error also triggered this exit code. My problem ended up being a typo in the DBPASSWORD environment variable that settings.DATABASES['default'] was using.
Related
so I'm pretty new to Flask and i've been following a tutorial but I have been getting a strange problem. I have the following code, a python3 script, launched in atom with the hydrogen package enabled and jupyter. I have the virtual environment running in the background in a terminal.
from flask import Flask
app = Flask(__name__)
if __name__ == '__main__':
app.run(debug=True)
So obviously the script is very basic and it's just used to see if i can connect to localhost, however when running it (through Hydrogen) I get the following error:
Serving Flask app 'main'
Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
Running on http://127.0.0.1:5000
Press CTRL+C to quit
Restarting with stat
Traceback (most recent call last):
File "/home/uses12/.local/lib/python3.10/site-packages/ipykernel_launcher.py", line 17, in
app.launch_new_instance()
File "/home/uses12/.local/lib/python3.10/site-packages/traitlets/config/application.py", line 977, in launch_instance
app.initialize(argv)
File "/home/uses12/.local/lib/python3.10/site-packages/traitlets/config/application.py", line 110, in inner
return method(app, *args, **kwargs)
File "/home/uses12/.local/lib/python3.10/site-packages/ipykernel/kernelapp.py", line 666, in initialize
self.init_sockets()
File "/home/uses12/.local/lib/python3.10/site-packages/ipykernel/kernelapp.py", line 307, in init_sockets
self.shell_port = self._bind_socket(self.shell_socket, self.shell_port)
File "/home/uses12/.local/lib/python3.10/site-packages/ipykernel/kernelapp.py", line 244, in _bind_socket
return self._try_bind_socket(s, port)
File "/home/uses12/.local/lib/python3.10/site-packages/ipykernel/kernelapp.py", line 220, in _try_bind_socket
s.bind("tcp://%s:%i" % (self.ip, port))
File "/home/uses12/.local/lib/python3.10/site-packages/zmq/sugar/socket.py", line 232, in bind
super().bind(addr)
File "zmq/backend/cython/socket.pyx", line 568, in zmq.backend.cython.socket.Socket.bind
File "zmq/backend/cython/checkrc.pxd", line 28, in zmq.backend.cython.checkrc._check_rc
zmq.error.ZMQError: Address already in use
Upon looking online, a possible solution was to specify the port in the app. parameters, however no matter what port I specify it ALWAYS says that the address is already in use, leading me to believe that there is some deeper issue. I've done a check to see what services are using those ports and it's always just two instances of python3, I guess because Flask is set to debug. I'm completely lost. Can anyone help? Thanks so much.
I am a fairly new to web developement.
First I deployed a static website on my vps (Ubuntu 16.04) without problem and then I tried to add a blog app to it.
It works well locally with PostgreSQL but I can't make it work on my server.
It seems like it tries to connect to Postgres with my Unix user.
Why would my server try to do that?
I did create a database and a owner via the postgres user, matching the login information in settings.py, I was expecting psycopg2 to try to connect to the database using these login informations:
Settings.py + python-decouple:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': config ('NAME'),
'USER': config ('USER'),
'PASSWORD': config ('PASSWORD'),
'HOST': 'localhost',
'PORT': '',
}
}
This is the error message I get each time I try to ./manage.py migrate
'myportfolio' is my Unix user name, the database username is different:
Traceback (most recent call last):
File "/home/myportfolio/lib/python3.5/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
self.connect()
File "/home/myportfolio/lib/python3.5/site-packages/django/db/backends/base/base.py", line 194, in connect
self.connection = self.get_new_connection(conn_params)
File "/home/myportfolio/lib/python3.5/site-packages/django/db/backends/postgresql/base.py", line 168, in get_new_connection
connection = Database.connect(**conn_params)
File "/home/myportfolio/lib/python3.5/site-packages/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: FATAL: password authentication failed for user "myportfolio"
FATAL: password authentication failed for user "myportfolio"
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "./manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/home/myportfolio/lib/python3.5/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/home/myportfolio/lib/python3.5/site-packages/django/core/management/__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/myportfolio/lib/python3.5/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/myportfolio/lib/python3.5/site-packages/django/core/management/base.py", line 335, in execute
output = self.handle(*args, **options)
File "/home/myportfolio/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 79, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback)
File "/home/myportfolio/lib/python3.5/site-packages/django/db/migrations/executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "/home/myportfolio/lib/python3.5/site-packages/django/db/migrations/loader.py", line 49, in __init__
self.build_graph()
File "/home/myportfolio/lib/python3.5/site-packages/django/db/migrations/loader.py", line 206, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/home/myportfolio/lib/python3.5/site-packages/django/db/migrations/recorder.py", line 61, in applied_migrations
if self.has_table():
File "/home/myportfolio/lib/python3.5/site-packages/django/db/migrations/recorder.py", line 44, in has_table
return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
File "/home/myportfolio/lib/python3.5/site-packages/django/db/backends/base/base.py", line 255, in cursor
return self._cursor()
File "/home/myportfolio/lib/python3.5/site-packages/django/db/backends/base/base.py", line 232, in _cursor
self.ensure_connection()
File "/home/myportfolio/lib/python3.5/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
self.connect()
File "/home/myportfolio/lib/python3.5/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/myportfolio/lib/python3.5/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
self.connect()
File "/home/myportfolio/lib/python3.5/site-packages/django/db/backends/base/base.py", line 194, in connect
self.connection = self.get_new_connection(conn_params)
File "/home/myportfolio/lib/python3.5/site-packages/django/db/backends/postgresql/base.py", line 168, in get_new_connection
connection = Database.connect(**conn_params)
File "/home/myportfolio/lib/python3.5/site-packages/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: FATAL: password authentication failed for user "myportfolio"
FATAL: password authentication failed for user "myportfolio"
I tried to:
delete my django code, re install
delete/purge postgres and reinstall
modify pg_hba.conf local to trust
At one point I did create a django superuser called 'myportfolio' as my unix user: could this have create a problem ?
As per the error, it is clear that the failure is when your Application is trying to postgres and the important part to concentrate is Authentication.
Do these steps to first understand and reproduce the issue.
I assume it as a Linux Server and recommend these steps.
Step 1:
$ python3
>>>import psycopg2
>>>psycopg2.connect("dbname=postgres user=postgres host=localhost password=oracle port=5432")
>>>connection object at 0x5f03d2c402d8; dsn: 'host=localhost port=5432 dbname=postgres user=postgres password=xxx', closed: 0
You should get such a message. This is a success message.
When i use a wrong password, i get this error.
>>>psycopg2.connect("dbname=postgres user=postgres host=localhost password=wrongpassword port=5432")
>>>Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/psycopg2/__init__.py", line 130, in connect conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: FATAL: password authentication failed for user "postgres"
FATAL: password authentication failed for user "postgres"
When there is no entry in pg_hba.conf file, i get the following error.
>>> psycopg2.connect("dbname=postgres user=postgres host=localhost password=oracle port=5432 ")
>>> Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: FATAL: no pg_hba.conf entry for host "::1", user "postgres", database "postgres", SSL on
FATAL: no pg_hba.conf entry for host "::1", user "postgres", database "postgres", SSL off
So, the issue is with password. Check if your password contains any special characters or spaces. if your password has spaces or special characters, use double quotes as i used below.
>>> psycopg2.connect(dbname="postgres", user="postgres", password="passwords with spaces", host="localhost", port ="5432")
If all is good with the above steps and you got success messages, it is very clear that the issue is with your dsn.
Print the values passed to these variables.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': config ('NAME'),
'USER': config ('USER'),
'PASSWORD': config ('PASSWORD'),
'HOST': 'localhost',
'PORT': '',
}
}
Validate if all the values are being substituted appropriately. You may have the correct password for the user but the dsn is not picking the correct password for the user. See if you can print the dsn and validate if the connection string is perfectly being generated. You will get the fix there.
So I was just stuck on this problem and I thought I'd save whoever comes across this post some time by posting the actual commands. This was done on my raspberry pi.
sudo su - postgres
postgres#raspberrypi:~$ psql
postgres=# CREATE DATABASE websitenamehere
postgres=# CREATE USER mywebsiteuser WITH PASSWORD 'Password';
postgres=# GRANT ALL PRIVILEGES ON DATABASE websitenamehere to mywebsiteuser;
postgres=# \q
Done, you have now created a user.
What is setup as user in config ('USER'). Following the error:
FATAL: password authentication failed for user "myportfolio"
user is myportfolio, so you will need to create that user if it does not exist.
I had something similar. My issue was that I did not set the environment variables correctly so it couldn't connect. Ensure that if you go to Edit Configurations, then Environment Variables, and put in your answers in that column.
This problem might also occur if you have some special characters within your password that Postgres cannot cope with (unless you do some special encoding).
Try something like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
For me, I had the wrong port. Additional characters.
This solved for me:
from sqlalchemy import create_engine
connection_string_orig = "postgres://user_with_%34_in_the_string:pw#host:port/db"
connection_string = connection_string_orig.replace("%", "%25")
engine = create_engine(connection_string)
print(engine.url) # should be identical to connection_string_orig
engine.connect()
from:
https://www.appsloveworld.com/coding/python3x/7/flask-alchemy-psycopg2-operationalerror-fatal-password-authentication-fail
I used the following guide so far:
http://docs.wagtail.io/en/v1.9/advanced_topics/performance.html
I added:
WAGTAILSEARCH_BACKENDS = {
'default': {
'BACKEND': 'wagtail.wagtailsearch.backends.elasticsearch',
'INDEX': '{{ project_name }}',
},
}
to my base.py. When I run $ ./manage.py update_index, I get the following output:
Updating backend: default
default: Rebuilding index {{ project_name }}
Traceback (most recent call last):
File "./manage.py", line 12, in <module>
execute_from_command_line(sys.argv)
File "/home/emtr0/Env/emtr0dotcom/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/home/emtr0/Env/emtr0dotcom/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/emtr0/Env/emtr0dotcom/local/lib/python2.7/site-packages/django/core/management/base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/emtr0/Env/emtr0dotcom/local/lib/python2.7/site-packages/django/core/management/base.py", line 345, in execute
output = self.handle(*args, **options)
File "/home/emtr0/Env/emtr0dotcom/local/lib/python2.7/site-packages/wagtail/wagtailsearch/management/commands/update_index.py", line 120, in handle
self.update_backend(backend_name, schema_only=options.get('schema_only', False))
File "/home/emtr0/Env/emtr0dotcom/local/lib/python2.7/site-packages/wagtail/wagtailsearch/management/commands/update_index.py", line 73, in update_backend
index = rebuilder.start()
File "/home/emtr0/Env/emtr0dotcom/local/lib/python2.7/site-packages/wagtail/wagtailsearch/backends/elasticsearch.py", line 613, in start
self.reset_index()
File "/home/emtr0/Env/emtr0dotcom/local/lib/python2.7/site-packages/wagtail/wagtailsearch/backends/elasticsearch.py", line 609, in reset_index
self.index.reset()
File "/home/emtr0/Env/emtr0dotcom/local/lib/python2.7/site-packages/wagtail/wagtailsearch/backends/elasticsearch.py", line 598, in reset
self.delete()
File "/home/emtr0/Env/emtr0dotcom/local/lib/python2.7/site-packages/wagtail/wagtailsearch/backends/elasticsearch.py", line 499, in delete
self.es.indices.delete(self.name)
File "/home/emtr0/Env/emtr0dotcom/local/lib/python2.7/site-packages/elasticsearch/client/utils.py", line 73, in _wrapped
return func(*args, params=params, **kwargs)
File "/home/emtr0/Env/emtr0dotcom/local/lib/python2.7/site-packages/elasticsearch/client/indices.py", line 200, in delete
params=params)
File "/home/emtr0/Env/emtr0dotcom/local/lib/python2.7/site-packages/elasticsearch/transport.py", line 318, in perform_request
status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout)
File "/home/emtr0/Env/emtr0dotcom/local/lib/python2.7/site-packages/elasticsearch/connection/http_urllib3.py", line 123, in perform_request
raise ConnectionError('N/A', str(e), e)
elasticsearch.exceptions.ConnectionError: ConnectionError(<urllib3.connection.HTTPConnection object at 0x7f3b708fa210>: Failed to establish a new connection: [Errno 111] Connection refused) caused by: NewConnectionError(<urllib3.connection.HTTPConnection object at 0x7f3b708fa210>: Failed to establish a new connection: [Errno 111] Connection refused)
Thanks in advance.
EDIT UPDATE:
It looks like my Linode might not have enough RAM. I get the following error when trying to run ES with ./bin/elasticsearch after global install:
OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000085330000, 2060255232, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 2060255232 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /home/emtr0/elasticsearch-5.3.0/hs_err_pid24719.log
Thanks.
For people still having issues with this, make sure your Elasticsearch version and elasticsearch-py version are the same. Both elasticsearch and the py versions need to be installed. That fixed it for me.
sorry I don't have enough reputation to comment directly under your question, I hope this will help you to get things sorted out:
install elasticsearch locally, run it, and then check if it's running (should be on port 9200) using a simple curl http://localhost:9200. If you get a JSON response, it's running 🎉
OR
setup a bonsai (http://bonsai.io) instance in order to have a hosted elasticsearch server running, and just copy-paste the bonsai's server URL into your config, but it can need a bit more settings (SSL etc), and is more limited in term of elasticsearch version
Bonus: if you are a macOS user, installing/running Elastic Search is as easy as brew install elasticsearch (maybe set a specific version though), and then elasticsearch if you don't mind about having it running in the foreground.
I want to connect my Django web app database to my postgresql database I have on my Pythonanywhere paid account. Before coding anything, I just wanted to get everything talking to each other. This is the settings.py DATABASE section from my django app. I'm running Python 3.5 and Django 1.9.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': '[myDatabaseName]',
'USER': '[myUsername]',
'PASSWORD': '[myPassword]',
'HOST': 'xxxxxxxx-xxx.postgres.pythonanywhere-services.com',
'PORT': '10130',
}
}
The HOST and PORT we're both provided from the pythonanywhere.com site under the tab DATABASE and Postgres. I did create my database, username, and password on the postgres console.
I then created a checkedb.py script I found that would check if the connection with the postgres database works.
from django.db import connections
db_conn = connections['default']
try:
c = db_conn.cursor()
except OperationalError:
connected = False
else:
connected = True
This is the error I receive after running this code.
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/django/conf/__init__.py", line 38, in _setup
settings_module = os.environ[ENVIRONMENT_VARIABLE]
File "/usr/lib/python3.4/os.py", line 633, in __getitem__
raise KeyError(key) from None
KeyError: 'DJANGO_SETTINGS_MODULE'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/giraldez/golf/golf/dbcheck.py", line 2, in <module>
db_conn = connections['default']
File "/usr/local/lib/python3.4/dist-packages/django/db/utils.py", line 196, in __getitem__
self.ensure_defaults(alias)
File "/usr/local/lib/python3.4/dist-packages/django/db/utils.py", line 170, in ensure_defaults
conn = self.databases[alias]
File "/usr/local/lib/python3.4/dist-packages/django/utils/functional.py", line 49, in __get__
res = instance.__dict__[self.func.__name__] = self.func(instance)
File "/usr/local/lib/python3.4/dist-packages/django/db/utils.py", line 153, in databases
self._databases = settings.DATABASES
File "/usr/local/lib/python3.4/dist-packages/django/conf/__init__.py", line 54, in __getattr__
self._setup(name)
File "/usr/local/lib/python3.4/dist-packages/django/conf/__init__.py", line 47, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable D
JANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
The directory for my project looks like this
golf/
---golf/
------__init.py__
------dbcheck.py
------settings.py
------urls.py
------wsgi.py
---media/
---static/
---manage.py
You need to setup django first if you are using it as a standalone script. Would have been easier to try with ./manage.py shell. but if you want to test with a standalone script, here goes:
import sys,os
if __name__ == '__main__': # pragma nocover
# Setup environ
sys.path.append(os.getcwd())
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "main.settings_dev")
import django
django.setup()
from django.db import connections
db_conn = connections['default']
try:
c = db_conn.cursor()
except OperationalError:
connected = False
else:
connected = True
The error you are getting is because you need to properly initialize the django environment before you can write custom scripts against it.
The easiest way to solve this is to run a python shell that already has the django configuration loaded, you can do this with python manage.py shell.
Once this shell has loaded, enter your code and it should tell you if the connection is valid or not.
Before I begin please respect that I'm a newbie, ask me what more information you need to solve this problem rather than close the question or something. ;) OK. I am trying to connect my Django developer server to a MySQL database that is being hosted on a remote server. I've installed MySQL and MySQL-Python and have these settings in 'settings.py':
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'database_name', # Or path to database file if using sqlite3.
'USER': 'database_user', # Not used with sqlite3.
'PASSWORD': 'database_password', # Not used with sqlite3.
'HOST': 'db9.subsys.no', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
When i try to start the developer server using python manage.py runserver I get this:
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/django/core/management/commands/runserver.py", line 91, in inner_run
self.validate(display_num_errors=True)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 266, in validate
num_errors = get_validation_errors(s, app)
File "/Library/Python/2.7/site-packages/django/core/management/validation.py", line 103, in get_validation_errors
connection.validation.validate_field(e, opts, f)
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/validation.py", line 14, in validate_field
db_version = self.connection.get_server_version()
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 411, in get_server_version
self.cursor()
File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 306, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 387, in _cursor
self.connection = Database.connect(**kwargs)
File "build/bdist.macosx-10.7-intel/egg/MySQLdb/__init__.py", line 81, in Connect
File "build/bdist.macosx-10.7-intel/egg/MySQLdb/connections.py", line 187, in __init__
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'db9.subsys.no' (60)")
When I changed 'HOST': 'db9.subsys.no' to 'HOST': '' I got this instead:
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/django/core/management/commands/runserver.py", line 91, in inner_run
self.validate(display_num_errors=True)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 266, in validate
num_errors = get_validation_errors(s, app)
File "/Library/Python/2.7/site-packages/django/core/management/validation.py", line 103, in get_validation_errors
connection.validation.validate_field(e, opts, f)
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/validation.py", line 14, in validate_field
db_version = self.connection.get_server_version()
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 411, in get_server_version
self.cursor()
File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 306, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 387, in _cursor
self.connection = Database.connect(**kwargs)
File "build/bdist.macosx-10.7-intel/egg/MySQLdb/__init__.py", line 81, in Connect
File "build/bdist.macosx-10.7-intel/egg/MySQLdb/connections.py", line 187, in __init__
_mysql_exceptions.OperationalError: (1045, "Access denied for user 'database_user'#'localhost' (using password: YES)")
Is it not possible to connect to MySQL using the developer server or do I need to do some kind of configuration on with the database? Reply if you have the answer or need more information.
Big Thanks in Advance!
Three things you should make sure of if you want to connect to a remote MySQL server:
The server is using networking and not sockets (there is no skip-networking line in my.cnf), and the server is listening on the public IP (with bind-address).
Your firewall allows remote access to the MySQL port, which is 3306 by default.
The user is allowed remote access:
GRANT ALL ON someDatabase.* to someUser#some.remote.ip IDENTIFIED BY 'foopass';
Just a quick sanity check - I assume you replaced the placeholder values (e.g. *database_name*) with the actual values?
Also, have you tried connecting to the database over the command line first (on your local development machine - not through some sort of SSH connection to a VPS etc.) to check that it's reachable from wherever you're developing from, e.g. mysql -u - p? If not, it probably means your web hosting company (I'm making an assumption that this isn't your database - please correct me if this is wrong and/or see #Burhan Khalid's answer) doesn't allow external connections to that database server and you won't be able to connect remotely from any software - this has nothing to do with Django. In this case either contact the hosting company to find out what port number to use (or if external database connections are even allowed under their firewall rules) or use a local database for development and then populate the production database from a dump-file of your local database. Here is a tutorial on this process - http://php.about.com/od/learnmysql/ss/mysql_backup.htm.
In my opinion this is a better way of doing things, as it is probably easier and more practical to develop locally and then upload. It can also prevent you from making a costly mistake if that winds up being the live database and you run one of the manage.py commands that can wipe out a table(s)!
You can use the database with the development server.
When you keep the host and port blank, django uses the default port to connect to the database on the local machine. In your case, you are getting an error because the username or password that you are using while connecting to the database on localhost is incorrect.
While connecting to a remote server, please make sure that you are
allowed to access it remotely. MySQL doesnt allow remote connections
by default.
Also, make sure that the port you are trying to connect
on is not blocked by a firewall.
Read this to figure out what you need to do to allow this behaviour: http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html
I hope this helps you figure out the solution.
Try using an IP-Adress instead of 'db9.subsys.no' after the 'HOST': Tag.