5
5

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 3 years have passed since last update.

Laravel + Oracleの環境構築

Last updated at Posted at 2020-09-10

はじめに

Laradockを使ってLaravel + Oracleの環境を構築する方法です。

手順

以下は~/laracle 以下に環境を構築する想定のコードです。

$ mkdir ~/laracle

OracleのDocker image作成

まず、oracleで配布されているdocker-imageからOracleのimageを作成します。

$ cd ~/laracle
$ git clone https://github.com/oracle/docker-images.git

以下のURLからOracle Databaseのバイナリをダウンロードします。
https://www.oracle.com/database/technologies/oracle-database-software-downloads.html
ダウンロードしたファイルを~/laracle/docker-images/OracleDatabase/SingleInstance/dockerfiles/以下の、対応するバージョンのディレクトリへ移動します。

ビルドします。

$ cd ~/laracle/docker-images/OracleDatabase/SingleInstance/dockerfiles/
$ ./buildDockerImage.sh -v <oracleのversion> -s -i

以下のようにoracleのイメージが表示されていればOKです。

$ docker images | grep oracle
oracle/database                      <oracle-version>                    784b3e60b30a        2 days ago          8.43GB

Laradockの設定

Laradockをクローンします。

$ cd ~/laracle
$ git clone https://github.com/laradock/laradock.git

docker-compose.yamlのservicesにoracleの設定を追加します。imageのバージョンはダウンロードしたoracleに合わせてください。

~/laracle/laradock/docker-compose.yaml
...
### Oracle ################################################
    oracle:
      image: oracle/database:<oracle-version>
      ports:
        - 1521:1521
      environment:
        ORACLE_SID: ${ORACLE_SID}
        ORACLE_PDB: ${ORACLE_PDB}
        ORACLE_PWD: ${ORACLE_PWD}
        ORACLE_CHARACTERSET: ${ORACLE_CHARACTERSET}
      volumes:
        - ${DATA_PATH_HOST}/oracle:/opt/oracle/oradata
      networks:
        - backend
...

env-exampleにも以下のコードを追加します。

~/laracle/laradock/env-example
...
### Oracle ################################################
ORACLE_SID=ORCLCDB
ORACLE_PDB=ORCLPDB1
ORACLE_PWD=secret
ORACLE_CHARACTERSET=AL32UTF8
...

env-exampleをコピーし、.envの以下の項目の値を書き換えます。

$ cp env-example .env
.env
APP_CODE_PATH_HOST=~/laracle/my_project
DATA_PATH_HOST=~/.laradock/my_project
COMPOSE_PROJECT_NAME=my_project
WORKSPACE_INSTALL_OCI8=true
PHP_FPM_INSTALL_OCI8=true
ORACLE_PWD=<Oracleの初期パスワードに設定したい値>

下記コマンドでコンテナを立ち上げます。oracle18cのコンテナのログを確認し、ORACLE_PWD で設定したパスワードが表示されていることを確認してください。
DATABASE IS READY TO USE! が表示されたら初期化は完了なので、ctrl + cで抜けてしまってOKです。

$ docker-compose up -d workspace php-fpm nginx oracle
$ docker-compose logs -f oracle

Attaching to my_project_oracle_1
oracle_1            | ORACLE PASSWORD FOR SYS, SYSTEM AND PDBADMIN: <ORACLE_PWDに設定した値>
oracle_1            |
oracle_1            | LSNRCTL for Linux: Version 18.0.0.0.0 - Production on 09-SEP-2020 07:56:56
...
...
#########################
DATABASE IS READY TO USE!
#########################
..

OracleのUser作成

Laravelで使用するためのユーザーをOracleに作成します。

$ cd ~/laracle/laradock
$ docker-compose exec oracle bash

# dockerコンテナ内
$ sqlplus system/${ORACLE_PWD}@ORCLPDB1
SQL> show con_name
CON_NAME
------------------------------
ORCLPDB1

SQL> create user laracle_user identified by laracle_pass default tablespace users temporary tablespace temp;
User created.

SQL> grant connect, resource to laracle_user;
Grant succeeded.

SQL> alter user laracle_user quota unlimited on users;
User altered.

SQL> exit
Disconnected from Oracle Database 18c Standard Edition 2 Release 18.0.0.0.0 - Production
Version 18.3.0.0.0

作成したユーザーで接続、テーブルの作成などの操作ができることを確認します。

$ sqlplus laracle_user/laracle_pass@ORCLPDB1

SQL> create table test_table ( id varchar2(8) not null primary key);
Table created.

SQL> insert into test_table (id) values ('01234');
1 row created.

SQL> select * from test_table;
ID
--------
01234

SQL> drop table test_table;
Table dropped.

SQL> exit
Disconnected from Oracle Database 18c Standard Edition 2 Release 18.0.0.0.0 - Production
Version 18.3.0.0.0

LaravelからOracleへの接続

まず、laradockのworkspaceコンテナにアタッチし、Laravelプロジェクトを作成します。

$ cd ~/laracle/laradock
# コンテナにアタッチ
$ docker-compose exec workspace bash

# プロジェクト作成
$ composer create-project laravel/laravel ./

続いて、Oracleに接続するために必要なライブラリのインストールと設定を行います。


$ composer require yajra/laravel-oci8

config/app.php に以下を追加します。

config/app.php
Yajra\Oci8\Oci8ServiceProvider::class,

下記コマンドを実行して設定ファイルを書き出します。

$ php artisan vendor:publish --tag=oracle

最後に.env の以下の項目の値を書き換えます。

.env
DB_CONNECTION=oracle
DB_HOST=oracle
DB_PORT=1521
DB_DATABASE=
DB_SERVICE_NAME=ORCLPDB1
DB_USERNAME=laracle_user
DB_PASSWORD=laracle_pass
DB_CHARSET=AL32UTF8
DB_SERVER_VERSION=18c

DBのマイグレートが実行できれば成功です。


$ artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (0.08 seconds)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (0.02 seconds)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (0.07 seconds)
5
5
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
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?