LoginSignup
16
13

More than 5 years have passed since last update.

Travis CI で MySQL を使う

Posted at

概要

Travis CI で MySQL を使う方法を調べました。Travis CI のドキュメントによると、テスト実行時にデフォルトで MySQL のサービスが起動してくれます。ユーザー名は root 、パスワードは空でアクセスできました。

動作試験

下記の .travis.yml とテーブルの一覧を表示するだけの PHP スクリプト show_tables.php で動作試験を行いました。

.travis.yml では before_script でデータベース hoge とテーブル 3 つを作成します。

travis.yml
language: php
before_script:
  - mysql -uroot -e "create database hoge"
  - mysql -uroot -e "use hoge; create table tokyo (id int(11))"
  - mysql -uroot -e "use hoge; create table saitama (id int(11))"
  - mysql -uroot -e "use hoge; create table osaka (id int(11))"
script:
  - php -f show_tables.php

show_tables.php.travis.yml で作成したデータベースからテーブルの一覧を読み出すスクリプトです。

show_tables.php
<?php

$pdo = new PDO(
  'mysql:host=localhost;dbname=hoge;charset=utf8',
  'root',
  ''
);
$stmt = $pdo->query('show tables');
$results = $stmt->fetchAll();
foreach($results as $table_name){
  echo $table_name[0] . PHP_EOL;
}

下記は実行結果です。Travis CI のコンソールで、テーブル名が読み出せていることが確認できました。

$ php -f show_tables.php
osaka
saitama
tokyo

上記の動作試験を行った GitHub のリポジトリです。
https://github.com/suzuki86/sample-of-using-mysql-on-travisci

Travis CI での実行結果です。
https://travis-ci.org/suzuki86/sample-of-using-mysql-on-travisci

16
13
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
13