Laravel 5.4 中的查询异常和 PDO 异常错误



我遇到了这个问题 2 天......我已经尝试了很多东西,但由于我是 Laravel 的初学者,因此无法修复它。我的代码片段如下:

这是我的数据库/迁移/2017_07_21_051254_add_votes_to_users_table:

<?php
use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
class AddVotesToUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id')->unique();
$table->varchar('name');
$table->varchar('email');
$table->varchar('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
//
});
}
}

这是我的路线/网络.php:

<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
$name='Random';
$x='beautiful lady';
return view('welcome', compact('name','x'));
});

这是我在命令行中显示的错误 php artisan:migrate

[IlluminateDatabaseQueryException]
SQLSTATE[HY000] [2002] A connection attempt failed because the connected party did not properly respond after a per
iod of time, or established connection failed because connected host has failed to respond.
(SQL: select * from information_schema.tables where table_schema = database and table_name = migrations)

[PDOException]
SQLSTATE[HY000] [2002] A connection attempt failed because the connected party did not properly respond after a per
iod of time, or established connection failed because connected host has failed to respond.

我也给了我的 .env 文件:

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:Qx8qKm2SoC/SBr/Cw4whbL3ORIjqq/QGmDjawXlx8ZA=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=back
DB_USERNAME=root
DB_PASSWORD=
BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=

还有我的配置/数据库.php文件:

'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'back'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
'read' => [
'host' => '192.168.1.1',
],
'write' => [
'host' => '196.168.1.2'
],
'driver'    => 'mysql',
'database'  => 'database',
'username'  => 'root',
'password'  => '',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix'    => '',
],

无法建立数据库连接。你能运行:php artisan optimize吗?例如?

这是因为没有建立与MySQL的连接。确保您有正确的MySQL端口。通常MySQL端口将是 3306。

也请检查您的.env文件。用户名、密码、数据库名称是否正确配置

最新更新