• Ubuntuに関連したハード、ソフトの備忘録サイトです

Ubuntu14.04LTSでのpython3-mysql.connectorによるDjango1.65の使用

Ubuntu14.04LTSでデータベースとしてMySQLを選択してDjango1.65を使う場合、aptでインストールできるpython3-mysql.connectorを利用すれば良いことが分かりました。

こちらに、http://dev.mysql.com/doc/connector-python/en/connector-python-django-backend.html記してあります。python3 manage.py syncdbの動作も問題ありませんでした。

まずは、python3-mysql.connectorのインストールですが、

$ sudo apt-get install python3-mysql.connector

と普通にインストールします。このパッケージは、/usr/lib/python3/dist-packages/mysqlディレクトリにインストールされますが、このディレクトリにconnectorというディレクトリがあり、その中にdjangoディレクトリがあります。

/usr/lib/python3/dist-packages/mysql/connector/django

このdjangoディレクトリの中身が、/usr/local/lib/python3.4/dist-packages/django/db/backends/mysqlとそっくりです。

michiaki@ubuntu01:/usr/lib/python3/dist-packages/mysql/connector/django$ ls -l
合計 56
-rw-r--r-- 1 root root     0  2月 12 06:37 __init__.py
drwxr-xr-x 2 root root  4096  6月 17 16:42 __pycache__
-rw-r--r-- 1 root root 24090  2月 12 06:37 base.py
-rw-r--r-- 1 root root  1749  2月 12 06:37 client.py
-rw-r--r-- 1 root root  1551  2月 12 06:37 compiler.py
-rw-r--r-- 1 root root  5037  2月 12 06:37 creation.py
-rw-r--r-- 1 root root  6140  2月 12 06:37 introspection.py
-rw-r--r-- 1 root root   904  2月 12 06:37 validation.py

 

michiaki@ubuntu01:/usr/local/lib/python3.4/dist-packages/django/db/backends/mysql$ ls -l
合計 52
-rw-r--r-- 1 root root      0  6月 18 22:59 __init__.py
drwxr-sr-x 2 root staff  4096  6月 18 22:59 __pycache__
-rw-r--r-- 1 root root  22154  6月 18 22:59 base.py
-rw-r--r-- 1 root root   1380  6月 18 22:59 client.py
-rw-r--r-- 1 root root   1305  6月 18 22:59 compiler.py
-rw-r--r-- 1 root root   3158  6月 18 22:59 creation.py
-rw-r--r-- 1 root root   5245  6月 18 22:59 introspection.py
-rw-r--r-- 1 root root    829  6月 18 22:59 validation.py

python3-mysql.connectorは、MySQLを傘下に収めたOracle謹製のパッケージで、Djangoにも対応させたようです。 それはそれとして、django-admin.py startproject mysiteとすると、
mysite/mysite/settings.pyが自動的に作成されますので、このsettings.pyを次のように変更します。

"""
Django settings for mysite project.

For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '+i04!lrfy3*tok9o5npf6)3jgc*g8xr7aa9u7ock@t@m4*r=b2'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'mysite.urls'

WSGI_APPLICATION = 'mysite.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases

DATABASES = {
    'default': {
        # 'ENGINE': 'django.db.backends.sqlite3',
        # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        # 'ENGINE': 'django.db.backends.mysql',
        'ENGINE': 'mysql.connector.django',
        'NAME': 'django_db',
        'USER': 'django',
        'PASSWORD': 'seiban',
        'HOST': 'localhost',
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/

LANGUAGE_CODE = 'ja'

TIME_ZONE = 'Asia/Tokyo'
# TIME_ZONE = 'TOKYO'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

STATIC_URL = '/static/'

とすると、python3 manage.py runserverで開発用のWebサーバーが動いてくれます。そして、python3 manage.py syncdbを実行すると

michiaki@ubuntu01:/srv/Django/mysite$ python3 manage.py syncdb
Creating tables ...
Creating table django_admin_log
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'michiaki'): admin
Email address: nomura@kanto.me
Password: 
Password (again): 
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

と、データベースを自動的に作成してくれました。python3 manage.py runserverで開発用のWebサーバーを起動し、http://localhost:8000/adminとすると、管理者のユーザー名とパスワードを聞いてくるので、上で入力したadminとパスワードを入れると、次のように管理画面が現れます。

django02

 

ということで、MySQLを傘下に収めたOracleがDjango用のMySQLBackEndsを作成してくれたようです。Oracle謹製なので、強い安心感がありますね。

3565OS_0

現在、Djangoの入門書としては「Django 1.5 Application Development Starter」がありますので、本書に従ってpython3-mysql.connectorが使えるかどうか、検証してみようと思っています。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA