1

I am installing lamp with tasksel on ubuntu 18.04. I have configured as per the instructions in the https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-phpmyadmin-on-ubuntu-18-04

After changing the database details in .env file and config/database.php files, accessed the app in browser.

created a database "mydb"

created an user "myusername" / "mypwd" and grant all privileges to this user and database. eg-->

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

Configuration in lumen .env

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

config/database.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,
]

In terminal run 'path/project_folder/' php artisan cache:clear

Log:

[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

NOTE: To validate if PDO is working or not in my machine Tested the mysql connection with testdb.php:

$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();
}
?>

Output is

mysqli Connected successfully

mysqli_connect Connected successfully

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

PDO in my local machine is not working. The extensions for mysql pdo is active. Restarted Lamp server many times. But it is still not working. Can any one help?

vahid
  • 11
  • 4

1 Answers1

0

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

This does not mean that PDO is not working, it is working. Your login just failed for user 'root'. The password can be incorrect or something else can be the problem. (such as the host)

Could you try the following?

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

This will create a user which can login from any host.

  • i already create user with all privilages. and successfully connect with mysqli. but for pdo with same user pass give me error. – vahid Jun 11 '19 at 11:38
  • I Solve the problem with set ```pdo_mysql.default_socket=/var/run/mysqld/mysqld.sock``` to php.ini – vahid Jun 11 '19 at 11:42