earmsby's avatar

SSH connection to Forge DB

When testing out some functions on my local machine, I connect to the live DB on my Forge server via SSH like so:

ssh -L 3307:127.0.0.1:3306 [email protected]

My env file for this is:

DB_CONNECTION=mysql
 DB_HOST=127.0.0.1
 DB_PORT=3307
 DB_DATABASE=my_db
 DB_USERNAME=forge
 DB_PASSWORD=my_password

This has been working fine for months. But just today when I opened the app on local after SSHing to the DB with the above command, I get a message: Access denied for user 'forge'@'localhost'

I'm a bit confused why I'm getting that. For what it's worth another forge site is working just fine.

0 likes
2 replies
LaryAI's avatar
Level 58

Certainly! The error message:

Access denied for user 'forge'@'localhost'

suggests that MySQL is seeing the connection as coming from "localhost" and rejecting the credentials. Here are some things to check and try:


1. Double-Check SSH Tunnel

Make sure your SSH tunnel is running and active. You should keep the terminal with this command open:

ssh -L 3307:127.0.0.1:3306 [email protected]

If you accidentally closed the tunnel or it disconnected, your app would attempt to connect to your local (non-Forge) MySQL server, which may not have a user forge.


2. MySQL User Host Setting

MySQL user permissions are host-based. If your MySQL user on the server is only allowed to connect from certain hosts, you may see this error. Check your MySQL users on the Forge server by running:

SELECT user, host FROM mysql.user WHERE user = 'forge';

You should see a row like:

forge | localhost

or

forge | %

If the Forge user is only permitted for localhost, that's correct for SSH tunnel usage. But if not, you may need to adjust user privileges.


3. .env and Config Cache

Make sure your .env is up to date, and clear Laravel's config cache just in case:

php artisan config:clear

Ensure the .env points to:

DB_HOST=127.0.0.1
DB_PORT=3307
DB_DATABASE=my_db
DB_USERNAME=forge
DB_PASSWORD=my_password

4. Test Using Command Line

From your local machine, while the tunnel is active, try:

mysql -h 127.0.0.1 -P 3307 -u forge -p

If you get the same error, the problem isn't Laravel—it's the tunnel or MySQL credentials.


5. MySQL Password

Double-check that the password for the forge user hasn't recently been changed on the live server.


6. Another Forge Site is Working

Since you mention another Forge site is working, perhaps that database has different user settings, or you may be tunneling to a different server or port without realizing.


Summary Checklist

  • Is the SSH tunnel up and stable?
  • Are your Laravel .env values correct?
  • Does the MySQL forge user have privileges for localhost?
  • Can you connect via the CLI with the same credentials?
  • Has the password for the forge user changed?

If everything seems correct but it still fails, try restarting the tunnel and double-checking the server IP and port.

Let me know if you find anything unusual in these steps!

earmsby's avatar

I had already checked the IP, password etc. and verified that the SSH tunnel was up. But I double checked one more time, ran the config:clear command and that seems to have solved the issue.

Please or to participate in this conversation.