平的PHP连接到数据库,但Laravel没有

时间:2019-03-01 09:12:39

标签: php mysql database laravel

让我们先登录网站...错误日志为:

  

local.ERROR:SQLSTATE [HY000] [1045]用户的访问被拒绝   'cp35161_xxx'@'127.0.0.1'(使用密码:是)(SQL:从*中选择*   users的限制1){“ exception”:“ [对象]   (Illuminate \ Database \ QueryException(代码:1045):SQLSTATE [HY000]   [1045]对用户'cp35161_xxx'@'127.0.0.1'的访问被拒绝(使用   密码:是)(SQL:在users限制1中选择*)   /home/cp35161/public_html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664,   PDOException(code:1045):SQLSTATE [HY000] [1045]拒绝访问   用户'cp35161_xxx'@'127.0.0.1'(使用密码:是)   /home/cp35161/public_html/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70)

所以我在/public/index.php中输入了以下代码:

$conn = new PDO("mysql:host=localhost; dbname=cp35161_xxx", "cp35161_yyy", "zzz");
$a = $conn->query("select * from users")->fetchAll();
print_r($a);
die;

这个打印出数据。问题出在哪里?

我的.env文件:

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=cp35161_xxx
DB_USERNAME=cp35161_yyy
DB_PASSWORD=zzz

我的/config/database.php文件:

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

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

0 个答案:

没有答案