Django + MySQL on Elastic Beanstalk - Error When Querying MySQL - python

I'm getting an error when I query MySQL on my Django app hosted on elastic beanstalk. The error says:
OperationalError at /admin/login
(1045, "Access denied for user 'adminDB'#'172.30.23.5' (using password: YES)")
Here is my .config file:
container_commands:
01_migrate:
command: "source /opt/python/run/venv/bin/activate && python manage.py migrate --noinput"
leader_only: true
option_settings:
"aws:elasticbeanstalk:application:environment":
DJANGO_SETTINGS_MODULE: "mysite.settings"
"PYTHONPATH": "/opt/python/current/app/mysite:$PYTHONPATH"
"ALLOWED_HOSTS": ".elasticbeanstalk.com"
"aws:elasticbeanstalk:container:python":
WSGIPath: mysite/wsgi.py
NumProcesses: 3
NumThreads: 20
"aws:elasticbeanstalk:container:python:staticfiles":
"/static/": "www/static/"
Here is my the databases section on settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.environ['RDS_DB_NAME'],
'USER': os.environ['RDS_USERNAME'],
'PASSWORD': os.environ['RDS_PASSWORD'],
'HOST': os.environ['RDS_HOSTNAME'],
'PORT': os.environ['RDS_PORT'],
}
}
I created the eb environment through the eb create command and then proceeded to create an RDS database on the eb console on their website. Is there anything else I need to do to connect Django to MySQL? Something to do with security groups or something?
Thanks!

Thanks for your response #DrewPierce, but the problem was a simple one, and it's also extremely silly. Turns out the dollar sign in my RDS password was causing a problem. Changed it to a simpler password and I successfully logged in.
Hope this helps anyone else with a similar issue!

Related

Unable to makemigrations with PostgreSQL setup

I am trying to setup PostgreSQL locally on my computer, but when I try to initially set up my Django application with it using python manage.py makemigrations, I am given this warning:
RuntimeWarning: Got an error checking a consistent migration
history performed for database connection 'default': fe_sendauth: no password supplied
My database table in my settings.py is as follows:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ.get("DB_NAME"),
'USER': os.environ.get("DB_USER"),
'PASSWORD': os.environ.get("DB_PASSWORD"),
'HOST': os.environ.get("DB_HOST"),
'PORT': os.environ.get("DB_PORT"),
}
}
My .env file is located in the same directory as my manage.py file:
DB_NAME=Smash
DB_PASSWORD=root
DB_USER=SmashDatabase
DB_HOST=localhost
DB_PORT=5432
DEBUG=1
I tried following this link's instructions but none of the offered solutions fixed the problem. I don't know what is wrong or what causes this issue.

Cannot connect to postgresql databse from django settings.py on localhost

Forgive me as I this is my first django project using postgresql (version 11.8). For now I just want to connect to a test database which I have set up locally using pgadmin4. When I create the database I am not given the option to add a password, but when I run python manage.py migrate it is insisting on a password. When I then set the password to "password" on pgadmin, django won't accept it. It's probably something really obvious as I am quite new to Django, but I have tried searching and not found the answer to my problem. In settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'test1',
'USER': 'postgres',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
Last line of the error when I run python manage.py migrate:
django.db.utils.OperationalError: fe_sendauth: no password supplied
Any help much appreciated. Craig
When you installed PgAdmin4 it asked you to create a password. This is for the superuser postgres, which is what you are trying to connect as. Use psql to connect to the database using the above settings and supply the password and see if it works.
I resolved it by removing the Postgresql 11 server and reinstalling it.

Unable to connect docker mysql via django

I'm using docker as MySQL server, in docker I use:
docker run --name what -e MYSQL_ROOT_PASSWORD=abc123 -e MYSQL_DATABASE=mami -p 3306:3306 -d mysql
After that I tested it in MySQL workbench.
It works fine with 192.168.99.100:3306 and root/abc123
but when I try to connect it via django:
import pymysql
pymysql.install_as_MySQLdb()
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mami',
'USER': 'root',
'PASSWORD': 'abc123',
'HOST': '192.168.99.100',
'PORT': '3306',
}
}
It gives me django.db.utils.OperationalError: (1045, "Access denied for user 'root'#'192.168.99.1' (using password: NO)") when I run application or just doing python manage.py migrate
I'm Using Windows and Python 3.6, what am I doing wrong?
Thanks in advance for any help!

Setting up postgresql in Travis CI for django project

I am trying to setup continuous integration for my django project. Since I am using postgresql als my database, I want to setup the same in travis. This is my travis.yml file:
language: python
services:
- postgresql
python:
- 3.4
env:
- DJANGO=1.8 DB=postgres
before_install:
- export DJANGO_SETTINGS_MODULE=ledenbestand.settings.local
install:
- pip install -r travis/requirements.txt
before_script:
- psql -c 'create database ledenbestand;' -U postgres
script:
- python manage.py test
notifications:
email:
recipients:
- random#email.com
on_success: never
on_failure: always
The problem is that this will fail because my local.py settings also gives a password when connecting to the database, the postgres user on travis doesn't have a password:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'ledenbestand',
'USER': 'postgres',
**'PASSWORD': 'password',**
'HOST': 'localhost',
'PORT': '5432',
}
}
The travis documentation says the following:
Should your local test setup use different credentials or settings to access the local test database, we recommend putting these settings in a database.yml.travis in your repository and copying that over as part of your build:
before_script:
- cp config/database.yml.travis config/database.yml
I however have no clue how this could work, how can this line of code overwrite my local.py settings?
Fixed this by adding an If statement in my config file:
if 'TRAVIS' in os.environ:`

Setup and connect to database for Django on AWS

I've currently hosted a Django application on EC2 using Apache.
My database engine
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'subscribe',
'USER': '<username>',
'PASSWORD': '<password>',
'HOST': 'localhost',
'PORT': '3306',
}
}
After setting up Apache, i can now access my web application on the public IP but I cannot perform any DB transcations as the tables don't exist. This is the error message:
ProgrammingError at /some-url
(1146, "Table 'subscribe.subscriberapp_subscriber' doesn't exist")
I know for sure this is because no migrations have been made post deploying to AWS. My question is, how do I setup the DB completely?
You need to run the initial migrations to create the tables. From the console verify that you have a connection to your db by running ./manage.py dbshell. If that works, you have the connection.
Then you need to either run the initial ./manage.py syncdb (for django <1.7) or if you're running django 1.7+ you will run ./manage.py migrate

Categories