Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Added support to MySQL dump and import using socket #35083

Merged
merged 4 commits into from
Nov 4, 2020
Merged

[8.x] Added support to MySQL dump and import using socket #35083

merged 4 commits into from
Nov 4, 2020

Conversation

eusonlito
Copy link
Contributor

If your MySQL server only allow socket as connection (skip-networking) you can not dump or import the database/schema/mysql-schema.dump file.

This PR check the DB_SOCKET value to generate the mysql/mysqldump command string with --socket instead --host and --port.

I haven't found any test about schema:dump artisan command. Checked on local and servers with MySQL 8 with the different .env DB_ configurations and enabling or disabling network connections on database server.

@selcukcukur
Copy link
Contributor

selcukcukur commented Nov 4, 2020

@eusonlito Does this change the way the code behaves? It is helpful to add the results of whatever tests you have done.

@eusonlito
Copy link
Contributor Author

@selcukcukur no, no changes on behaves. Without DB_SOCKET configured, this process will work as before the code change.

I will add my tests output ASAP.

@taylorotwell
Copy link
Member

What if no unix_socket entry is in the configuration array?

@eusonlito
Copy link
Contributor Author

What if no unix_socket entry is in the configuration array?

Same as without host or port 😅

Ok, let me update code to avoid unix_socket fail without unix_socket key (exists as default in config/database.php).

Also, if I only have unix_socket config value (without host and port) must framework work?

@taylorotwell
Copy link
Member

If they are not truly required we should probably handle cases where they aren't present.

@eusonlito
Copy link
Contributor Author

Socket connections don't need host and port on any way. Also, socket connection is fast than host:port connection: https://www.percona.com/blog/2020/04/13/need-to-connect-to-a-local-mysql-server-use-unix-domain-socket/

@eusonlito
Copy link
Contributor Author

MySQL with --user and --password and commented skip-networking and no dump file

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db
DB_USERNAME=root
DB_PASSWORD=root
✗ php artisan migrate:refresh

Migration table not found.
Migration table created successfully.
Migrating: 2020_02_05_000000_base
Migrated:  2020_02_05_000000_base (2,652.55ms)
✗ php artisan schema:dump

mysqldump: [Warning] Using a password on the command line interface can be insecure.
Database schema dumped successfully.

MySQL with --user and --password and enabled skip-networking and no dump file

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db
DB_USERNAME=root
DB_PASSWORD=root
✗ php artisan migrate:refresh

   Illuminate\Database\QueryException 

  SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = fready and table_name = migrations and table_type = 'BASE TABLE')

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671
    667▕         // If an exception occurs when attempting to run a query, we'll format the error
    668▕         // message to include the bindings with SQL, which will make this exception a
    669▕         // lot more helpful to the developer instead of just the database's errors.
    670▕         catch (Exception $e) {
  ➜ 671▕             throw new QueryException(
    672▕                 $query, $this->prepareBindings($bindings), $e
    673▕             );
    674▕         }
    675▕ 

      +49 vendor frames 
  50  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()
✗ php artisan schema:dump

   Doctrine\DBAL\Driver\PDO\Exception 

  SQLSTATE[HY000] [2002] Connection refused

  at vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDO/Exception.php:18
     14▕ final class Exception extends PDOException
     15▕ {
     16▕     public static function new(\PDOException $exception): self
     17▕     {
  ➜  18▕         return new self($exception);
     19▕     }
     20▕ }
     21▕ 

      +26 vendor frames 
  27  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

MySQL with --socket and enabled skip-networking and no dump file

DB_CONNECTION=mysql
DB_SOCKET=/var/run/mysqld/mysqld.sock
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db
DB_USERNAME=root
DB_PASSWORD=root
✗ php artisan migrate:refresh

Migration table not found.
Migration table created successfully.
Migrating: 2020_02_05_000000_base
Migrated:  2020_02_05_000000_base (2,703.70ms)
✗ php artisan schema:dump

mysqldump: [Warning] Using a password on the command line interface can be insecure.
Database schema dumped successfully.

MySQL with --socket and without DB_HOST and DB_PORT and enabled skip-networking and no dump file

Removed also host and port from config/database.php file.

DB_CONNECTION=mysql
DB_SOCKET=/var/run/mysqld/mysqld.sock
DB_DATABASE=db
DB_USERNAME=root
DB_PASSWORD=root
✗ php artisan migrate:refresh

Migration table not found.
Migration table created successfully.
Migrating: 2020_02_05_000000_base
Migrated:  2020_02_05_000000_base (2,703.70ms)
✗ php artisan schema:dump                                           

   ErrorException 

  Undefined index: host

  at vendor/laravel/framework/src/Illuminate/Database/Schema/MySqlSchemaState.php:118
    114▕     protected function baseVariables(array $config)
    115▕     {
    116▕         return [
    117▕             'LARAVEL_LOAD_SOCKET' => $config['unix_socket'],
  ➜ 118▕             'LARAVEL_LOAD_HOST' => \is_array($config['host']) ? $config['host'][0] : $config['host'],
    119▕             'LARAVEL_LOAD_PORT' => $config['port'],
    120▕             'LARAVEL_LOAD_USER' => $config['username'],
    121▕             'LARAVEL_LOAD_PASSWORD' => $config['password'],
    122▕             'LARAVEL_LOAD_DATABASE' => $config['database'],

      +16 vendor frames 
  17  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

MySQL with --socket and enabled skip-networking and dump file

DB_CONNECTION=mysql
DB_SOCKET=/var/run/mysqld/mysqld.sock
DB_DATABASE=db
DB_USERNAME=root
DB_PASSWORD=root
✗ php artisan migrate:refresh        

Migration table not found.
Migration table created successfully.
Loading stored database schema: /home/www/app/database/schema/mysql-schema.dump
Loaded stored database schema. (1,401.10ms)
Nothing to migrate.
✗ php artisan schema:dump

mysqldump: [Warning] Using a password on the command line interface can be insecure.
Database schema dumped successfully.

@taylorotwell
Copy link
Member

I don't understand what you're getting at. We just need to account in the code for the options being missing.

@eusonlito
Copy link
Contributor Author

Sorry @taylorotwell, @selcukcukur asked about It is helpful to add the results of whatever tests you have done.

@eusonlito
Copy link
Contributor Author

Done!

@eusonlito eusonlito changed the title [8.x] Added suport to MySQL dump and import using socket [8.x] Added support to MySQL dump and import using socket Nov 4, 2020
@selcukcukur
Copy link
Contributor

selcukcukur commented Nov 4, 2020

Done!

@eusonlito I wanted to tell you to add the test files in case there is a code that changes the behavior, I apologize if I am misunderstood 😄

@taylorotwell taylorotwell merged commit 5c1d55a into laravel:8.x Nov 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants