1
votes

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?

3
Your DB_HOST value is set to 127.0.0.1 which stands for localhost. Your provider should give you a remote mysql db host. Could this be the issue? - AlexioVay
@AlexioVay Note that, I am able to connect to the db from the same server using native php code like this <?php $database ="wymtav_food"; $user ="wymtav_admin"; $password ="PasswordNce"; $port = "3306"; $host = "localhost"; $conn = mysqli_connect($host, $user, $password, $database, $port); if(!$conn){ echo mysqli_error($conn); } - The Only Smart Boy

3 Answers

1
votes

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.

0
votes
'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.

0
votes

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