Rails用データベースの準備
今日はRailsアプリで使うDBを準備します。Railsアプリの作成まで後少しです。
MySQLのインストール
DBは、MySQLの4.0.26を利用することにします。MySQLってあんまり使わないから、コマンドとか良くわからないんですよね・・・。まあ、なんとかなるでしょう。
まずはMySQLのサイトから、インストーラつきのmysql-4.0.26-win32.zipをダウンロードして、適当に解凍。Setup.exeを起動してウィザードどおりに進めれば、インストールは完了。
次に、C:\mysql\bin\winmysqladmin.exeをダブルクリックして実行。
1回目の起動時にユーザIDとパスワードを聞かれるので、任意の値を入力します。
そうするとタスクトレイに信号機のアイコンが表示され、緑が点灯します。
OSがNT系の場合は、このときにNTサービスとして「MySQL」が登録されるみたいです。
うまく接続できるかtest用のDBを使って確認。
C:\>cd C:\mysql\bin C:\mysql\bin>mysql test Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 to server version: 4.0.26-nt Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
OK。終了します。
mysql> exit Bye
データベースの作成
続けてDBを作成します。
Railsでは、本番/開発/テストの3つのDBを使うようなので、DBを3つ作成します。
C:\mysql\bin>mysqladmin -u root create rails-demo_development C:\mysql\bin>mysqladmin -u root create rails-demo_test C:\mysql\bin>mysqladmin -u root create rails-demo_production
うまく作成できたか確認。
C:\mysql\bin>mysqlshow -u root +------------------------+ | Databases | +------------------------+ | mysql | | rails-demo_development | | rails-demo_production | | rails-demo_test | | test | +------------------------+
ちゃんと3つDBが作成されました。
テーブルの作成
次は、usersテーブルを作成します。
C:\mysql\bin>mysql -u root rails-demo_development mysql> create table users ( -> id int unsigned not null auto_increment, -> name varchar(20) not null, -> password varchar(20) not null, -> primary key(id) -> ); Query OK, 0 rows affected (0.09 sec)
テーブルが生成されたか確認。
mysql> show tables; +----------------------------------+ | Tables_in_rails-demo_development | +----------------------------------+ | users | +----------------------------------+ 1 row in set (0.00 sec)
どういう風にRailsで使い分けるのかわかりませんが、とりあえずテスト用DBにもusersテーブルを作成しておきました。
最後にExitして、DBとテーブルの準備完了。
mysql> exit Bye
今日はここでタイムアップです。なかなか先に進みません・・・
#今回参考にした@ITの記事
- http://www.atmarkit.co.jp/flinux/rensai/mysql02/mysql02.html
- http://www.atmarkit.co.jp/flinux/rensai/mysql03/mysql03.html
#今回使ったSQL。コピペできるように残しておきます。
create table users ( id int unsigned not null auto_increment, name varchar(20) not null, password varchar(20) not null, primary key(id) );