Connecting esp8266 to MySQL database

hello everyone !
i'm trying to connect directly esp8266 to MySQL database and I used this code:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <TimeLib.h>
#include <NtpClientLib.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>

ESP8266WebServer server ;
WiFiClient client;
MySQL_Connection conn((Client *)&client);

byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
uint8_t pin_led = 2;
char* ssid = "dorra";
char* password = "dorradorra";
char* mySsid = "ESP8266_SSID";
char* user = "root"; // MySQL user
char* passwordd = "sogeclair&"; // MySQL password
char query[128];

IPAddress ip(192,168,11,4);
IPAddress gateway(192,168,11,1);
IPAddress subnet(255,255,255,0);
IPAddress server_addr(127,0,0,1); // MySQL server IP

void toggleLED()
{
digitalWrite(pin_led,!digitalRead(pin_led));
server.send(204,"");
}

void setup()
{
pinMode(pin_led, OUTPUT);
WiFi.mode(WIFI_AP_STA);
WiFi.begin(ssid,password);
Serial.begin(115200);
while(WiFi.status()!=WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
WiFi.macAddress(mac_addr);

Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
server.begin();
NTP.begin();
server.on("/",{server.send(200,"text/plain","Page web gneree par "+ String(mySsid) + " \n"
"Heure et Date d entree sont : "+NTP.getTimeDateString ());});

server.on("/toggle",toggleLED);

WiFi.softAPConfig(ip, gateway, subnet);
WiFi.softAP(mySsid, password);

Serial.println("Connecting to database...");

while (conn.connect(server_addr, 3306, user, passwordd) != true) {
delay(200);
Serial.print ( "trying to connect..." );
}

Serial.println("");
Serial.println("Connected to SQL Server!");

}
void loop()
{
server.handleClient();
delay(2000);

Serial.println("Recording data.");
// Initiate the query class instance
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);

cur_mem->execute(query);
// Note: since there are no results, we do not need to read any data
// Deleting the cursor also frees up memory used
delete cur_mem;

}

I can connect to My WIFI and set the esp8622 as an access piont at the same time , printing date and time on the webserver is working too but the problem is that i can't connect to MySQL Database .I wrote my provider where I has MySQL Database stored but I don't know why i can not connect to.
So,please cann someone help me with my problem ?
thank you and have a good day :slight_smile:

IPAddress server_addr(127,0,0,1);    // MySQL server IP

That is NOT the IP address of the server that MySQL is installed on.

The 127.0.0.1 address is the loopback address. Any requests to that address loop back to the sender to fulfill. Your MySQL database is not running on the Arduino/ESPxxxxx making the request.

i understood ;Can you tell me how can i get MySQL server adresse ???

shiraz2:
i understood ;Can you tell me how can i get MySQL server adresse ???

If you know its name, ping it. ping will tell you the IP address that the name mapped to.

why all these hassles for? eating your precious RAM with bunch of libraries on it

why not making a REST API with PHP in a server you'd freely get one from 000webhost.app

OR just using a file.txt as DB situated somewhere in your server

OR using the ready made API from http://dweet.io, with GET request to send your data there and retrieve it when you want

PaulS:
If you know its name, ping it. ping will tell you the IP address that the name mapped to.

Ichanged the IP adresss of the MySQL server to : IPAddress server_addr(192,168,11,104); but it still not working -_-''

KASSIMSAMJI:
why all these hassles for? eating your precious RAM with bunch of libraries on it

why not making a REST API with PHP in a server you'd freely get one from 000webhost.app

OR just using a file.txt as DB situated somewhere in your server

OR using the ready made API from http://dweet.io, with GET request to send your data there and retrieve it when you want

i want to connect directly to the database using one code

Don't forget that by default Mysql only accepts requests from the localhost. You will need to change the config, specifically the bind-address to allow your arduino tp talk to the database.

shiraz2:
Ichanged the IP adresss of the MySQL server to : IPAddress server_addr(192,168,11,104); but it still not working -_-''

The fact that the server address is in your local area network means that you have access to the server logs. Look at them. Tell us what the Arduino, or any other machine in the LAN is doing when it successfully, or unsuccessfully, connects, or tries to.

Hi

you are traying to connect the esp to MySQL; please had you seen that probleme before , if yes please give me the solution; i'm using the arduino uno carte with the esp8266

In file included from C:\Program Files (x86)\Arduino\libraries\ESP8266WiFi\src/ESP8266WiFiSTA.h:28:0,

from C:\Program Files (x86)\Arduino\libraries\ESP8266WiFi\src/ESP8266WiFi.h:34,

from C:\Users\FK\Documents\Arduino\EXP_BD\EXP_BD.ino:6:

C:\Program Files (x86)\Arduino\libraries\ESP8266WiFi\src/ESP8266WiFiGeneric.h:27:22: fatal error: functional: No such file or directory

compilation terminated.

exit status 1
Erreur de compilation pour la carte Arduino/Genuino Uno

I still trying but it doesn't work until now :confused: i don't know why :slightly_frowning_face:
but i did not seen that problem before , maybe your port and your card type in tools icon are wrongs ; check them

shiraz2:
Ichanged the IP adresss of the MySQL server to : IPAddress server_addr(192,168,11,104); but it still not working -_-''

Hi, i had the same problem, but isn't relation with the program, the problem is because the server doesn't let insert data from nobody external for security.

So, for solve that you have to "permit" to you server what IP have permission to inyect info into you Data Base, and the configuration depend of the hosting that you have