简体   繁体   中英

Remote MySQL Connection Error SQLSTATE[HY000] [2002]

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

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";

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM