はじめに
どうしてもわからないので質問します。
現状
DokcerでCakePHP3.6の開発環境を作成しております。
しかし、Docker上で動いているはずのMySQLにCakePHPから接続ができません。
IntelliJのDatabaseアクセス機能、WorkBenchからはアクセスできます。
docker-composeファイル、CakePHPの設定は以下になります。
これは何が間違っているのでしょうか。
ご存知の方がいらっしゃいましたら、ご教示頂けますと幸いです。
docker-composeファイル
docker
1version: '3.5' 2services: 3 db: 4 image: mysql:5.7 5 environment: 6 MYSQL_ROOT_PASSWORD: root 7 MYSQL_DATABASE: dev 8 MYSQL_USER: user 9 MYSQL_PASSWORD: user 10 volumes: 11 - ./data/mysql/conf.d:/etc/mysql/conf.d 12 - mysql-storage:/var/lib/mysql 13 ports: 14 - "13306:3306" 15 container_name: my_database_mysql 16 17 nginx: 18 image: nginx:mainline-alpine 19 ports: 20 - "10080:80" 21 volumes: 22 - ./data/nginx/conf/conf.d:/etc/nginx/conf.d 23 - ./log/nginx:/var/log/nginx 24 - ./run:/var/run 25 - ./htdocs/myproject:/var/www/html/webroot 26 environment: 27 TZ: "Asia/Tokyo" 28 depends_on: 29 - phpfpm 30 31 phpfpm: 32 build: ./data/php-fpm/ 33 image: phpfpm:7.2 34 environment: 35 TZ: "Asia/Tokyo" 36 volumes: 37 - ./htdocs/:/var/www/html/ 38 - ./log/php-fpm:/var/log/php-fpm 39 depends_on: 40 - db 41 ports: 42 - 8183:10080 43 44volumes: 45 mysql-storage: 46 driver: local
CakePHP
php
1'Datasources' => [ 2 'default' => [ 3 'className' => 'Cake\Database\Connection', 4 'driver' => 'Cake\Database\Driver\Mysql', 5 'persistent' => false, 6 'host' => 'db', 7 /** 8 * CakePHP will use the default DB port based on the driver selected 9 * MySQL on MAMP uses port 8889, MAMP users will want to uncomment 10 * the following line and set the port accordingly 11 */ 12 'port' => '13306', 13 'username' => 'user', 14 'password' => 'user', 15 'database' => 'dev', 16 'encoding' => 'utf8mb4', 17 'timezone' => '+9:00', 18 'flags' => [], 19 'cacheMetadata' => true, 20 'log' => false, 21 22 /** 23 * Set identifier quoting to true if you are using reserved words or 24 * special characters in your table or column names. Enabling this 25 * setting will result in queries built using the Query Builder having 26 * identifiers quoted when creating SQL. It should be noted that this 27 * decreases performance because each query needs to be traversed and 28 * manipulated before being executed. 29 */ 30 'quoteIdentifiers' => false, 31 32 /** 33 * During development, if using MySQL < 5.6, uncommenting the 34 * following line could boost the speed at which schema metadata is 35 * fetched from the database. It can also be set directly with the 36 * mysql configuration directive 'innodb_stats_on_metadata = 0' 37 * which is the recommended value in production environments 38 */ 39 //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'], 40 41 'url' => env('DATABASE_URL', null), 42 ], 43 ],
もちろん'host' => 'db',部分は、locahostや0.0.0.0、127.0.0.0を試しましたがすべて弾かれました。
ブラウザ
http://localhost:10080/
にアクセスると「server error 」が表示されます。
出たログのエラー
CakePHPのエラーです(logs/error.log)
Error: [PDOException] SQLSTATE[HY000] [2002] Connection refused Request URL: /
試しに作ってみた素のPHP
$pdo = new PDO('mysql:dbname=dev;host=db;', 'user', 'user'); var_dump($pdo); die;
↓
object(PDO)[15]
エラーではなさそう。
参考にしたサイト
https://github.com/laradock/laradock/issues/178
https://github.com/docker-library/php/issues/320
http://www.aipacommander.com/entry/2014/05/20/130000
https://stackoverflow.com/questions/40075065/using-docker-i-get-the-error-sqlstatehy000-2002-no-such-file-or-directory
https://qiita.com/Yorinton/items/202cab1b2187ff478ac5