skip to Main Content

I have developed a Laravel project that connects to MySQL servers on remote sites (via static IP). On each site I have setup Port Forwarding on router (with no restrictions) so that the MySQL server is made public to the internet. Furthermore, on each remote MySQL server I have created a user with no IP restrictions and the service is listening on all interfaces. I have verified that the public connections are working, because from my dev server (which is my laptop in my house) I am able to execute queries and retrieve results from the remote sites. I have deployed the project in my production server at my hosting service provider (via Plesk). The project is working correct except from one thing.

When the code (at the production server) attempts to connect to any of the remote MySQL servers gets the error SQLSTATE[HY000] [2002] An attempt was made to access a socket in a way forbidden by its access permissions. I have verified that the connection string for each remote MySQL server on my production server is the same as my local server.

What can be the problem?

Thank you for your time.

EDIT:
The following code succeeds on local machine but fails in production.

$remote_db = Store::find(1)->db_url; // mysql://db_user:db_pass@public_ip:18889/database
Config::set("database.connections.remote-db-1", [
    'url' => $remote_db
]);
DB::connection('remote-db-1')
    ->select('SELECT * FROM my_table'); // this produces the error

2

Answers


  1. Chosen as BEST ANSWER

    After contacting my Hosting Provider they informed that all outgoing traffic through unusual ports (other than 80,443,3306 etc) is forbidden by firewall rules. So I changed all ports on remote MySQL from 18889 to 3306 and everything works.


  2. You can create multiple connections to your database.php file inside your config folder then you just include $connection class variable to your model to use specific connection.

    /**
    * The database connection used by the model.
    *
    * @var string
    */
    protected $connection = "master_db";
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search