Horai Nuri
Horai Nuri

Reputation: 5568

How to set up Django MySQL Database on pythonanywhere?

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 ?

Upvotes: 2

Views: 6815

Answers (2)

Muhammad Faizan Fareed
Muhammad Faizan Fareed

Reputation: 3748

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           

}
}

Upvotes: 0

r0xette
r0xette

Reputation: 908

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

enter image description here

Upvotes: 3

Related Questions