dongwu9647 2018-03-30 15:09
浏览 41
已采纳

laravel中的数据库连接

i can't find why laravel doesn't connect to my database, i think however my configuration is right. the error i got is "SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES) " Here is my .env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=chat
DB_USERNAME=root
DB_PASSWORD=root

and my database.php:

      'default' => env('DB_CONNECTION', 'mysql'),

     'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        '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' => '',
        'strict' => true,
        'engine' => null,
      ],

did i miss something ?

  • 写回答

2条回答 默认 最新

  • douganbi7686 2018-03-30 15:34
    关注

    Try to replace

        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
    

    with

        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
    

    and then execute, from your project root:

    php artisan cache:clear
    

    Let me know what you get.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • donglv6960 2018-03-30 15:15
    关注

    Replace content of database.php with code in below. It's looks like mysql key must be inside in connections key.

    return [
    
        /*
        |--------------------------------------------------------------------------
        | Default Database Connection Name
        |--------------------------------------------------------------------------
        |
        | Here you may specify which of the database connections below you wish
        | to use as your default connection for all database work. Of course
        | you may use many connections at once using the Database library.
        |
        */
    
        'default' => env('DB_CONNECTION', 'mysql'),
    
        /*
        |--------------------------------------------------------------------------
        | Database Connections
        |--------------------------------------------------------------------------
        |
        | Here are each of the database connections setup for your application.
        | Of course, examples of configuring each database platform that is
        | supported by Laravel is shown below to make development simple.
        |
        |
        | All database work in Laravel is done through the PHP PDO facilities
        | so make sure you have the driver for your particular database of
        | choice installed on your machine before you begin development.
        |
        */
    
        'connections' => [
    
            'sqlite' => [
                'driver' => 'sqlite',
                'database' => env('DB_DATABASE', database_path('database.sqlite')),
                'prefix' => '',
            ],
    
            'mysql' => [
                'driver' => 'mysql',
                'host' => env('DB_HOST', '127.0.0.1'),
                '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' => '',
                'strict' => true,
                'engine' => null,
            ],
    
            'pgsql' => [
                'driver' => 'pgsql',
                'host' => env('DB_HOST', '127.0.0.1'),
                'port' => env('DB_PORT', '5432'),
                'database' => env('DB_DATABASE', 'forge'),
                'username' => env('DB_USERNAME', 'forge'),
                'password' => env('DB_PASSWORD', ''),
                'charset' => 'utf8',
                'prefix' => '',
                'schema' => 'public',
                'sslmode' => 'prefer',
            ],
    
            'sqlsrv' => [
                'driver' => 'sqlsrv',
                'host' => env('DB_HOST', 'localhost'),
                'port' => env('DB_PORT', '1433'),
                'database' => env('DB_DATABASE', 'forge'),
                'username' => env('DB_USERNAME', 'forge'),
                'password' => env('DB_PASSWORD', ''),
                'charset' => 'utf8',
                'prefix' => '',
            ],
    
        ],
    
        /*
        |--------------------------------------------------------------------------
        | Migration Repository Table
        |--------------------------------------------------------------------------
        |
        | This table keeps track of all the migrations that have already run for
        | your application. Using this information, we can determine which of
        | the migrations on disk haven't actually been run in the database.
        |
        */
    
        'migrations' => 'migrations',
    
        /*
        |--------------------------------------------------------------------------
        | Redis Databases
        |--------------------------------------------------------------------------
        |
        | Redis is an open source, fast, and advanced key-value store that also
        | provides a richer set of commands than a typical key-value systems
        | such as APC or Memcached. Laravel makes it easy to dig right in.
        |
        */
    
        'redis' => [
    
            'client' => 'predis',
    
            'default' => [
                'host' => env('REDIS_HOST', '127.0.0.1'),
                'password' => env('REDIS_PASSWORD', null),
                'port' => env('REDIS_PORT', 6379),
                'database' => 0,
            ],
    
        ],
    
    ];
    
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 win11安卓子系统打开谷歌登录不了切使用浏览器显示处于离线状态
  • ¥15 三维直角坐标系下,给定一个平面内四个点坐标,如何将四个点逆时针排序
  • ¥15 UnityWebRequest访问内网地址502
  • ¥20 Python语言来实现牛顿法(Newton's Method)解决非线性(系统)方程的问题。
  • ¥15 matlab控制工业相机采集图像
  • ¥25 R里做折线图和柱状图
  • ¥20 使用kokkos移植项目,遇到问题
  • ¥15 求该问题的Matlab代码
  • ¥15 python下使用pdpbox为何提示has no attribute 'pdp_isolate'?
  • ¥15 求java web病房管理系统项目,用netbeans做的