gpt4 book ai didi

mysql - SQLSTATE [HY000] [1045] 流明 api 上的 'root' @'localhost' 访问被拒绝

转载 作者:行者123 更新时间:2023-11-30 21:30:23 25 4
gpt4 key购买 nike

<分区>

我在 ubuntu 18.04 上用 tasksel 安装 lamp。我已按照说明中的说明进行了配置 https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-phpmyadmin-on-ubuntu-18-04

更改 .env 文件和 config/database.php 文件中的数据库详细信息后,在浏览器中访问该应用。

创建了一个数据库“mydb”

创建了一个用户“myusername”/“mypwd”并授予该用户和数据库所有权限。例如-->

CREATE USER 'myusername'@'localhost' IDENTIFIED BY 'mypwd';
GRANT ALL PRIVILEGES ON * . * TO 'myusername'@'localhost';
FLUSH PRIVILEGES;

lumen.env 中的配置

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mydb
DB_USERNAME=myusername
DB_PASSWORD=mypwd

配置/数据库.php

'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'mydb'),
'username' => env('DB_USERNAME', 'myusername'),
'password' => env('DB_PASSWORD', 'mypwd'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
]

在终端运行'path/project_folder/'php artisan 缓存:清除

日志:

[2017-01-23 09:34:24] local.ERROR: exception 'PDOException' with message >'SQLSTATE[HY000] [1045] Access denied for user 'myusername'@'localhost' (using password: YES)' in Illuminate\Database\Connectors\Connector.php:119

注意:验证 PDO 是否在我的机器上工作使用 testdb.php 测试了 mysql 连接:

$servername = "localhost";
$dbname = "testdb";
$username = "root";
$password = "secret";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
die("mysqli Connection failed: " . $conn->connect_error);
}
echo "
mysqli Connected successfully
";
// working correct

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
die("

mysqli_connect Connection failed: " . mysqli_connect_error());
}
echo "
mysqli_connect Connected successfully
";

// working correct
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "
MySQL PDO Connected successfully
";
}
catch(PDOException $e)
{
echo "

MySQL PDO Connection failed: " . $e->getMessage();
}
?>

输出是

mysqli Connected successfully

mysqli_connect Connected successfully

MySQL PDO Connection failed: SQLSTATE[HY000] [1045] Access denied for >user 'root'@'localhost' (using password: YES)

我本地机器上的 PDO 不工作。 mysql pdo 的扩展处于事件状态。多次重启 Lamp 服务器。但它仍然无法正常工作。谁能帮忙?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com