1 PDOException ::(" SQLSTATE [HY000] [2002]无法建立连接,因为目标计算机主动拒绝它

时间:2018-03-07 21:45:42

标签: php mysql laravel-5.6

运行时出错:

  

php artisan migrate:reset

在我的Laravel网站的根目录中:

  1   PDOException::("SQLSTATE[HY000] [2002] No connection could be made because
 the target machine actively refused it.
")
      C:\Users\jarro\Documents\Sites\homestead\vendor\laravel\framework\src\Illu
minate\Database\Connectors\Connector.php : 68

  2   PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=homestead", "homes
tead", "secret", [])
      C:\Users\jarro\Documents\Sites\homestead\vendor\laravel\framework\src\Illu
minate\Database\Connectors\Connector.php : 68

  Please use the argument -v to see more details.

当我跑-v时我得到了这个:

  1   PDOException::("SQLSTATE[HY000] [2002] No connection could be made because
 the target machine actively refused it.
")
      C:\Users\jarro\Documents\Sites\homestead\vendor\laravel\framework\src\Illu
minate\Database\Connectors\Connector.php : 68

  2   PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=homestead", "homes
tead", "secret", [])
      C:\Users\jarro\Documents\Sites\homestead\vendor\laravel\framework\src\Illu
minate\Database\Connectors\Connector.php : 68

  3   Illuminate\Database\Connectors\Connector::createPdoConnection("mysql:host=
127.0.0.1;port=3306;dbname=homestead", "homestead", "secret", [])
      C:\Users\jarro\Documents\Sites\homestead\vendor\laravel\framework\src\Illu
minate\Database\Connectors\Connector.php : 44

  4   Illuminate\Database\Connectors\Connector::createConnection("mysql:host=127
.0.0.1;port=3306;dbname=homestead", [])
      C:\Users\jarro\Documents\Sites\homestead\vendor\laravel\framework\src\Illu
minate\Database\Connectors\MySqlConnector.php : 24

  5   Illuminate\Database\Connectors\MySqlConnector::connect()
      C:\Users\jarro\Documents\Sites\homestead\vendor\laravel\framework\src\Illu
minate\Database\Connectors\ConnectionFactory.php : 183

  6   Illuminate\Database\Connectors\ConnectionFactory::Illuminate\Database\Conn
ectors\{closure}()
      C:\Users\jarro\Documents\Sites\homestead\vendor\laravel\framework\src\Illu
minate\Database\Connection.php : 915

  7   call_user_func(Object(Closure))
      C:\Users\jarro\Documents\Sites\homestead\vendor\laravel\framework\src\Illu
minate\Database\Connection.php : 915

  8   Illuminate\Database\Connection::getPdo()
      C:\Users\jarro\Documents\Sites\homestead\vendor\laravel\framework\src\Illu
minate\Database\Connection.php : 940

  9   Illuminate\Database\Connection::getReadPdo()
      C:\Users\jarro\Documents\Sites\homestead\vendor\laravel\framework\src\Illu
minate\Database\Connection.php : 399

  10  Illuminate\Database\Connection::getPdoForSelect()
      C:\Users\jarro\Documents\Sites\homestead\vendor\laravel\framework\src\Illu
minate\Database\Connection.php : 325

  11  Illuminate\Database\Connection::Illuminate\Database\{closure}("select * fr
om information_schema.tables where table_schema = ? and table_name = ?")
      C:\Users\jarro\Documents\Sites\homestead\vendor\laravel\framework\src\Illu
minate\Database\Connection.php : 657

  12  Illuminate\Database\Connection::runQueryCallback("select * from informatio
n_schema.tables where table_schema = ? and table_name = ?", Object(Closure))
      C:\Users\jarro\Documents\Sites\homestead\vendor\laravel\framework\src\Illu
minate\Database\Connection.php : 624

  13  Illuminate\Database\Connection::run("select * from information_schema.tabl
es where table_schema = ? and table_name = ?", Object(Closure))
      C:\Users\jarro\Documents\Sites\homestead\vendor\laravel\framework\src\Illu
minate\Database\Connection.php : 333

  14  Illuminate\Database\Connection::select("select * from information_schema.t
ables where table_schema = ? and table_name = ?")
      C:\Users\jarro\Documents\Sites\homestead\vendor\laravel\framework\src\Illu
minate\Database\Schema\MySqlBuilder.php : 18

  15  Illuminate\Database\Schema\MySqlBuilder::hasTable("migrations")
      C:\Users\jarro\Documents\Sites\homestead\vendor\laravel\framework\src\Illu
minate\Database\Migrations\DatabaseMigrationRepository.php : 169

  16  Illuminate\Database\Migrations\DatabaseMigrationRepository::repositoryExis
ts()
      C:\Users\jarro\Documents\Sites\homestead\vendor\laravel\framework\src\Illu
minate\Database\Migrations\Migrator.php : 545

  17  Illuminate\Database\Migrations\Migrator::repositoryExists()
      C:\Users\jarro\Documents\Sites\homestead\vendor\laravel\framework\src\Illu
minate\Database\Console\Migrations\ResetCommand.php : 63

  18  Illuminate\Database\Console\Migrations\ResetCommand::handle()
      C:\Users\jarro\Documents\Sites\homestead\vendor\laravel\framework\src\Illu
minate\Container\BoundMethod.php : 29

  19  call_user_func_array([])
      C:\Users\jarro\Documents\Sites\homestead\vendor\laravel\framework\src\Illu
minate\Container\BoundMethod.php : 29

  20  Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
      C:\Users\jarro\Documents\Sites\homestead\vendor\laravel\framework\src\Illu
minate\Container\BoundMethod.php : 87

  21  Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Founda
tion\Application), Object(Closure))
      C:\Users\jarro\Documents\Sites\homestead\vendor\laravel\framework\src\Illu
minate\Container\BoundMethod.php : 31

  22  Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Applic
ation), [])
      C:\Users\jarro\Documents\Sites\homestead\vendor\laravel\framework\src\Illu
minate\Container\Container.php : 564

  23  Illuminate\Container\Container::call()
      C:\Users\jarro\Documents\Sites\homestead\vendor\laravel\framework\src\Illu
minate\Console\Command.php : 183

  24  Illuminate\Console\Command::execute(Object(Symfony\Component\Console\Input
\ArgvInput), Object(Illuminate\Console\OutputStyle))
      C:\Users\jarro\Documents\Sites\homestead\vendor\symfony\console\Command\Co
mmand.php : 252

  25  Symfony\Component\Console\Command\Command::run(Object(Symfony\Component\Co
nsole\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
      C:\Users\jarro\Documents\Sites\homestead\vendor\laravel\framework\src\Illu
minate\Console\Command.php : 170

  26  Illuminate\Console\Command::run(Object(Symfony\Component\Console\Input\Arg
vInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      C:\Users\jarro\Documents\Sites\homestead\vendor\symfony\console\Applicatio
n.php : 865

  27  Symfony\Component\Console\Application::doRunCommand(Object(Illuminate\Data
base\Console\Migrations\ResetCommand), Object(Symfony\Component\Console\Input\Ar
gvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      C:\Users\jarro\Documents\Sites\homestead\vendor\symfony\console\Applicatio
n.php : 241

  28  Symfony\Component\Console\Application::doRun(Object(Symfony\Component\Cons
ole\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      C:\Users\jarro\Documents\Sites\homestead\vendor\symfony\console\Applicatio
n.php : 143

  29  Symfony\Component\Console\Application::run(Object(Symfony\Component\Consol
e\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      C:\Users\jarro\Documents\Sites\homestead\vendor\laravel\framework\src\Illu
minate\Console\Application.php : 88

  30  Illuminate\Console\Application::run(Object(Symfony\Component\Console\Input
\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      C:\Users\jarro\Documents\Sites\homestead\vendor\laravel\framework\src\Illu
minate\Foundation\Console\Kernel.php : 121

  31  Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Cons
ole\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      C:\Users\jarro\Documents\Sites\homestead\artisan : 37

我想我知道发生了什么事。我不得不将我的数据库连接更改为PHPStorm中的192.168.10.10以连接到MySQL,因为MySQL位于hometead VM(虚拟机)上并且我无法在本地连接到它,因为技术上它位于不同的机器上#34 ;。那么我去寻找更改数据库信息的位置。我的查询截至目前为止。所以我不知道为什么会出现问题。我找到了/config/database.php并将每个用户名,密码,数据库名称,主机更改为正确的内容:

'mysql' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', '192.168.10.10'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'homestead'),
    'username' => env('DB_USERNAME', 'homestead'),
    'password' => env('DB_PASSWORD', 'secret'),
    'unix_socket' => env('DB_SOCKET', ''),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
    'strict' => true,
    'engine' => null,
],

'pgsql' => [
    'driver' => 'pgsql',
    'host' => env('DB_HOST', '192.168.10.10'),
    'port' => env('DB_PORT', '5432'),
    'database' => env('DB_DATABASE', 'homestead'),
    'username' => env('DB_USERNAME', 'homestead'),
    'password' => env('DB_PASSWORD', 'secret'),
    'charset' => 'utf8',
    'prefix' => '',
    'schema' => 'public',
    'sslmode' => 'prefer',
],

'sqlsrv' => [
    'driver' => 'sqlsrv',
    'host' => env('DB_HOST', '192.168.10.10'),
    'port' => env('DB_PORT', '1433'),
    'database' => env('DB_DATABASE', 'homestead'),
    'username' => env('DB_USERNAME', 'homestead'),
    'password' => env('DB_PASSWORD', 'secret'),
    'charset' => 'utf8',
    'prefix' => '',
],

但是,这并没有真正帮助。我做了命令

  

vagrant reload --provision

确保这不是问题。它在终端

的错误消息中说
  

2
  PDO :: __构建体(" MySQL的:主机= 127.0.0.1 ;端口= 3306; DBNAME =宅&#34 ;,   "家庭教育","秘密",[])

  

127.0.0.1

当我明确地将/ config中的database.php文件中的主机更改为192.168.10.10时,在尝试连接phpstorm时有效。

我很难学习laravel,我很困惑如何将此更改为192.168.10.10。一个经验丰富的人或至少有人试图告诉我正确的方向,以帮助我找到变量$ dsn来改变传递给它的主机吗?

感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

尝试跑步 php artisan cache:clearphp artisan config:clear

您可能还有.env文件可能会覆盖它。

答案 1 :(得分:0)

尝试在根文件夹中编辑.env文件,或者在根文件夹中也编辑.env.example文件