Direct connection ESP8266 to MySQL database

Hello everyone,

I use Wemos D1 which has ESP8266 module for wifi connection. I would like to connect directly to the MySQL database and I used this code:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
//192,168,1,34
IPAddress server_addr(192,168,1,34); // IP of the MySQL server here
char user[] = "user"; // MySQL user login username
char password[] = "password"; // MySQL user login password

// WiFi card example
char ssid[] = "my SSID"; // your SSID
char pass[] = "my Password"; // your SSID Password

// Sample query
char INSERT_SQL[] = "INSERT INTO mytable.arduino_data (hodnota) VALUES (1)";

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

void setup() {
Serial.begin(115200);
// Begin WiFi section
WiFi.begin(ssid, pass);
// Wait for connection
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
Serial.println ( "" );
Serial.print ( "Connected to " );
Serial.println ( ssid );
Serial.print ( "IP address: " );
Serial.println ( WiFi.localIP() );
// End WiFi section
Serial.println("DB - Connecting...");
while (conn.connect(server_addr, 3306, user, password) != true) {
delay(500);
Serial.print ( "." );
}
}

void loop() {
delay(2000);

Serial.println("Recording data.");

// Initiate the query class instance
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
// Execute the query
cur_mem->execute(INSERT_SQL);
// 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 but problem is that I can’t connect to mysql database.I wrote my provider where I has a mysql databse stored and he sent me IP adress so I hope that this is good. But I don’t know why I can’t connect to my database (or hosting where I have a databases stored). So, please can someone help me with my problem?
Thank you

Can you connect to the server from your PC, and access the database using the user ID and password that is in the code?

Thank you for sharing this code.
I am trying to build a netwok of sensors to monitor my neighbourhood.
Therefore I need to write sensor data into my database. Your script is doing the job I think.

I have only one (simple) question.
You are writing a standard value to your DB.

// Sample query
char INSERT_SQL = “INSERT INTO mytable.arduino_data (hodnota) VALUES (1)”;

Do you already have an update of your script that writes sensor values to the database?
If so. Are you willing to share this option?

Thank you

If so. Are you willing to share this option?

Can't you figure out how to do that? A few simple changes.

char INSERT_SQL_FORMAT = "INSERT INTO mytable.arduino_data (hodnota) VALUES (%d)";
char INSERT_SQL[80];
sprintf(INSERT_SQL, INSERT_SQL_FORMAT, someIntVariable);

If the variable you want to store is not an int, change the format specifier to the correct format for your variable type.