Ian
Ian

Reputation: 51

Laravel 5.8 Won’t Connect to Non-Local Database

I am having an issue where a new Laravel Project (5.8) will not connect to a non-local database. I continue getting an “Access Denied for User...” error. I know that the configuration is correct because I have another project running in 5.6 connecting to the same database with the same configuration and it works. It appears that once I use a hostname other than “localhost”, it breaks. It was working locally until I attempted to connect to another non-local database.

I believe Laravel is not accepting the hostname correctly. I have recached everything as well, with no luck. The access denied error always shows the same local hostname of the machine the project is on, never the remote hostname in the env, so I assume it is trying to connect to that. Any ideas on how to fix this issue? I am stumped with no idea where to go.

Thanks in advance!

EDIT: I know for sure there is not a configuration error, IP access error, or caching error as I have already troubleshooted all of these possible issues (it sounds like one of these issues). My env and the error are below (anything starting with "my" is the correct information that I have changed for security):

DB_CONNECTION=mysql
DB_HOST=mysql.myURL.com
DB_PORT=3306
DB_DATABASE=myDbName
DB_USERNAME=myDbUsername
DB_PASSWORD=myDbPassword
SQLSTATE[HY000] [1045] Access denied for user 'myDbUsername'@'NOTmysql.myURL.com' (using password: YES) (SQL: select count(*) as aggregate from `myTable`)

NOTmysql.myURL.com in the error actually happens to be the local hostname of the server that the website is running on, but myDbUsername is the username I set in the configuration.

Upvotes: 0

Views: 1815

Answers (3)

Ian
Ian

Reputation: 51

Embarrassing mistake on my part, but I figured out the answer to my question. I have a # in the password and it was causing the part of the password after the # to be read as a comment so it was only using part of the password. I put the password in quotes and everything is now working.

Upvotes: 2

Vikash Pathak
Vikash Pathak

Reputation: 3562

@IanCowan do try to connect with simple php connect script. Verify does this working for you on the same repository... just placed that php file in /public folder.

And open/browse url to that file. Don't forgot to change the config.

This is just to ensure... there is no issue at connection end.

<?php
    $servername = "localhost";
    $username = "username";
    $password = "password";

    try {
        $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        echo "Connected successfully"; 
        }
    catch(PDOException $e)
        {
        echo "Connection failed: " . $e->getMessage();
        }
    ?>

Upvotes: 1

Sushank Pokharel
Sushank Pokharel

Reputation: 881

1. Go to the .env file and check the database configuration

DB_HOST=localhost
DB_DATABASE=laravelDb
DB_USERNAME=root
DB_PASSWORD=null

2. Then restart the apache server and refresh the page

3. Exit/close and re-run the command

php artisan serve

4. Clear the old configuration cache and config file

php artisan config:clear
php artisan cache:clear

Now your error should be gone.

Upvotes: 0

Related Questions