How to set up Django MySQL Database on pythonanywhere? - python

It's the first time I'm deploying a site using MySQL instead of SQLite3, so I'm quite new at this, after reading some documentation I found out that I have to change some files like this :
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'my_site_db',
'USER': 'username',
'PASSWORD': 'password',
'HOST': '?', #currently using pythonanywhere (not on localhost)
'PORT': '?',
}
}
manage.py
try:
import pymysql
pymysql.install_as_MySQLdb()
except:
pass
(PyMySQL==0.7.10, Django 1.9, Python 3.5)
When I run the Bash Console using python manage.py migrate I get this error message :
django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)")
I thought it has something to do with the HOST and the PORT from the Database configurations, I don't know what I should add in these, does it have to do with MySQL, PythonAnywhere or even my own domain ? How can I resolve this problem and set up MySQL correctly with Django 3.5 ?

Check this guide. I used it to deploy my webapp.

You can use in this way
Username : johndoe
Database name : socialdatabase
Host : xyz.mysql.pythonanywhere-services.com
User : johndoe
Name : johndoe$socialdatabase
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'my_site_db', # Username$database name
'USER': 'username', #Username:
'PASSWORD': 'password',
'HOST': '?', # Database host address
}
}

Related

Trouble shooting with postgresql to connect with Django | Window 10

I install PostgreSQL version 10
also, install pgAdmin4
During the PostgreSQL installation, as Every developer know default username = Postgres
My PostgreSQL username was Postgres.
installation window asked me the password, so put the new password
Now when I connect PostgreSQL to my Django project
I install this module
pip install psycopg2
I created a new database using pgAdmin4 named 'mydb'
In my Settings.py file
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydb',
'USERNAME':'postgres',
'PASSWORD' : '<my_Password_here>' , # I entered the same password that i provided during postgres installation
'HOST' : 'localhost',
'PORT': '5432',
}
}
postgres username = 'postgres'
where
my Window Username = 'Huzaifa'
ERROR is here
django.db.utils.OperationalError: FATAL: password authentication failed for user "Huzaifa"
Why Postgres using my Window User (username) for authentication
NOTE:
I already set environment variables as following
C:\Program Files\PostgreSQL\10\lib
C:\Program Files\PostgreSQL\10\bin
After Posting this Question. I found what I was mistaking.
There is no Variable name USERNAME. change USERNAME to USER
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydb',
'USERNAME':'postgres', # <-- Here is the Mistake -->
'PASSWORD' : '<my_Password_here>' ,
'HOST' : 'localhost',
'PORT': '5432',
}
}
Change to...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydb',
'USER':'postgres', # <-- Here is the Mistake -->
'PASSWORD' : '<my_Password_here>' ,
'HOST' : 'localhost',
'PORT': '5432',
}
}

Django Database Migration

Hi have a django project a full project now I want to migrate to mysql from the default Sqlite3 which is the default database. I am on a Mac OS and I don't know how to achieve this process. Any one with a complete guide on how to make the switch would be appreciated.
Go to your project's settings file and edit DATABASES with proper database connection
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DB_NAME',
'USER': 'DB_USER',
'PASSWORD': 'DB_PASSWORD',
'HOST': 'localhost',
'PORT': '3306',
}
}
Now open mysql and create database as you give in DATABASE settings.
Go to your project -
./manage makemigrations
./manage migrate
This will create all the tables in the specified database name.

Python, MySQL, Django, Syncdb gives DataBaseError (1046, No database selected)

I have installed mysql, added it to services to start automatically, however when i try to run the command line interface it says that it cannot find mysql.exe. It asks me to browse for it. I am using windows 10 64 bit and mysql Cluster 7.4. I am following instructions and all they say to do is just enter the instruction to the command line like this manage.py syncdb and that is all that is said on the matter. Thanks in advance for any help that i get.
Have you defined your database in settings.py
https://docs.djangoproject.com/en/1.9/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
where engine is your database engine.

Cannot connect MySQL database from django

I am trying to connect my MySql server from Django application , but whenever I am running the command python manage.py runserver it showing the error:
File "/usr/local/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-i686.egg/MySQLdb/connections.py", line 187, in __init__
_mysql_exceptions.OperationalError: (1045, "Access denied for user 'user'#'localhost' (using password: NO)")
But my settings.py connection string is like :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'django_personnel', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': 'personnel', # Not used with sqlite3.
'HOST': '127.0.0.1', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '3306', # Set to empty string for default. Not used with sqlite3.
}
}
But I can easily connect the database from mysql shell :
mysql -u root -ppersonnel django_personnel
Please guide me. Thanks in advance.
It seems that your current user is not having permissions to connect to DB.
Re installing mysql will help
and also
'HOST': '',
'PORT': '',
Leave these two fields empty

Django testing MySQL permission

I am going through the Django tutorial and I've run into some trouble because I'm using MySQL as my database.
When I run
python manage.py test polls
I get
Creating test database for alias 'default'...
Got an error creating the test database: (1044, "Access denied for user 'USER123'#'%' to database 'test_USER123'")
Type 'yes' if you would like to try deleting the test database 'test_USER123', or 'no' to cancel:
This is my settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'USER123', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'USER123',
'PASSWORD': 'PASSWORD',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
I was wondering if anyone would explain to me what's wrong and what's going on. Thank you!

Categories

Resources