Django1.2でINNODBを使いたい場合のsettings.pyの設定方法

Djangoは1.2から複数のデータベースに接続することが可能になりました。

データベースをMySQLInnodbに指定して、構築してみます。

まだあまり設定方法が出ていないようなので、今回確認できている例を載せておきます。

初期のsetting.py

mysqlの設定をくわえてみました。

初期はOPTIONSの設定は入っていません。


DATABASES = {
   'default': {
        'ENGINE': 'mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'hogename',                      # Or path to database file if using sqlite3.
        'USER': 'hogeuser',                      # Not used with sqlite3.
        'PASSWORD': 'hogepass',                  # Not used with sqlite3.
        'HOST': 'xxx.xxx.xxx.xxx',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': 'xxxx',                      # Set to empty string for default. Not used with sqlite3.
    }
}

設定後のsetting.py

OPTIONSを入れてみましょう。

DATABASES = {
   'default': {
        'ENGINE': 'mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'hogename',                      # Or path to database file if using sqlite3.
        'USER': 'hogeuser',                      # Not used with sqlite3.
        'PASSWORD': 'hogepass',                  # Not used with sqlite3.
        'HOST': 'xxx.xxx.xxx.xxx',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': 'xxxx',                      # Set to empty string for default. Not used with sqlite3.
        'OPTIONS': {
            'init_command': 'SET storage_engine=INNODB',
        }
    }
}

'OPTIONS': {
'init_command': 'SET storage_engine=INNODB',
}


を追加しています。


このあと、MySQLでデータベース作成、Django側でsyncdbをすると、INNODBが指定されるはずです。


MySQLのステータス

確認してみましょう。show tableコマンドを使います。

mysql> show table status;
+----------------------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+
| Name                       | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time         | Update_time | Check_time | Collation       | Checksum | Create_options | Comment |
+----------------------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+
| auth_group                 | InnoDB |      10 | Compact    |    0 |              0 |       16384 |               0 |        16384 |  13631488 |              1 | 2011-01-09 03:32:40 | NULL        | NULL       | utf8_general_ci |     NULL |                |         |
| auth_group_permissions     | InnoDB |      10 | Compact    |    0 |              0 |       16384 |               0 |        49152 |  13631488 |              1 | 2011-01-09 03:33:22 | NULL        | NULL       | utf8_general_ci |     NULL |                |         |
| auth_message               | InnoDB |      10 | Compact    |    0 |              0 |       16384 |               0 |        16384 |  13631488 |              1 | 2011-01-09 03:33:22 | NULL        | NULL       | utf8_general_ci |     NULL |                |         |
| auth_permission            | InnoDB |      10 | Compact    |   24 |            682 |       16384 |               0 |        32768 |  13631488 |             25 | 2011-01-09 03:33:22 | NULL        | NULL       | utf8_general_ci |     NULL |                |         |
| auth_user                  | InnoDB |      10 | Compact    |    1 |          16384 |       16384 |               0 |        16384 |  13631488 |              2 | 2011-01-09 03:32:40 | NULL        | NULL       | utf8_general_ci |     NULL |                |         |
| auth_user_groups           | InnoDB |      10 | Compact    |    0 |              0 |       16384 |               0 |        49152 |  13631488 |              1 | 2011-01-09 03:33:22 | NULL        | NULL       | utf8_general_ci |     NULL |                |         |
| auth_user_user_permissions | InnoDB |      10 | Compact    |    0 |              0 |       16384 |               0 |        49152 |  13631488 |              1 | 2011-01-09 03:33:22 | NULL        | NULL       | utf8_general_ci |     NULL |                |         |
| django_admin_log           | InnoDB |      10 | Compact    |    0 |              0 |       16384 |               0 |        32768 |  13631488 |              1 | 2011-01-09 03:33:23 | NULL        | NULL       | utf8_general_ci |     NULL |                |         |
| django_content_type        | InnoDB |      10 | Compact    |    8 |           2048 |       16384 |               0 |        16384 |  13631488 |              9 | 2011-01-09 03:32:40 | NULL        | NULL       | utf8_general_ci |     NULL |                |         |
| django_session             | InnoDB |      10 | Compact    |    0 |              0 |       16384 |               0 |            0 |  13631488 |           NULL | 2011-01-09 03:32:40 | NULL        | NULL       | utf8_general_ci |     NULL |                |         |
| django_site                | InnoDB |      10 | Compact    |    1 |          16384 |       16384 |               0 |            0 |  13631488 |              2 | 2011-01-09 03:32:40 | NULL        | NULL       | utf8_general_ci |     NULL |                |         |
+----------------------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+
11 rows in set (0.00 sec)


完成です。

InnoDBがEngineに登録されているはずです。