0

I'm attempting to connect to a database through ssh tunnel. For whatever reason my database refuses to connect with 'my_user'@'localhost' but will connect with 'my_user'@'127.0.0.1'.

My php code specifies the host as 127.0.0.1 but when I run the code I'm not allowed to connect and receive.

Access denied for user 'my_user'@'localhost' (using password: YES)

I am able to connect via the command line with

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

however if I use

mysql -u my_user -p -P 3307 -h localhost

I receive the error above

decapo
  • 789
  • 3
  • 8
  • 25
  • Did you changed the database after running the ssh tunnel ? – Roland Allla Aug 15 '18 at 14:46
  • I will suggest to stop "terminate your ssh tunnel" , change the database based on your request and run again the tunnel ... – Roland Allla Aug 15 '18 at 14:48
  • I have not made any changes to the database user and host since making the tunnel. I am able to connect to the database through command line. In the command line if I connect with localhost I receive access denied, if i use 127.0.0.1 I am allowed to connect. – decapo Aug 15 '18 at 14:49
  • What does `nslookup localhost` return? – AbraCadaver Aug 15 '18 at 14:50
  • https://stackoverflow.com/questions/5239376/mysql-localhost-127-0-0-1-problem?rq=1 – AbraCadaver Aug 15 '18 at 14:52
  • @AbraCadaver I"m on centos7 and it does not have nslookup even in yum. And the question you linked to looks like it's for a local instance of mysql. I'm able to connect to the local mysql instance on both machines with localhost. It's only through the tunnel that I get the access denied error. – decapo Aug 15 '18 at 14:56
  • No `nslookup` on CentOS??? Try `ping` then. – AbraCadaver Aug 15 '18 at 14:57
  • @AbraCadaver running ping goes through the ping sequence (i.e. 64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=1 ttl=64 time=0.029 ms) several times – decapo Aug 15 '18 at 14:59
  • Take a look at your config/database.php and .env file. Maybe your database information are different ... clear cache , and run again the tunnel – Roland Allla Aug 15 '18 at 15:15

1 Answers1

0

I found my problem. I was not specifying the port. My ssh tunnel is on port 3307 and the port was not specified in my dsn so it was trying to connect to the default port of 3306 which is probably why it was translating to localhost.

decapo
  • 789
  • 3
  • 8
  • 25