/var/www/yatta47.log

/var/www/yatta47.log

やったのログ置場です。スクラップみたいな短編が多いかと。

Cloud9のlaravelでMigrationエラー発生時の対処法

f:id:yatta47:20180305205633p:plain

最近勉強を始めたlaravel。

勉強方法はドットインストールで勉強しています。

【旧版】Laravel 5.5入門 (全31回) - プログラミングならドットインストール

とりあえず流し見でみて、概要をつかんでこれから実際に同じものを見て手を動かしていこうとしています。

本日は、Cloud9上でlaravelの勉強中している最中に遭遇したエラーです。

[問題1] could not find driver(Migrationエラー)

php artisan migrate を実行したら could not find driver が発生。

$ php artisan migrate

   Illuminate\Database\QueryException  : could not find driver (SQL: select * from information_schema.tables where table_schema = c9 and table_name = migrations)

  at /home/ubuntu/workspace/laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php: 664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668| 

  Exception trace:

  1   PDOException::("could not find driver")
      /home/ubuntu/workspace/laravel/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php : 68

  2   PDO::__construct("mysql:host=user01-laravel-lesson-5966914;port=3306;dbname=c9", "user01", "", [])
      /home/ubuntu/workspace/laravel/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php : 68

  Please use the argument -v to see more details.

 

ドライバーがないということでドライバーをインストール。

っていっても何を入れるのかどうかわからなかったのでとりあえず見つけたコマンドラインを打ってみる。

$ sudo apt-get install php-mysqlnd
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Package php-mysqlnd is a virtual package provided by:
  php7.2-mysql 7.2.2-3+ubuntu14.04.1+deb.sury.org+1
  php7.1-mysql 7.1.14-1+ubuntu14.04.1+deb.sury.org+1
  php5.6-mysql 5.6.33-3+ubuntu14.04.1+deb.sury.org+1
  php7.0-mysql 7.0.27-1+ubuntu14.04.1+deb.sury.org+1
You should explicitly select one to install.

E: Package 'php-mysqlnd' has no installation candidate
$ 

ふむ。PHPのバージョンによって入れるものが違うのね。違うのをおそらく入れてしまっているのでしょう。

今のphpのバージョンを確認。

$ php -v
PHP 7.1.14-1+ubuntu14.04.1+deb.sury.org+1 (cli) (built: Feb  9 2018 10:06:35) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.1.14-1+ubuntu14.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

使っているPHPのバージョンが7.1なので、それに合わせたドライバを入れる。

$ sudo apt-get install php7.1-mysql
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  php7.1-mysql
0 upgraded, 1 newly installed, 0 to remove and 199 not upgraded.
Need to get 119 kB of archives.
After this operation, 507 kB of additional disk space will be used.
Get:1 http://ppa.launchpad.net/ondrej/php/ubuntu/ trusty/main php7.1-mysql amd64 7.1.14-1+ubuntu14.04.1+deb.sury.org+1 [119 kB]
Fetched 119 kB in 1s (93.4 kB/s)       
Selecting previously unselected package php7.1-mysql.
(Reading database ... 73867 files and directories currently installed.)
Preparing to unpack .../php7.1-mysql_7.1.14-1+ubuntu14.04.1+deb.sury.org+1_amd64.deb ...
Unpacking php7.1-mysql (7.1.14-1+ubuntu14.04.1+deb.sury.org+1) ...
Processing triggers for libapache2-mod-php7.1 (7.1.14-1+ubuntu14.04.1+deb.sury.org+1) ...
Setting up php7.1-mysql (7.1.14-1+ubuntu14.04.1+deb.sury.org+1) ...

Creating config file /etc/php/7.1/mods-available/mysqlnd.ini with new version

Creating config file /etc/php/7.1/mods-available/mysqli.ini with new version

Creating config file /etc/php/7.1/mods-available/pdo_mysql.ini with new version
Processing triggers for libapache2-mod-php7.1 (7.1.14-1+ubuntu14.04.1+deb.sury.org+1) ...

これでアクセスできるようになりました。

 

結論:

sudo apt-get install php7.1-mysql

SQLSTATE[HY000] [2002] Connection refused

やっている最中にConnection refusedも発生。

$ php artisan migrate

   Illuminate\Database\QueryException  : SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = c9 and table_name = migrations)

  at /home/ubuntu/workspace/laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php: 664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668| 

  Exception trace:

  1   PDOException::("SQLSTATE[HY000] [2002] Connection refused")
      /home/ubuntu/workspace/laravel/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php : 68

  2   PDO::__construct("mysql:host=user01-laravel-lesson-5966915;port=3306;dbname=c9", "user01", "", [])
      /home/ubuntu/workspace/laravel/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php : 68

  Please use the argument -v to see more details.

これは、.env での設定により発生しました。

[NGバージョン]

DB_CONNECTION=mysql
DB_HOST=user01-laravel-lesson-5966915
DB_PORT=3306
DB_DATABASE=c9
DB_USERNAME=user01
DB_PASSWORD=

[OKバージョン]

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=c9
DB_USERNAME=user01
DB_PASSWORD=

DB_HOSTを 127.0.0.1 に設定すればOKです。

 

まとめ

ドットインストールの知識をベースにCloud9環境で動かしている最中です。やっぱりいくつか引っ掛かりますねー。そりゃそうか。

 

しかしドットインストール、本当すごい。説明をしながらあのスピードで打てるのは本当すごいと思う。

そして説明の内容もすごくわかりやすい。今回laravelですが、AnsibleとかrailsとかActiveRecordとかめっちゃやっています。

あれで月額1,000円弱だったら全然個人的には納得です。

やった記録もこの部六ぐにどんどん残していこうと思います。

それではー。