Django with MySQL
After PostgreSQL, MySQL is probably the most popular DBMS (database management system) used with Django. Here is how I set it up
- Install MySQL, etc
- Use the Software Manager to install mysql-server (write down the root password you entered here) and phpmyadmin
- In your browser, go to http://localhost/phpmyadmin. Enter “root” and the password you wrote down
- Note, on my LinuxMint 17 installation I got an error “The mcrypt extension is missing. Please check your PHP configuration”. I used the instructions at http://stackoverflow.com/questions/22721630/the-mcrypt-extension-is-missing-please-check-your-php-configuration to fix this. The commands I used were
sudo apt-get install mcrypt php5-mcrypt
php5enmod mcrypt
sudo service apache2 restart
The first line may not have been necessary. I made the beginner’s mistake of not using “sudo” at the start of the first line, so apache didn’t actually reboot, until I got this right
- Note, on my LinuxMint 17 installation I got an error “The mcrypt extension is missing. Please check your PHP configuration”. I used the instructions at http://stackoverflow.com/questions/22721630/the-mcrypt-extension-is-missing-please-check-your-php-configuration to fix this. The commands I used were
- Create a database and a user for Django (still in phpmyadmin in your browser)
- Second line from the top, click on “Databases”
- Create database, enter the name (e.g. “HelloWorldDjango”)
- For collation, https://docs.djangoproject.com/en/dev/ref/databases/ suggests using utf8_general_ci, but read the note about case sensitivity
- Click on Create
- Second line from the top, click on “Users”
- Click on “Add user”
- Enter the user name – e.g. “DjangoUser” – and write it down
- Set the host to “localhost”
- Enter a password, or click on the “Generate” button to get a random (and fairly secure) password and write it down (or copy it into an editor – you’ll need it soon)
- Leave the rest as it is, and click on “Go”
- Against your new user, click on the “Edit Privileges” link
- Scroll down to the “Database-specific privileges” block, next to “Add privileges on the following database:” click on the “Use text field:” dropdown, and select the new database name (e.g. “HelloWorldDjango”)
- At the top of the first box, next to “Database-specific privileges”, click on “Check All”
- Click on the “Go” button
- Now tell Django to use the database
- Open <dev root>Projects/HelloWorldDjango/HelloWorldDjango/settings.py
- Replace the 6 lines which start with “DATABASES = {” with:
DATABASES = {
‘default’: {
‘ENGINE’: ‘django.db.backends.mysql’,
‘NAME’: ‘HelloWorldDjango’,
‘USER’: ‘DjangoUser’,
‘PASSWORD’: ‘(your password)’,
‘HOST’: ‘localhost’, # Or an IP Address that your DB is hosted on
‘PORT’: ‘3306’,
}
}
- Install python-mysql – so Python/Django can access the MySQL database
- sudo apt-get install libmysqlclient-dev
- sudo apt-get install python-dev
- Switch to the virtual env
source <dev root>/envs/DjangoTest/bin/activate - pip install MySQL-python
- And test that it is all working correctly
- Still in the virtual env (see previous step)
- cd <dev root>Projects/HelloWorldDjango
- python manage.py shell
- >>> from django.db import connection
- >>> cursor = connection.cursor()
- Second line from the top, click on “Databases”
Congratulations. You can now create some Django models to start using the database