I am trying to do a migration with PHINX in a database with a MySQL container, but getting the following issue.
PDOException: SQLSTATE[HY000] [2002] Connection refused in /var/www/html/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php:86
This is my phinx.php:
<?php
require './vendor/autoload.php';
return
[
'paths' => [
'migrations' => 'database/migrations',
'seeds' => '/database/seeds'
],
'migration_base_class' => '\Migrations\Migration',
'environments' => [
'default_environment' => 'dev',
'default_database' => 'dev',
'dev' => [
'adapter' => 'mysql',
'host' => '127.0.0.1',
'name' => 'db_slim',
'user' => 'db_slim',
'pass' => 'root',
]
]
];
Migration class:
<?php
namespace Migrations;
use Illuminate\Database\Capsule\Manager as Capsule;
use Phinx\Migration\AbstractMigration;
class Migration extends AbstractMigration
{
/** @var \Illuminate\Database\Capsule\Manager $capsule */
public $capsule;
/** @var \Illuminate\Database\Schema\Builder $capsule */
public $schema;
public function init()
{
$this->capsule = new Capsule;
$this->capsule->addConnection([
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'db_slim',
'username' => 'db_slim',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
]);
$this->capsule->bootEloquent();
$this->capsule->setAsGlobal();
$this->schema = $this->capsule->schema();
}
}
Create table instruction:
<?php
declare(strict_types=1);
use Migrations\Migration;
final class CreateUsersTable extends Migration
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$this->schema->create('users', function (Illuminate\Database\Schema\Blueprint $table)
{
$table->increments("id");
$table->string("name");
$table->string("email");
$table->string("password");
$table->timestamps();
});
}
}
Docker container:
mysql:
image: mysql:5.7.29
container_name: mysql_slim
restart: unless-stopped
tty: true
ports:
- "3307:3306"
environment:
MYSQL_DATABASE: db_slim
MYSQL_USER: db_slim
MYSQL_PASSWORD: root
MYSQL_ROOT_PASSWORD: root
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- php-challenge
volumes:
- ./mysql:/var/lib/mysql
I can access the container DB with tools like a workbench, php-storm database tab, I don't know why I try do execute phinx.php migrate is getting the issue, for some reason, I think that my conn values (host, etc) do not work.
I've tried to do the same command migration outside the container (with a local MySQL env) and works, is there another way to solve or connect to the MySQL container?