By Axel
I’ve got one droplet at DO and one webspace with domain at another hosting provider. I don’t have access to anything other than ftp on the other server. On that other webspace I’ve got a php script which I want to remotely connect to my mySQL db on my droplet. I’ve opened the port 3306 in the firewall, added a user with access to the table from % and changed the row starting with “bind-adress” (also tried commenting out) in the configuration file. Still I get an error like this when trying to connect:
Fatal error: Uncaught exception ‘PDOException’ with message ‘SQLSTATE[HY000] [2002] Connection timed out’ in {the url to the script that’s trying to connect}:19 Stack trace: #0 {the url to the script that’s trying to connect}(19): PDO->__construct('mysql:host={my droplet public ip}, ‘{username}’, ‘{password}’) #1 {main} thrown in {the url to the script that’s trying to connect} on line 19
And the connection part in php looks like this:
$db = new PDO(‘mysql:host={droplet public ip};port=3306;dbname={database name};charset=utf8’, ‘{username}’, ‘{password}’);
What am I doing wrong?! I’ve contacted the DO support, they couldn’t help me since we don’t know where the problem is. They told me to try the community.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
not sure if this qualifies as an answer, but if you haven’t already, you’ll probably want to ensure that port 3306 is open on mysql server ip address. From the webserver with the php pages, you’ll want to issue:
telnet <mysql ip address> 3306
just to make sure there is connectivity. You may wish to install the mysql client on the webserver installation as well, just to check if you are able to login properly. If you are using ubuntu,
apt-get install mysql-client
and then connect with:
mysql -h<mysql public ip> -u<user> -p<password>
Can’t say much about PDO since I’ve never used it, but I had apparently got mysql connectivity some years ago using mysqli. It’s been a while so the information I have below might be incorrect:
(http://php.net/manual/en/mysqli.quickstart.connections.php)
function connect() {
$con = new mysqli ("<host>","<username>","<password>","<database schema>");
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
return $con;
}
Most shared hosting providers block remote access to anything other than SSH, if they provide SSH access to their customers at all.
The primary reason for blocking port access is security. It’s also the primary reason that any VPS or Dedicated Server should be using a firewall configured to block commonly accessed ports and the Private Network IP should be used in place of the public (where possible).
While the web hosting provider could simply block each IP, throttle connections, etc, when you’re a provider that’s hosting 1,000+ clients on a server (not uncommon for the cheap-o providers offering the world $1 - i.e. every single “unlimited” provider on the market), it’d bog service down even more allowing such and they’d spend more time handling issues in this area than elsewhere.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.