0

I added a database to my cPanel, but I cannot connect it.

try{
$db = new 
PDO('mysql:host=markus.veridyen.com;dbname=sosyalki_pratiki1_ipss;charset=utf8','sosyalki','password');
}catch(PDOException $e){
echo 'Hata: '.$e->getMessage();
} 

When I try to connect my database, it gives me this error message:

Hata: SQLSTATE[HY000] [2002] The connection could not be established because the target machine actively refused.

Notice: Undefined variable: db in C:\xampp\htdocs\ipss\survey.php on line 55

Ozan Bilgiç
  • 215
  • 1
  • 9
  • Is that a remote machine, of so it would be uncommon to allow anyone to connect to it remotely. – Nigel Ren Sep 22 '21 at 18:44
  • Yes it's remote machine. if it's not allowing, is there anything possible that I can understand it doesn't allow? – Ozan Bilgiç Sep 22 '21 at 18:46
  • When I make host=localhost it gives me this error: Hata: SQLSTATE[HY000] [1045] Access denied for user 'sosyalki_funnel'@'localhost' (using password: YES) – Ozan Bilgiç Sep 22 '21 at 18:47
  • https://stackoverflow.com/questions/2972600/no-connection-could-be-made-because-the-target-machine-actively-refused-it OR https://stackoverflow.com/questions/55600630/how-to-fix-no-connection-could-be-made-because-the-target-machine-actively-refus OR https://stackoverflow.com/questions/9695224/no-connection-could-be-made-because-the-target-machine-actively-refused-it-127-0 OR not enough (useful) info to give a proper answer. – Luuk Sep 22 '21 at 18:52
  • The mysql server likely has a firewall rule closing off port 3306. "actively refused" means that the connection attempt went through, but the remote server said "no, go away". That said, opening 3306 to the internet would be a fantastically bad idea. Only open it for the IP you're connecting from, or set up tunneling. – Sammitch Sep 22 '21 at 18:57
  • allow `3306 TCP` in firewall and use `localhost` –  Sep 23 '21 at 01:59
  • Change the **host=markus.veridyen.com** to **"host=localhost"** inside the sever and try the code inside the server – Kaviranga Sep 23 '21 at 05:43
  • @Indian how can I allow that in cpanel? – Ozan Bilgiç Sep 23 '21 at 09:00
  • @OzanBilgiç In you cpanel, you can see an option `Remote Mysql` , Just add your ip address –  Sep 23 '21 at 09:15
  • @OzanBilgiç to know your public IP just search on google `my ip` –  Sep 23 '21 at 09:16
  • @OzanBilgiç or Just put `%` Wildcard if you want to allow any public ip address –  Sep 23 '21 at 09:19
  • @OzanBilgiç and also use your server domain for connecting to Remotehost from your local computer –  Sep 23 '21 at 09:22
  • I did all of these, but I still get this error message: SQLSTATE[HY000] [1045] Access denied for user 'sosyalki'@'localhost' (using password: YES) – Ozan Bilgiç Sep 23 '21 at 09:23
  • try { $db = new PDO('mysql:host=localhost;dbname=sosyalki_deneme;charset=utf8','sosyalki','cpanelpassword'); // set the PDO error mode to exception $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected successfully"; } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } sosyalki is my cpanel name – Ozan Bilgiç Sep 23 '21 at 09:23

1 Answers1

0

You still can use localhost as server name on a live server.

I'll assume you are using cpanel.

<?php
$servername = "localhost";
$username = "username"; //your cpanel username
$password = "password"; //your cpanel password

try {
  $conn = new PDO("mysql:host=$servername;dbname=DatabaseName", $username, $password);
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  echo "Connected successfully";
} catch(PDOException $e) {
  echo "Connection failed: " . $e->getMessage();
}
?>
  • unfortunately this is not working too. It gives me this error message: SQLSTATE[HY000] [1045] Access denied for user 'sosyalki'@'localhost' (using password: YES) – Ozan Bilgiç Sep 23 '21 at 08:58
  • basically you put a wrong password thats all. –  Sep 23 '21 at 13:49
  • if you are using cpanel use your cpanel username and password. The one you are using to log in. –  Sep 23 '21 at 13:51