Setting MySQL Database in Django

You must have already installed mysql server and mysql client in your system before doing this tutorial.

Once you have your mysql server running, you may want to hook it up with your Django project and replace the default sqlite database. Sqlite database, a file-based database is good for development but once your application is ready for production it is recommended to use a central database system. First of all you need a python interface for mysql called “mysqlclient”.

pip install mysqlclient

In case you’re getting this error: “EnvironmentError: mysql_config not found”. mysql_config is missing on your system or the installer could not find it. Be sure mysql_config is installed. If not, you can install using

sudo apt-get install libmysqlclient-dev

And then, you need to add the following to your settings.py file

DATABASES = {
'default': {
  'ENGINE': django.db.backends.mysql,
  'NAME': DATABASE_NAME,
  'USER': DATABASE_USER),
  'PASSWORD': DATABASE_PASSWORD),
  'HOST': DATABASE_HOST),
  'PORT': DATABASE_PORT)   
  }
}

If you don’t have the above values, you can look them up from the mysql shell by using the following command:

mysql> show variables;

Leave a Reply

Your email address will not be published. Required fields are marked *