Do not connect to SQL

I need help connecting my project to a database.

I'm using a NodeMCU ESP32S for a project where I need to connect it to an external MySQL database hosted on HostGator.

Through Workbench, I managed to test and successfully establish a connection using the following credentials:

Hostname: 11.110.0.24
Username: mariam01_teste
Database: mariam01_ControleAcesso1
Password: lala@1301

Using the hostname: br548.hostgator.com.br also worked.

I've already configured access to the database to allow any IP (%).

The project's Wi-Fi is connecting perfectly, but I can't connect to the database. If anyone can help me, I would greatly appreciate it because I don't know what else to do. I've spent hours trying.

Below is the code:

#include <SPI.h>
#include <WiFi.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>

#include "arduino_secrets.h"

const char* ssid = "NET_2GDFFECE";           // Nome da sua rede WiFi
const char* password_wifi = "4BDFFECE";      // Senha da sua rede WiFi
IPAddress serverIP(108, 179, 252, 200);      // Endereço IP do servidor MySQL
char user[] = "mariam01_teste";       // Seu nome de usuário do MySQL
char password_mysql[] = "lala@1301";       // Sua senha do MySQL
char database[] = "mariam01_ControleAcesso1"; // Seu banco de dados MySQL

WiFiClient client;
MySQL_Connection conn((Client *)&client);
MySQL_Cursor cur = MySQL_Cursor(&conn);

void conectaWifi() {
    Serial.println("Conectando à rede WiFi...");
    WiFi.begin(ssid, password_wifi);  // Inicia a conexão com a rede WiFi

    // Aguarda a conexão ser estabelecida
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }

    Serial.println("");
    Serial.println("WiFi conectado.");
    Serial.println("Endereço IP: ");
    Serial.println(WiFi.localIP());
    delay(3000);
}

void conectaBancoDados() {
    Serial.println("Conectando ao banco de dados MySQL...");
    
    while (!conn.connect(serverIP, 3306, user, password_mysql)) {
        Serial.println("Conexão SQL falhou.");
        conn.close();
        delay(1000);
        Serial.println("Conectando SQL novamente.");
    }
    
    Serial.println("Conectado ao servidor SQL.");
    digitalWrite(LED_BUILTIN, HIGH);
}

void setup() {
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, LOW);
    Serial.begin(115200);
    delay(10);

    // Conecta-se à rede WiFi
    Serial.println();
    Serial.println();
    Serial.print("Conectando a ");
    Serial.println(ssid);
    conectaWifi();  // Chamando a função conectaWifi para se conectar à rede WiFi

    delay(1000);
}

void loop() {
    // Não há necessidade de fazer nada no loop neste momento

    // Conecta-se ao banco de dados
    conectaBancoDados();  // Chamando a função conectaBancoDados para se conectar ao banco de dados

    // Evita a reconexão excessiva ao banco de dados
    delay(10000); // Espera 10 segundos antes de tentar se reconectar
}

And here's the output printed on the monitor:
Stuck at "trying"

ets Jul 29 2019 12:21:46

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)

configsip: 0, SPIWP:0xee

clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00

mode:DIO, clock div:1

load:0x3fff0030,len:1184

load:0x40078000,len:13260
load:0x40080400,len:3028
entry 0x400805e4
Conectando a NET_2GDFFECE
Conectando à rede WiFi...
..
WiFi conectado.
Endereço IP:
192.168.0.8
Conectando ao banco de dados MySQL...
...trying...

I've tried your test DB credentials with my MySQL library and it works as you can see from the serial monitor printed results (using the hostname br548.hostgator.com.br because the IP address is not reachable)

I've just published the new release 1.0.2, if you can't find in Arduino Library manager install manually from zip file.

Your code helped me a lot.
It worked, thank you very much!

1 Like

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