SOLVED: Yii2 Basic: Database connection error, but only in terminal ('PDOException' ... 'Access denied for user 'testuser'@'localhost' (using password: YES)')

Hi,

I’m having a strange issue with my database MySql connection, that seems to be failing on one specific occasion.

I’m running a Yii2 framework application (basic) on Xampp on my local machine, using PhpMyAdmin for the MySQL Database.

I’ve got no problem connecting with the db on my site (127.0.0.1:8080/yii-platform/web) or even via “mysql -u root -p”, but my php yii commands in the terminal give an error. So I can’t do any migrations, commands or anything that accesses the database from there (which is why I’ve been creating all my tables with sql queries in PhpMyAdmin).

This is the error message:

The strange thing is that this connection is also using the configuration of the db-connection I have in the Yii app, which is working perfectly on the site. I also know the username and password are correct.

‘class’ => ‘yii\db\Connection’,
‘dsn’ => ‘mysql:host=127.0.0.1;dbname=platformbase;’,
‘username’ => ‘root’,
‘password’ => ‘****’,
‘charset’ => ‘utf8’,

If anyone has an idea what the problem might be, I would be really thankful!

Welcome to the forum.

Check what you have in config/console.php

Thanks, I’m still very new to the framework and I really like it. This is the first problem that I’m really not able to solve…

This is my config > console:

I don’t know if there’s anything useful in there? The $db variable references the db-configuration from my original post.

To me it looks strange that the error is about testuser@localhost and not root@localhost (but I must admit I don’t know).

Which config is used in the yii script?

I’m sorry, I uploaded the wrong image. The error is about whatever user is filled in on the db dsn. I changed the image in the original post so that both say “root”.

How do I see which $config is used in the “yii script” ? I’ve noticed I’ve got a $config in another file called config > “web.php” (this is actually the one I’ve been expanding with routes and stuff) and also a $config in “console.php”, but I don’t understand why I started with two different $configs.

Check with phpMyAdmin if the user have enough privileges for modifying platformbase (e.g. ALL_PRIVILEGES).

I’ve checked and I think this is the case:

The fact that my site works and has access to database (with read, write, delete, update possibilities, so he must be able to login with username and password) makes it hard to pin down where the issue lies. Could it be something very specific with using the commands in terminal?

The web app and the console app have different configs ('web.php' and 'console.php' respectively) because they differ in some aspects.
https://www.yiiframework.com/doc/guide/2.0/en/structure-applications

But usually both of them share the same config file for db connection ('db.php') which is included in 'web.php' and in 'console.php'. The db connection should work in both of them, or should fail in both of them.

This is from the error message.
I’m not familiar with XAMPP, but I guess you are using XAMPP-VM and your web site is working on a virtual machine. Am I right?

The error message seems to tell that you are trying to run the console app in the host machine. But I think it should be run on the VM. Could you try to run the yii command in the VM?

You were right that I’m using a XAMPP-VM, and I had been having issues getting into the terminal of this VM.

Finally I managed to get in, and I ran the command without issues! I checked in the database and I can delete records. So it worked, thank you very much!

For anyone with the same or a similar issue, what I did:

Enter this command in the terminal:

Then you’ll have “root@debian” to browse in, and you’ll need to go to:
cd …
cd opt/lampp/htdocs/project-folder

From there you can use the same php yii commands, normally without any issues!

Thanks again to everyone above who helped me!