Search code examples
phplaraveldatabaseconnectionlaravel-7

Accessing a remote database is giving an error on Laravel


I'm trying to access a database (which exists on a different server) on my Laravel application. I keep getting the following error: SQLSTATE[HY000] [2002] Permission denied

My .env file is set up as follows (changed a bit to protect privacy):

DB_CONNECTION=mysql
DB_HOST= IP_ADDRESS
DB_PORT=3306
DB_DATABASE=DB_NAME
DB_USERNAME="laravel_user"
DB_PASSWORD="password"

I'm not sure if this is important or not but I can access the web server's database if I go to the link IP.ADDRESS/phpmyadmin

My web.php file is as follows:

Route::get('/test', function(){
    return DB::select("select * from umts_list");
});

Additionally, I also went to IP_ADDRESS/phpmyadmin (where my database exists) and added a new user called laravel_user with the ability to SELECT all data and saved it with the 'password' as its password.

My attempt: I tried whatever I mentioned above and also cleared the cache since I made changes to the .env file by entering the following commands:

php artisan config: clear 
php artisan cache: clear

Any help would be appreciated. Thank you.

Edit 1: On phpMyAdmin, I made sure to add the user as depicted by the following picture: PhpMyAdmin User Account

Additionally, I also checked the port number on phpMyadmin and it is 3306 as depicted by the following image: Port Number


Solution

  • just in case anyone else stumbles upon this post in the future, it turns out that I did not have to create an SSH tunnel or whitelist the IP. The problem was actually with SELinux. This post was the solution: Laravel permission denied on remote Mysql server (AWS aurora)

    Specifically, I ran the command: sudo setsebool -P httpd_can_network_connect_db=1

    This worked perfectly and it was actually blocking connection to remote databases.