1

SQLSTATE[HY000] [2002] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (SQL: SELECT * FROM users)

I want to connect to my remote database in my local project but when I do I get the error listed above. It works in my another database (from localhost), how can I connect my remote server database?

database.php

'mysql2' => [
    'driver' => 'mysql',
    'host' => 'domain.com',
    'port' => '3306',
    'database' => 'mybd',
    'username' => 'username',
    'password' => 'password',
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
    'options' => ['mode' => 'ssl'],
    'strict' => false,
    'engine' => null,
]

Controller

public function test()
{
    $users = DB::connection('mysql2')->select("SELECT * FROM users");
    dd($users);
}
Nick
  • 138,499
  • 22
  • 57
  • 95
Uzzal Hosen
  • 253
  • 6
  • 16
  • What are you using for local development? Xampp or vm or docker – Abdulla Oct 13 '18 at 22:42
  • Does the remote database allow you to connect with SQL Workbench? My guess is that you have some kind of remote block enabled. If you are using Cpanel you can log in and check under `Remote MySQL` if you are hosting it yourself you can check out [This StackOverflow question](https://stackoverflow.com/questions/14779104/how-to-allow-remote-connection-to-mysql) – Marcello B. Oct 13 '18 at 22:44
  • does the dbms server on your remote, listen public network or only localhost. It seems like yours is not open to public network (or lan if you're working on one) – Cemal Oct 13 '18 at 22:44
  • @abdulla_malik i am using xampp – Uzzal Hosen Oct 13 '18 at 22:47
  • Have you been able to connect to the database from your machine with mySQl or data grip? Secondly have you tried adding these settings to your .env file? @jack-sanderson – Abdulla Oct 13 '18 at 22:47
  • 1
    @SuitBoyApps i have added host access – Uzzal Hosen Oct 13 '18 at 22:48
  • @abdulla_malik no , i don't have added those data in env file. i am assigned those data directly here – Uzzal Hosen Oct 13 '18 at 22:49
  • http://prntscr.com/l5si8g http://prntscr.com/l5sicz – Uzzal Hosen Oct 13 '18 at 23:20

2 Answers2

2

Solutions:

  1. Try removing SSL from 'options'=>['mode'=>'ssl'].
  2. Disable firewall on the remote database(if you have the access) temporarily just to test. If it works then allow or add source http://localhost on your remote server.
  3. Use VM or docker, configure SSL on one of these.
Abdulla
  • 485
  • 6
  • 16
0
  • Check that the port 3306 is enabled by doing $ telnet [IP] [PORT]
  • Add Remote Access Host if using cPanel
  • You would may also need to create an iptable/firewall rule to open the port 3306 with the protocol of choice [TCP/UDP]

This solved the problem for us.

Robert Sinclair
  • 4,550
  • 2
  • 44
  • 46