PHP動いているんだったらやっぱりDBも使いたいよね。ってことでMySQLを動かしてみようと。
公式のDocumentにはこんな感じで書いてあった。
If you want to add a database to an existing application the command is similar:
$ rhc cartridge-add <dbcartridge> --app <myappname>
This command will also spin up your database server and insert the environment variables.
前回の
Herokuよりも無料枠が良さげなOpenShiftでPHPをデプロイするで
myphpappというアプリ名で作ったので、これで使えるようにしてみる。
まずは、使えるカートリッジを確認する。
$ rhc cartridge list
RSA 1024 bit CA certificates are loaded due to old openssl compatibility
jbossas-7 JBoss Application Server 7 web
jboss-dv-6.1.0 (!) JBoss Data Virtualization 6 web
jbosseap-6 (*) JBoss Enterprise Application Platform 6 web
jboss-unified-push-1 (!) JBoss Unified Push Server 1.0.0.Beta1 web
jboss-unified-push-2 (!) JBoss Unified Push Server 1.0.0.Beta2 web
jenkins-1 Jenkins Server web
nodejs-0.10 Node.js 0.10 web
perl-5.10 Perl 5.10 web
php-5.3 PHP 5.3 web
php-5.4 PHP 5.4 web
zend-6.1 PHP 5.4 with Zend Server 6.1 web
python-2.6 Python 2.6 web
python-2.7 Python 2.7 web
python-3.3 Python 3.3 web
ruby-1.8 Ruby 1.8 web
ruby-1.9 Ruby 1.9 web
ruby-2.0 Ruby 2.0 web
jbossews-1.0 Tomcat 6 (JBoss EWS 1.0) web
jbossews-2.0 Tomcat 7 (JBoss EWS 2.0) web
jboss-vertx-2.1 (!) Vert.x 2.1 web
jboss-wildfly-10 (!) WildFly Application Server 10 web
jboss-wildfly-8 (!) WildFly Application Server 8.2.1.Final web
jboss-wildfly-9 (!) WildFly Application Server 9 web
diy-0.1 Do-It-Yourself 0.1 web
cron-1.4 Cron 1.4 addon
jenkins-client-1 Jenkins Client addon
mongodb-2.4 MongoDB 2.4 addon
mysql-5.1 MySQL 5.1 addon
mysql-5.5 MySQL 5.5 addon
phpmyadmin-4 phpMyAdmin 4.0 addon
postgresql-8.4 PostgreSQL 8.4 addon
postgresql-9.2 PostgreSQL 9.2 addon
rockmongo-1.1 RockMongo 1.1 addon
switchyard-0 SwitchYard 0.8.0 addon
haproxy-1.4 Web Load Balancer addon
Note: Web cartridges can only be added to new applications.
(*) denotes a cartridge with additional usage costs.
(!) denotes a cartridge that will not receive automatic security updates.
mysql-5.5というのがあったので、これが使えそうだ。
なので、作成してみる。
$ rhc cartridge-add mysql-5.5 --app myphpapp
RSA 1024 bit CA certificates are loaded due to old openssl compatibility
Adding mysql-5.5 to application 'myphpapp' ... done
mysql-5.5 (MySQL 5.5)
---------------------
Gears: Located with php-5.4
Connection URL: mysql://$OPENSHIFT_MYSQL_DB_HOST:$OPENSHIFT_MYSQL_DB_PORT/
Database Name: myphpapp
Password: ***********
Username: ***********
MySQL 5.5 database added. Please make note of these credentials:
Root User: ***********
Root Password: ***********
Database Name: myphpapp
Connection URL: mysql://$OPENSHIFT_MYSQL_DB_HOST:$OPENSHIFT_MYSQL_DB_PORT/
You can manage your new MySQL database by also embedding phpmyadmin.
The phpmyadmin username and password will be the same as the MySQL credentials above.
MySQLへの接続は環境変数に入っているようだ。
myphpappにSSHで接続して、
envで確認してみる。
$ rhc ssh myphpapp
\> env | grep OPENSHIFT_MYSQL -
OPENSHIFT_MYSQL_DIR=xxxxxx
OPENSHIFT_MYSQL_DB_PORT=xxxxxx
OPENSHIFT_MYSQL_DB_HOST=xxxxxx
OPENSHIFT_MYSQL_DB_PASSWORD=xxxxxx
OPENSHIFT_MYSQL_IDENT=xxxxxx
OPENSHIFT_MYSQL_DB_USERNAME=xxxxxx
OPENSHIFT_MYSQL_DB_SOCKET=xxxxxx
OPENSHIFT_MYSQL_DB_URL=xxxxxx
OPENSHIFT_MYSQL_VERSION=5.5
OPENSHIFT_MYSQL_DB_LOG_DIR=xxxxxx
OPENSHIFT_MYSQL_LD_LIBRARY_PATH_ELEMENT=xxxxxx
接続に必要な環境変数がいっぱい出てきたので、これを元にSSHでMySQLへログインできるのかやってみる。
\> mysql -u ($OPENSHIFT_MYSQL_DB_USERNAME) -p -h ($OPENSHIFT_MYSQL_DB_HOST)
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.45 MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
普通にデータベースとか見えるのかな。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| myphpapp |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.01 sec)
mysql> use myphpapp;
Database changed
mysql> show tables;
Empty set (0.00 sec)
結構普通に使えて超便利!