16
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PlayフレームワークでMYSQLの設定を行う

Posted at

MYSQLの設定からマイグレート実行までの手順を記述。

前提条件:
playのアプリケーションが作られていること。
MYSQLが準備されていること。

##ビルドの設定変更

MYSQLドライバが取得できるようにビルドの設定を書き換える。
(property/build.sbt:ビルドの設定ファイル)

次のようにmysqlドライバの記述を追加する。

build.sbt
libraryDependencies ++= Seq(
  jdbc,
  anorm,
  cache,
  "mysql" % "mysql-connector-java" % "5.1.24"
) 

##データベースの接続設定

アプリケーションの設定ファイルを書き換える。
(conf/application.conf:設定ファイル)

下記の記述がされるようにコメントアウトを外し内容を修正する。

application.conf
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://localhost/データベース名"
db.default.user=<ユーザー名>
db.default.password=<パスワード>
db.default.logStatements=true

アプリを起動すると自動的にドライバがダウンロードされる。

$ play run

##マイグレートファイル作成
次のディレクトリを作成する。
conf/evolutions/default

次のファイルパスになるようにSQLファイルを作成する。
conf/evolutions/default/1.sql
※2番目、3番目とファイルを足すときはファイル名の数字を増やしていく。

SQLファイルには下記のような記述を行う。

1.sql
# --- !Ups

create table dummy(
	id integer not null, 
	name varchar(255), 
	created_at datetime not null, 
	updated_at datetime not null, 
	version integer not null
	);

# --- !Downs
  drop table dummy

※Ups以下はマイグレート時に実行されるSQL
※Downs以下はマイグレート差し戻し時に実行されるSQL

##マイグレートの実行

ホームのURLにアクセスするとマイグレートを実行するかきかれるのでボタンを押して実行する。
(http://ホスト名:9000など)

16
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?