Tuesday, March 12, 2013

Notes on the django 1.5 tutorial01: defining mysql as your database

I'm referring to the instructions found here:
https://docs.djangoproject.com/en/1.5/intro/tutorial01/

If you'd like to use MySQL as your database backend, there are some steps you must do beforehand (besides installing MySQL itself).
  1. Add your database's settings to the settings.py file. While this step is listed on the tutorial, note that you should notice the name, username and password, as you will need to re-use them in the following steps.
    DATABASES = {
        'default':{
            'ENGINE': 'django.db.backends.mysql',
            'NAME':'mysite',
            'USER':'django',
            'PASSWORD':'django',
            'HOST':'',
            'PORT':''
        },
        'original_default': {#I've left their example here
            'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
            'NAME': '',                      # Or path to database file if using sqlite3.
            # The following settings are not used with sqlite3:
            'USER': '',
            'PASSWORD': '',
            'HOST': '',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
            'PORT': '',                      # Set to empty string for default.
        }
    }
    
  2. Install the Python MySQL support:

    #http://stackoverflow.com/questions/2952187/getting-error-loading-mysqldb-module-no-module-named-mysqldb-have-tried-pre
    sudo apt-get install python-mysqldb
    sudo easy_install mysql-python
  3. Create a user/pasword pair, same as you'd put in your site's settings.py file:

    #http://dev.mysql.com/doc/refman/5.1/en/create-user.html
     mysql -uroot -p
    #and in the mysql login:
    #create user '<username>'@'localhost' identified by '<password in plaintext>';
    #for instance:
    CREATE USER 'jeffrey'@'localhost' IDENTIFIED BY 'mypass';
  4. Create a database with the same name you specified in the settings.py file and grant your user the necessary permissions:
    #http://dev.mysql.com/doc/refman/5.1/en/grant.html
    CREATE USER 'jeffrey'@'localhost' IDENTIFIED BY 'mypass';
    GRANT ALL ON db1.* TO 'jeffrey'@'localhost';
As they say in walkthroughs, this will get you as far as the models definition...

I'll post more updates if I'll come across more inconsistencies.