Part VII.b – Install and Configure MySQL for Django

Now it’s time to configure the database of our project. In this part of the tutorial we will cover MySQL, and in part VII.a we will cover PostgreSQL. Feel free to choose whichever you want! 🙂

However, keep in mind that in a later part of this tutorial we will deploy our app in Heroku, and we will use PostgreSQL. So if you don’t know which database to configure, I would recommend PostgreSQL 😉

The outline of this part is the following:

Let’s start! 🙂

Install MySQL

First, we need to download the MySQL Community Server form the official site. Select your platform and download the file (you will be asked to create an oracle account, but you can skip it by clicking at the link at the bottom of the page).

For a Mac OS X, you should have a dmg file. After clicking on it to install MySQL, you will have to double click on the .pkg file to install it.

Next, we need to modify the $PATH so that the mysql command lines are available in your terminal. Edit the ~/.bash_profile file and add the following:

Close your terminal and open it again.

Next, we need to start mysql:

Next, create a username with a password:

If you need to change the roopt password use:

Note: there is no space between the -p and the old password.

Create a MySQL Database

Next, open mysql using the root account

Now that we are inside the mysql console with root privileges, we will create a database, a user, and grant all privileges to that user:

You can check the grants for a given user with:

Install the MySQL Django adapter, mysqlclient

Next, we need to install a MySQL database adapter for Python: the mysqlclient package. In the development enviroment:

and add it into your requirements/base.txt file as:

Next you have to install it into your testing environment, where you can use:

Configure the Django Database Settings

Now, we need to edit the settings for developing and testing (only local). Edit the files settings/ and settings/ and add the following:

Remember that these files import from the settings/ file, in which we defined the get_env_variable function as:

Next, edit the postactivate file of each environment:

and add the Database settings:

Now edit the predeactivate file of each enviroment and add:

To make these changes effective, you need to deactivate and activate the environments.


Ok, now we are ready to check, sync and migrate our database.

You will be asked to create a super user, so create one now 😉 If you’re not asked to create one, you might want to do so with:

Finally, let’s run our tests to see that everything works as expected!

If you get any of the following erros:

you have to grant the User the correct privileges. To grant them all privileges (including creation and deletion of a database) use:

Hope is everything ok?!! Did you see how useful are tests? Now we know that after changing the database, everything works as good as before 😉

MySQL Workbench

Download MySQL Workbench from the official site and install it in your computer. Next, open the application and select your local instance.

On the left menu you should see your database, here, the taskbuster_db. You can navigate through the tables and elements of your database and see their properties on the bottom-left box.

One think I really like of MySQL Workbench is that it allows me to see the database structure. To do so, go to Database –> Reverse Engineer… in the upper menu. Select the user to connect to the database (for example, the root user), select the taskbuster_db and click continue.

At the end of this wizard you’ll see the structure of your database 🙂


However! From here, you can make changes to the database tables and properties, so be carful because they will interfere with the structure defined in your Django models.


In the next part of this tutorial, we’ll cover User Authentication with social accounts, like for example Google or Twitter.

Keep working! 😉

Don’t forget to share it to your friends, they might find this tutorial helpful! Thanks!


Please, add +Marina Mele in your comments. This way I will get a notification email and I will answer you as soon as possible! :-)