Question
How To Connect Managed MySQL with PHP PDO
Here is my code;
public function __construct($username = "doadmin", $password = "blablabla", $host = "blablabla.db.ondigitalocean.com", $dbname = "defaultdb", $options=array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false)){
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
PDO::MYSQL_ATTR_SSL_CA => 'ca-certificate.crt',
);
$this->isConnected = true;
try {
$this->datab = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8;port:25060", $username, $password, $options);
$this->datab->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->datab->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
}
catch(PDOException $e) {
$this->isConnected = false;
throw new Exception($e->getMessage());
}
}
But when i try to connect it gives “Uncaught Exception: SQLSTATE[HY000] [2002] Connection timed out in”
whats wrong with it? i couldnt find any documentation how to connect with php. and tried to save my certificate.
how can I resolve this?
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
×