laravel / ideas

Issues board used for Laravel internals discussions.
938 stars 28 forks source link

[5.8] Only use ip adress '172.17.0.1' for migration to mysql database. #1540

Closed sh-ogawa closed 5 years ago

sh-ogawa commented 5 years ago

I upgrade laravel 5.7 ->5.8, so I encountered this problem.

.env setting

DB_CONNECTION=mysql
DB_HOST=192.168.70.71
DB_PORT=3306
# php artisan migrate:fresh

   Illuminate\Database\QueryException  : SQLSTATE[HY000] [1045] Access denied for user 'vagrant'@'172.17.0.1' (using password: YES) (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')

  at /var/www/html/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   Doctrine\DBAL\Driver\PDOException::("SQLSTATE[HY000] [1045] Access denied for user 'vagrant'@'172.17.0.1' (using password: YES)")
      /var/www/html/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:31

  2   PDOException::("SQLSTATE[HY000] [1045] Access denied for user 'vagrant'@'172.17.0.1' (using password: YES)")
      /var/www/html/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:27

  Please use the argument -v to see more details.

So, I change config/database.php, but the situation does not change, the same error is vomited.

'mysql' => [
            'driver' => 'mysql',
            'host' => '192.168.70.71', // <- direct input
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            /*
            'options' => array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]),
            */
        ],
sh-ogawa commented 5 years ago

It also happens except for running migration.

When using sqlite driver, then it ran successfully.

sisve commented 5 years ago

172.17.0.1 isn't the ip address you're connecting to, but the address you're connecting from. The user does not have the required permissions.

sh-ogawa commented 5 years ago

Then, when it was 5.7, it was connected normally, and it became such a phenomenon as soon as it was raised to 5.8. What is this?

sisve commented 5 years ago

It's a permission problem at your end. Perhaps the machine that is executing the code changed ip address? Maybe you changed to another project that uses another way to run code? Point is that it's in your end, you can successfully connect to mysql, but mysql says "hey, i don't approve this username and password from that ip address".

sh-ogawa commented 5 years ago

I understand this problem. driver setting maybe change required at read and write.

change host ip address, so change ip address written log file.

mysql' => [
            'driver' => 'mysql',

            'read' => [
                'host' => ['172.17.0.1'],
            ],
            'write' => [
                'host' => ['172.17.0.1'],
            ],
        ],
sh-ogawa commented 5 years ago

I will change the question. What if I want to allow all hosts? I tried to set *, but it got an error.

Patryk27 commented 5 years ago

How did you try to set *? Can you tell us exactly what you did?

sh-ogawa commented 5 years ago

Everything was useless

'read' => [
               'host' => ['*'],
            ],
            'write' => [
               'host' => ['*'],
            ],
'read' => [
               'host' => ['%'],
            ],
            'write' => [
               'host' => ['%'],
            ],
'read' => [
               'host' => '*',
            ],
            'write' => [
               'host' => '*',
            ],
sisve commented 5 years ago

Those look like php code. The problem is permissions on your mysql user. That's defined in mysql, not in your php code.

sh-ogawa commented 5 years ago
mysql> SHOW GRANTS for 'vagrant'@'%';
+---------------------------------------------------+
| Grants for vagrant@%                              |
+---------------------------------------------------+
| GRANT ALL PRIVILEGES ON `hoge`.* TO 'vagrant'@'%' |
+---------------------------------------------------+
1 rows in set (0.00 sec)

now check error log, so change log message. sorry.


SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known (SQL: select * from `users` where `id` = 1 and `users`.`deleted_at` is null limit 1)
sh-ogawa commented 5 years ago

I am creating an environment using docker, but I feel that the docker network construction method is wrong. I confirm soon..

sh-ogawa commented 5 years ago

ver. 5.7 set read/write host in config/database.php -> normal end. ver. 5.7 set read/write host in config/database.php and docker network setting -> normal end.

ver. 5.8 set read/write host in config/database.php -> error end.(and ip address change 172.17.0.1) ver. 5.8 set read/write host in config/database.php and docker network setting -> error end(and ip address change 172.17.0.1).

Now, I read Illuminate\Database\Connection class.

sh-ogawa commented 5 years ago

I can not understand the cause at all.

Exception throws Doctrine\DBAL\Driver\PDOConnection line 27. (Call parent constructor.)

In Doctrine\DBAL\Driver\PDOConnection::_construct(), I tried to compare the parameters passed to the parent constructor, but it was exactly the same thing. It seems that PDO is setting the IP address automatically. Is there any way to quit this?

Laravel 5.7(OK)

$dsn = "mysql:host=192.168.70.71;port=3306;dbname=hoge"
$options = {array} [5]
 8 = 0
 3 = 2
 11 = 0
 17 = false
 20 = false
$password = "vagranthogehoge12e$#"
$user = "vagrant"

Laravel5.8(NG)


$dsn = "mysql:host=192.168.70.71;port=3306;dbname=hoge"
$options = {array} [5]
 8 = 0
 3 = 2
 11 = 0
 17 = false
 20 = false
$password = "vagranthogehoge12e$"
$username = "vagrant"

The operating environment itself, including the Docker container, is not restarting either. Simply updating to 5.8 will result in an error. I have not written anything in the upgrade guide, so I am very embarrassed.

hjelmdal commented 5 years ago

After upgrading to 5.8 I get the exact same error on my webhost.. Not locally.. what is going on? unprofessional of Laravel team to overlook this?!

sisve commented 5 years ago

It sounds like a mismatch between your application code and docker setup. I would start by verifying that the correct username and password is used. Also, make sure to use a certificate if your database requires ssl.

bbashy commented 5 years ago

Great idea, I like it.

sh-ogawa commented 5 years ago

Hmm, is it OK to understand that it is temporary? At least IntelliJ's database plugin and mysql workbench can log in without problems. Only Laravel 5.8 can not log in.

chris-rr commented 5 years ago

I am having the same issue after upgrade to 5.8, not containerised just hosted with Apache. Was working on 5.7, now access denied for any operation on MySQL DB. Can connect to the DB using the credentials in Workbench. Changing .env to connect with root user seems to correct the problem though.

Kindly advise?

chanafdo commented 5 years ago

I had the same issue and it was a # symbol in password.

Laravel 5.8 uses the new phpdotenv package which treats # symbol as start of comment. From upgrade guide.

the # character in an unquoted value will now be considered a comment instead of part of the value.

To preserve the previous behavior, you may wrap the environment values in quotes:

TimoStahl commented 5 years ago

@chanafdo thanks a lot, for me the # in the mysql password fixed it

sh-ogawa commented 5 years ago

@chanafdo thank you very mach. I confirmed now it works properly.

ervin369 commented 5 years ago

@chanafdo - lifesaver - thanks very much!

jellepals commented 5 years ago

Same for me! # in password is killing. Very strange bug!!