openshift4.3でlaravel+MariaDBする

# はじめに

前回までで基本的なLaravelを動作させることができましたが
やっぱりDB無いと話になりませんよね。

ということで前回の環境にMariaDBを追加します！

前回のお話はこちら
[openshift4.3でlaravelする](https://qiita.com/imp555sti/items/ad530f671015b64b0af0)

前提
　- CODEREADY CONTAINERS 1.6が起動していること
　- PHPプロジェクトでLaravelが正常に動作していること

# MariaDBの追加

ProjectにMariaDBを追加していきます。
＋Addから「From Catalog」を選択しフィルタで「MariaDB」と入力します。

![2020-03-08 (12).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/324255/ee2d80e5-a653-5f6c-bd22-6178b972eef3.png)

![2020-03-08 (1).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/324255/5c6a7a5d-78ab-b8cc-c565-779a889f33dc.png)

MariaDBを選択したら指示に従いながらアカウント情報やDB情報を入力していきます。
入力が終わったら「Create」します。

![2020-03-08 (2).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/324255/905fc209-439c-c933-589f-d5bf54c941f1.png)
![2020-03-08 (4).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/324255/6e108971-35bb-73d8-4252-61e1e4af6ddf.png)
![2020-03-08 (5).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/324255/8236ba6f-7e79-8e31-f3ce-48f3555f4a81.png)

ここのDatabase Service Nameが所謂DBHostNameとなります。

![2020-03-08 (6).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/324255/27c817f8-dc53-82db-f878-b5b726c6f9c3.png)

ここまで来たらTopology画面に戻ります。
そうするとPHPの横っちょにMariaDBのContenerが鎮座しています(笑)
Buildが終わって濃い青色になるまで待ちます。

![2020-03-08 (7).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/324255/8caea6f3-d90c-03f8-f6c4-9705606f35a5.png)

![2020-03-08 (8).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/324255/dad1cbbe-440e-80f8-d6c1-aab86984709d.png)

# LaravelとMariaDBを接続する

[https://gitlab.com/imp555/laravel5.8-s2i.git](https://gitlab.com/imp555/laravel5.8-s2i.git)

`.s2i`のソースを一部修正します。といっても`environment`のみでOKなようにしてあります。
それをPHPのソースにPushしたら、PHPのApplicationをリビルドします。

- DB_CONNECTION=mysql
- DB_HOST=*"Database Service Name"*
- DB_PORT=3306
- DB_DATABASE=*"MariaDB Database Name"*
- DB_USERNAME=*"MariaDB Connection Username"*
- DB_PASSWORD=*"MariaDB Connection Password"*


```bash:.s2i/environment
DOCUMENTROOT=/public
SHORT_OPEN_TAG=ON
APP_LOG=errorlog

DB_CONNECTION=mysql
DB_HOST=mariadb
DB_PORT=3306
DB_DATABASE=sampledb
DB_USERNAME=php
DB_PASSWORD=php@1234
```

## PHP Applicationのリビルド

Topology画面からPHPをクリックし、Start Buildをクリックすると、リビルドされます。

![2020-03-08 (9).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/324255/58904b26-a07f-62e7-0be8-692e94f2ea68.png)
![2020-03-08 (10).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/324255/c557f018-3f6d-bda9-6914-641345896f6e.png)

この画面のまま待っていてもよいのですが、心配なのでLog眺めます(笑)
\#2 の View Logsから

![2020-03-08 (11).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/324255/ecc276f9-fe59-01bf-d8e9-68703bc4c43e.png)

## `php artisan migrate`の確認

Buildが完了したらPodの入れ替えが行われます。
入れ替わったPodのView Logsをクリックします。

`setup laravel`のところでMigrateされていますね。
最初にEnvを表示しているのは完全に趣味です。
Build時とDeploy時に環境変数が変わるのでデバック用となります。

```console
﻿*--------------------------------------------------------------------------[ENV]
APP_DATA=/opt/app-root/src
APP_LOG=errorlog
APP_PORT=tcp://172.30.142.243:8080
APP_PORT_8080_TCP=tcp://172.30.142.243:8080
APP_PORT_8080_TCP_ADDR=172.30.142.243
APP_PORT_8080_TCP_PORT=8080
APP_PORT_8080_TCP_PROTO=tcp
APP_PORT_8443_TCP=tcp://172.30.142.243:8443
APP_PORT_8443_TCP_ADDR=172.30.142.243
APP_PORT_8443_TCP_PORT=8443
APP_PORT_8443_TCP_PROTO=tcp
APP_ROOT=/opt/app-root
APP_SERVICE_HOST=172.30.142.243
APP_SERVICE_PORT=8080
APP_SERVICE_PORT_8080_TCP=8080
APP_SERVICE_PORT_8443_TCP=8443
BASH=/bin/bash
BASHOPTS=cmdhist:extquote:force_fignore:hostcomplete:interactive_comments:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=([0]="0")
BASH_SOURCE=([0]="/tmp/scripts/run")
BASH_VERSINFO=([0]="4" [1]="2" [2]="46" [3]="2" [4]="release" [5]="x86_64-redhat-linux-gnu")
BASH_VERSION='4.2.46(2)-release'
DB_CONNECTION=mysql
DB_DATABASE=sampledb
DB_HOST=mariadb
DB_PASSWORD=php@1234
DB_PORT=3306
DB_USERNAME=php
DESCRIPTION='PHP 7.2 available as container is a base platform for building and running various PHP 7.2 applications and frameworks. PHP is an HTML-embedded scripting language. PHP attempts to make it easy for developers to write dynamically generated web pages. PHP also offers built-in database integration for several commercial and non-commercial database management systems, so writing a database-enabled webpage with PHP is fairly simple. The most common use of PHP coding is probably as a replacement for CGI scripts.'
DIRSTACK=()
DOCUMENTROOT=/public
EUID=1000540000
GROUPS=()
HOME=/opt/app-root/src
HOSTNAME=app-74f6889874-ds9zx
HOSTTYPE=x86_64
HTTPD_CONFIGURATION_PATH=/opt/app-root/etc/conf.d
HTTPD_DATA_ORIG_PATH=/opt/rh/httpd24/root/var/www
HTTPD_DATA_PATH=/var/www
HTTPD_MAIN_CONF_D_PATH=/etc/httpd/conf.d
HTTPD_MAIN_CONF_PATH=/etc/httpd/conf
HTTPD_VAR_PATH=/opt/rh/httpd24/root/var
HTTPD_VAR_RUN=/var/run/httpd
IFS=$' \t\n'
KUBERNETES_PORT=tcp://172.30.0.1:443
KUBERNETES_PORT_443_TCP=tcp://172.30.0.1:443
KUBERNETES_PORT_443_TCP_ADDR=172.30.0.1
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_SERVICE_HOST=172.30.0.1
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_PORT_HTTPS=443
LD_LIBRARY_PATH=/opt/rh/rh-nodejs10/root/usr/lib64:/opt/rh/httpd24/root/usr/lib64:/opt/rh/rh-php72/root/usr/lib64
LIBRARY_PATH=/opt/rh/httpd24/root/usr/lib64
MACHTYPE=x86_64-redhat-linux-gnu
MANPATH=/opt/rh/rh-nodejs10/root/usr/share/man:/opt/rh/httpd24/root/usr/share/man:/opt/rh/rh-php72/root/usr/share/man:
MARIADB_PORT=tcp://172.30.91.107:3306
MARIADB_PORT_3306_TCP=tcp://172.30.91.107:3306
MARIADB_PORT_3306_TCP_ADDR=172.30.91.107
MARIADB_PORT_3306_TCP_PORT=3306
MARIADB_PORT_3306_TCP_PROTO=tcp
MARIADB_SERVICE_HOST=172.30.91.107
MARIADB_SERVICE_PORT=3306
MARIADB_SERVICE_PORT_MARIADB=3306
NAME=php
NODEJS_SCL=rh-nodejs10
OPENSHIFT_BUILD_COMMIT=37742889f0427c78eed1855da5bb128a0b7f041f
OPENSHIFT_BUILD_NAME=app-3
OPENSHIFT_BUILD_NAMESPACE=laravel
OPENSHIFT_BUILD_SOURCE=https://gitlab.com/imp555/laravel5.8-s2i.git
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/opt/rh/rh-nodejs10/root/usr/bin:/opt/rh/httpd24/root/usr/bin:/opt/rh/httpd24/root/usr/sbin:/opt/rh/rh-php72/root/usr/bin:/opt/rh/rh-php72/root/usr/sbin:/opt/app-root/src/bin:/opt/app-root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/rh/rh-php72/root/usr/bin
PHP_CONTAINER_SCRIPTS_PATH=/usr/share/container-scripts/php/
PHP_DEFAULT_INCLUDE_PATH=/opt/rh/rh-php72/root/usr/share/pear
PHP_HTTPD_CONF_FILE=rh-php72-php.conf
PHP_SYSCONF_PATH=/etc/opt/rh/rh-php72
PHP_VERSION=7.2
PHP_VER_SHORT=72
PIPESTATUS=([0]="0")
PKG_CONFIG_PATH=/opt/rh/httpd24/root/usr/lib64/pkgconfig
PLATFORM=el7
PPID=0
PS4='+ '
PWD=/opt/app-root/src
PYTHONPATH=/opt/rh/rh-nodejs10/root/usr/lib/python2.7/site-packages
SCL_ENABLED=rh-php72
SHELL=/sbin/nologin
SHELLOPTS=braceexpand:errexit:hashall:interactive-comments
SHLVL=2
SHORT_OPEN_TAG=ON
STI_SCRIPTS_PATH=/usr/libexec/s2i
STI_SCRIPTS_URL=image:///usr/libexec/s2i
SUMMARY='Platform for building and running PHP 7.2 applications'
TERM=xterm
UID=1000540000
X_SCLS='rh-nodejs10 httpd24 rh-php72 '
_='*--------------------------------------------------------------------------[ENV]'
container=oci
*----------------------------------------------------------------[setup laravel]
Migrating database 'php artisan migrate --force'...
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (0.36 seconds)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (0.27 seconds)
Create the symbolic link 'php artisan storage:link'...
The [public/storage] directory has been linked.
*--------------------------------------------------------------[exec run script]
=> sourcing 20-copy-config.sh ...
---> 12:18:02     Processing additional arbitrary httpd configuration provided by s2i ...
=> sourcing 00-documentroot.conf ...
=> sourcing 50-mpm-tuning.conf ...
=> sourcing 40-ssl-certs.sh ...
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 10.128.0.53. Set the 'ServerName' directive globally to suppress this message
[Sun Mar 08 12:18:04.150285 2020] [ssl:warn] [pid 1] AH01909: 10.128.0.53:8443:0 server certificate does NOT include an ID which matches the server name
[Sun Mar 08 12:18:04.332619 2020] [ssl:warn] [pid 1] AH01909: 10.128.0.53:8443:0 server certificate does NOT include an ID which matches the server name
[Sun Mar 08 12:18:04.333001 2020] [http2:warn] [pid 1] AH10034: The mpm module (prefork.c) is not supported by mod_http2. The mpm determines how things are processed in your server. HTTP/2 has more demands in this regard and the currently selected mpm will just not do. This is an advisory warning. Your server will continue to work, but the HTTP/2 protocol will be inactive.
[Sun Mar 08 12:18:04.335446 2020] [lbmethod_heartbeat:notice] [pid 1] AH02282: No slotmem from mod_heartmonitor
[Sun Mar 08 12:18:04.565113 2020] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.34 (Red Hat) OpenSSL/1.0.2k-fips configured -- resuming normal operations
[Sun Mar 08 12:18:04.565309 2020] [core:notice] [pid 1] AH00094: Command line: 'httpd -D FOREGROUND'
```

これで開発できる環境が整いました！