# Erro SQLSTATE\[HY000\] \[2002\] Connection refused

**URL:** <https://forum.infinityfree.com/t/erro-sqlstate-hy000-2002-connection-refused/93928>\
**Category:** Hosting Support\
**Created:** [May 22, 2024, 1:48pm UTC](https://forum.infinityfree.com/t/erro-sqlstate-hy000-2002-connection-refused/93928 "2024-05-22T13:48:18Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![edsoncardoso](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/edsoncardoso/32/48416_2.png) [@edsoncardoso](https://forum.infinityfree.com/u/edsoncardoso)\
**Post date:** [May 22, 2024, 1:48pm UTC](https://forum.infinityfree.com/t/erro-sqlstate-hy000-2002-connection-refused/93928/1 "2024-05-22T13:48:18Z")

</div>

I can’t connect to my database, please help.

---

<div class="post-metadata">

**Author:** ![Greenreader9](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/greenreader9/32/22847_2.png) [@Greenreader9](https://forum.infinityfree.com/u/Greenreader9)\
**Post date:** [May 22, 2024, 1:50pm UTC](https://forum.infinityfree.com/t/erro-sqlstate-hy000-2002-connection-refused/93928/2 "2024-05-22T13:50:06Z")

</div>

> [@Common MySQL Connection Errors](https://forum.infinityfree.com/t/common-mysql-connection-errors/49338):
>
> Many websites use MySQL databases to store information and settings. However, there are some reasons why the connection to the database may fail. This article describes some commonly seen error messages when connecting to a database, as well as some general tips to keep in mind when setting up a database connection. Common Error Messages Using MySQL is great, but the error message returned if there is an issue logging in to your database can be quite confusing. Here are some commonly seen error…

---

<div class="post-metadata">

**Author:** ![Topher7lol](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/topher7lol/32/59404_2.png) [@Topher7lol](https://forum.infinityfree.com/u/Topher7lol)\
**Post date:** [May 24, 2024, 12:01am UTC](https://forum.infinityfree.com/t/erro-sqlstate-hy000-2002-connection-refused/93928/3 "2024-05-24T00:01:16Z")

</div>

> [@edsoncardoso](#):
>
> I can’t connect to my database, please help.

can you provide the file name and/or contents (excluding private login details), and I can see how you setup the config/other file name? Because there may be a typo, incorrect details, etc, and the connection must be in a php code.

---

<div class="post-metadata">

**Author:** ![edsoncardoso](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/edsoncardoso/32/48416_2.png) [@edsoncardoso](https://forum.infinityfree.com/u/edsoncardoso)\
**Post date:** [May 24, 2024, 1:12pm UTC](https://forum.infinityfree.com/t/erro-sqlstate-hy000-2002-connection-refused/93928/4 "2024-05-24T13:12:21Z")

</div>

Erro SQLSTATE[HY000] [2002] Connection refused  
**Fatal error** : Uncaught Error: Call to a member function prepare() on null in /home/vol15\_6/epizy.com/epiz\_24233891/htdocs/SIGA/classes/ConfiguracaoInstituicao.class.php:136 Stack trace: #0 /home/vol15\_6/epizy.com/epiz\_24233891/htdocs/SIGA/index.php(12): ConfiguracaoInstituicao-\>dados\_instituicao() #1 {main} thrown in **/home/vol15\_6/epizy.com/epiz\_24233891/htdocs/SIGA/classes/ConfiguracaoInstituicao.class.php** on line **136**

function:

```
	public function dados_instituicao(){
		try {
			$cst = $this->con->conectar()->prepare("SELECT * FROM `tb_instituicao`");
			$cst->execute();
			return $cst->fetch();
		} catch (PDOException $e) {
			return 'erro' . $e->getMessage();
		}
	}

```

database class:

```
class Conexao {

	#ATRIBUTOS PRIVADOS
	private $servidor;
	private $porta;
	private $usuario;
	private $senha;
	private static $pdo;

	// #METODO CONSTRUTOR
	public function __construct(){
		$this->servidor = "sql310.epizy.com";			
		$this->porta = 3306; // Adicione a porta padrão aqui
		$this->usuario = "epiz_24233";
		$this->senha = "SMTusODpWGjYAp";
	}

	#METODO PARA CONECTAR
	public function conectar(){
		try {
			if(is_null(self::$pdo)){
				self::$pdo = new PDO("mysql:host=".$this->servidor.";port=".$this->porta.";dbname=epiz_24233891_siga;charset=utf8", $this->usuario, $this->senha);
				self::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
				// echo "Conectado com sucesso!";
			}
			return self::$pdo;
		} catch (PDOException $e) {
			echo "Erro ".$e->getMessage();
		}
	}
}

```

---

<div class="post-metadata">

**Author:** ![KangJL](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/kangjl/32/12130_2.png) [@KangJL](https://forum.infinityfree.com/u/KangJL)\
**Post date:** [May 24, 2024, 1:32pm UTC](https://forum.infinityfree.com/t/erro-sqlstate-hy000-2002-connection-refused/93928/5 "2024-05-24T13:32:28Z")

</div>

Unlisted on May 24, 2024, 1:32pm UTC

Sensitive details exposed

---

<div class="post-metadata">

**Author:** ![KangJL](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/kangjl/32/12130_2.png) [@KangJL](https://forum.infinityfree.com/u/KangJL)\
**Post date:** [May 24, 2024, 1:37pm UTC](https://forum.infinityfree.com/t/erro-sqlstate-hy000-2002-connection-refused/93928/6 "2024-05-24T13:37:03Z")

</div>

Please reset or change your account password through the client area.

> [@edsoncardoso](#):
>
> `$this->usuario = "epiz_24233";`

Definitely wrong

---

<div class="post-metadata">

**Author:** ![edsoncardoso](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/edsoncardoso/32/48416_2.png) [@edsoncardoso](https://forum.infinityfree.com/u/edsoncardoso)\
**Post date:** [May 24, 2024, 1:57pm UTC](https://forum.infinityfree.com/t/erro-sqlstate-hy000-2002-connection-refused/93928/7 "2024-05-24T13:57:16Z")

</div>

I changed it to $this-\>usuario = “epiz\_24233891”; the correct data but it still doesn’t work, it still has the same error.

---

<div class="post-metadata">

**Author:** ![Admin](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/admin/32/36437_2.png) [@Admin](https://forum.infinityfree.com/u/Admin)\
**Post date:** [May 25, 2024, 1:16pm UTC](https://forum.infinityfree.com/t/erro-sqlstate-hy000-2002-connection-refused/93928/8 "2024-05-25T13:16:29Z")

</div>

I just reset your hosting account password.

Please note that this is a public forum, anything you post here can be seen by anyone. So make sure to not share any confidential information here, notably, and especially, any passwords.

Feel free to change the password again, but don’t use the password you used before, because it is now compromised.

---

<div class="post-metadata">

**Author:** ![Admin](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/admin/32/36437_2.png) [@Admin](https://forum.infinityfree.com/u/Admin)\
**Post date:** [May 25, 2024, 1:16pm UTC](https://forum.infinityfree.com/t/erro-sqlstate-hy000-2002-connection-refused/93928/9 "2024-05-25T13:16:33Z")

</div>

Listed on May 25, 2024, 1:16pm UTC



---

<div class="post-metadata">

**Author:** ![edsoncardoso](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/edsoncardoso/32/48416_2.png) [@edsoncardoso](https://forum.infinityfree.com/u/edsoncardoso)\
**Post date:** [May 29, 2024, 9:27am UTC](https://forum.infinityfree.com/t/erro-sqlstate-hy000-2002-connection-refused/93928/10 "2024-05-29T09:27:16Z")

</div>

Thanks for the alert. But the error still persists.

---

<div class="post-metadata">

**Author:** ![Admin](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/admin/32/36437_2.png) [@Admin](https://forum.infinityfree.com/u/Admin)\
**Post date:** [May 29, 2024, 9:49am UTC](https://forum.infinityfree.com/t/erro-sqlstate-hy000-2002-connection-refused/93928/11 "2024-05-29T09:49:29Z")

</div>

I dove into the issue, because it was a weird head scratcher. But I found the issue.

If you `var_dump` the contents of the `$this->con` variable within the `ConfiguracaoInstituicao` class, you would expect to see the database variables defined in `SIGA/classes/Conexao.class.php`. But that’s not what I saw: I saw database host `127.0.0.1` and username `root`.

So why is this? Because your project has two classes with the name `Conexao`. One in the `SIGA` folder directly, and another one in the `classes` directory. And the one in the `SIGA` folder has the wrong credentials.

Now I assume that the one in the `SIGA` folder gets loaded first. So when in the `ConfiguracaoInstituicao` you try to load the other `Conexao` class, it doesn’t do anything because another `Conexao` class was already loaded.

So to fix this, you could update the database credentials in the `SIGA/Conexao.class.php` file, but I think you’ll instead just one to have one copy of this class in your codebase, and not two.

---

<div class="post-metadata">

**Author:** ![system](https://infinityfree-forum-uploads.s3.dualstack.eu-central-1.amazonaws.com/original/3X/b/0/b024fd3fa88bae093c9576e55c2c94e33f890206.svg) [@system](https://forum.infinityfree.com/u/system)\
**Post date:** [June 5, 2024, 9:50am UTC](https://forum.infinityfree.com/t/erro-sqlstate-hy000-2002-connection-refused/93928/12 "2024-06-05T09:50:27Z")

</div>

Closed on June 5, 2024, 9:50am UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
