The Only Smart Boy
The Only Smart Boy

Reputation: 575

SQLSTATE[HY000] [1045] Access denied for user 'wymtavvh_admin'@'localhost' (using password: YES)

I am using laravel on a live shared hosting and I am getting the error above despite using the right database connection credentials. I have edited the .env and the database.php files and added the necessary credentils. My problem is that the same credentials are working on a raw php file that I have written to test but not on laravel. the laravel project works correctly though on localhost

here is a sample of the .env file code

      DB_CONNECTION=mysql
      DB_HOST=127.0.0.1
      DB_PORT=3306
      DB_DATABASE=wymtav_food
      DB_USERNAME=wymtav_admin
      DB_PASSWORD=PasswordNce
      BROADCAST_DRIVER=log
      CACHE_DRIVER=file
      QUEUE_CONNECTION=sync
      SESSION_DRIVER=file
      SESSION_LIFETIME=120

and here is a snippet of the edited database.php file

    'mysql' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'wymtav_food'),
        'username' => env('DB_USERNAME', 'wymtav_admin'),
        'password' => env('DB_PASSWORD', 'PasswordNce'),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]) : [],
    ],

Note that on local environment, the database port was 3309 and I have changed it to 3306 as per the production environment port. What could I be doing wrong?

Upvotes: 1

Views: 967

Answers (3)

The Only Smart Boy
The Only Smart Boy

Reputation: 575

As it turned out my host had set the database password and username to be the same as the ones for accessing the cpanel. No other user created could do read or write the db directly unless logged into the phpmyadmin. Therefore anyone who experiences this problem especially in directadmin, can try changing the credentials to the ones for login into directadmin

Upvotes: 0

Amit Gupta
Amit Gupta

Reputation: 2806

You are not required to update database connection both at .env and database.php files. You can either do at .env file or database.php file.

It is recommended to do in .env file like below :-

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=wymtav_food
DB_USERNAME=wymtav_admin
DB_PASSWORD=PasswordNce

Some of the hosting servers provide localhost as DB_HOST but some provides their own host url so you need to check with your hosting account for that.

Upvotes: 1

Hugo Trial
Hugo Trial

Reputation: 396

'host' => env('DB_HOST', '127.0.0.1'),

This is not the correct IP address of your database. You should check your host and get the IP of your db.

Upvotes: 0

Related Questions