MySQL_MariaDB_Generic icon indicating copy to clipboard operation
MySQL_MariaDB_Generic copied to clipboard

Can't connect to my local MariaDB database

Open SrikanthBala opened this issue 6 years ago • 1 comments

Arduino IDE version : 1.8.12 ESP8266 Core 2.7.2 OS Windows 10 64 bit Server installed : XAMPP 64 bit for Windows

I have been struggling with this for 3 days and can't see what is wrong

The code is the Basic_Insert_ESP example file (exact code posted below)

Just 8 days back, a similar code from MySQLConnecter was connecting and inserting values in the database quiclky That code doesn't work now either with same settings That same code was also simultaneously inserting data into a online MySQL database and that STILL WORKS Perfectly Its only my local MariaDB database that doesnt work When I try using a GET statement on the browser and insert into the same local database using a PHP script with the same user credentials IT WORKS that way I don't remember changing anything

I have a XAMPP installation with MariaDB (MySQL) on port 3306

The code connects to the router OK On the server side I have checked thru phpMyAdmin and I have a user account called 'esp8266' with a password 'pass' and host name has been set to '%' Global privileges are set to 'ALL PRIVILEGES' and Grant has been set to 'Yes' for this account The user account 'esp8266' shows up in Privileges for the said database called 'test' The name of the table is 'temp'

The following is the Serial output

Starting Basic_Insert_ESP on ESP8266_NODEMCU
Connecting to XXXXXX (My router name here) 
............Connected to network. My IP address is: 10.0.0.6
Connecting to SQL Server @ 10.0.0.100, Port = 3306
User = esp8266, PW = pass, DB = test
Connecting...
[SQL] Connecting to Server: 10.0.0.100 , Port =  3306
[SQL] Can't connect. Retry # 1
[SQL] Can't connect. Retry # 2
[SQL] Can't connect. Retry # 3
[SQL] Can't connect. Retry # 4
[SQL] Can't connect. Retry # 5

Connect failed. Trying again on next iteration.

############ End of serial output

So I tried whatever I could think of including reinstalling XAMPP and recreating the database as it was (had exported it and reimported it) I have also gone to MySQLConnecter 1.1 version and that doesnt work either. I'm sure even version 1.2 was working earlier So I tried this new MySQL_MariaDB_Generic library This is not working either

Just cannot understand what changed in those 8 days.

Connecting to the same database with a GET request to insert data using a php file for the purpose works ok. But I wanted to connect directly to database

Please help.

My code is basically the same as the original Basic_Insert_ESP sketch and is as below. Have not included the credentials file but the user is 'esp8266' and password has been set to 'pass' there

########### The code // Basic Insert ESP File

#if ! (ESP8266 || ESP32 )
  #error This code is intended to run on the ESP8266/ESP32 platform! Please check your Tools->Board setting
#endif

#include "Credentials.h"

#define MYSQL_DEBUG_PORT      Serial

// Debug Level from 0 to 4
#define _MYSQL_LOGLEVEL_      1

#include <MySQL_Generic_WiFi.h>

IPAddress server_addr(10,0,0,100); 
uint16_t server_port = 3306;

char default_database[] = "test";   
char default_table[]    = "temp";    

// Sample query
char INSERT_SQL[] = "INSERT INTO test.temp(temperature,device,station,batt) VALUES (1,2,3,4)";

MySQL_Connection conn((Client *)&client);

MySQL_Query *query_mem;

void setup()
{
  Serial.begin(115200);
  while (!Serial);

  Serial.println("\nStarting Basic_Insert_ESP on " + String(ARDUINO_BOARD));

  // Begin WiFi section
  Serial.println(String("Connecting to ") + ssid);
  
  WiFi.begin(ssid, pass);
  
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print(".");
  }

  // print out info about the connection:
  Serial.print("Connected to network. My IP address is: ");
  Serial.println(WiFi.localIP());

  Serial.print("Connecting to SQL Server @ ");
  Serial.print(server_addr);
  Serial.println(String(", Port = ") + server_port);
  Serial.println(String("User = ") + user + String(", PW = ") + password + String(", DB = ") + default_database);
}

void runInsert(void)
{
  // Initiate the query class instance
  MySQL_Query query_mem = MySQL_Query(&conn);

  if (conn.connected())
  {
    Serial.println(INSERT_SQL);
    
    // Execute the query
    // KH, check if valid before fetching
    if ( !query_mem.execute(INSERT_SQL) )
      Serial.println("Insert error");
    else    
      Serial.println("Data Inserted.");
  }
  else
  {
    Serial.println("Disconnected from Server. Can't insert.");
  }
}

void loop()
{
  Serial.println("Connecting...");
  
  //if (conn.connect(server_addr, server_port, user, password))
  if (conn.connectNonBlocking(server_addr, server_port, user, password) != RESULT_FAIL)
  {
    delay(500);
    runInsert();
    conn.close();                     // close the connection
  } 
  else 
  {
    Serial.println("\nConnect failed. Trying again on next iteration.");
  }

  Serial.println("\nSleeping...");
  Serial.println("================================================");
 
  delay(10000);
}

########### End of code

SrikanthBala avatar Aug 28 '20 04:08 SrikanthBala

It's working now ! Only, I don't know what was wrong earlier. I reinstalled xampp and created a new user and granted all privileges and it works like before !

Strange ! Don't know what mistake I had made earlier

SrikanthBala avatar Aug 29 '20 04:08 SrikanthBala