在laravel 5.4中查询异常和PDO异常错误

时间:2017-07-22 05:48:03

标签: php laravel laravel-5.4

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

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

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

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');
            //
        });
    }
}

这是我的Routes / web.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

 [Illuminate\Database\QueryException]
  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=

我的config / database.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'    => '',
        ],

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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