1

I'm trying to connect my Arduino Uno with MySQL.

This is my code:

#include <SPI.h>
#include <Ethernet.h>
#include <sha1.h>
#include <mysql.h>
/* Setup for Ethernet Library */
byte mac_addr[] = { 0xC8, 0x0A, 0xA9, 0xAF, 0x83, 0x58 }; 
IPAddress server_addr(192,168,0,102);
/* Setup for the Connector/Arduino */
Connector my_conn; // The Connector/Arduino reference
char user[] = "root";
char password[] = "fifa2005";
char INSERT_SQL[] = "INSERT INTO test_arduino.hello VALUES ('Hello from Arduino!', NULL)";
void setup() {
Ethernet.begin(mac_addr);
Serial.begin(115200);
delay(1000);
Serial.println("Connecting...");
if (my_conn.mysql_connect(server_addr, 3306, user, password))
delay(500);
else
Serial.println("Connection failed.");
}
void loop() {
}

I have the following database:

mysql> CREATE DATABASE test_arduino;
Query OK, 1 row affected (0.00 sec)
mysql> USE test_arduino;
Database changed
mysql> CREATE TABLE hello (source char(20), event_date timestamp);
Query OK, 0 rows affected (0.01 sec)

This is the output message:

Connecting... Connection failed.

I am not sure this is the right IP address: 192,168,0,102. I tried with the localhost IP address - 127.0.0.1, but nothing happened.

Greenonline
3,1527 gold badges36 silver badges48 bronze badges
asked Apr 6, 2015 at 17:19
6
  • What kind of system is MySQL running on? E.g. Windows, Linux, MacOS? Commented Apr 6, 2015 at 23:37
  • 1
    Is 192.168.0.102 the address of the computer you're trying to connect to, or is that the address of the ethernet shield? I'm confused as to why you would try connecting to localhost... Commented Apr 7, 2015 at 1:31
  • My operating system is windows 7. 192.168.0.102 is ip adress of my laptop. At first I thought that I need to use IP address of the server but then I saw that actually need the IP address of my laptop Commented Apr 7, 2015 at 6:56
  • You need to use the IP address of the server running MySQL. Commented Apr 7, 2015 at 9:15
  • I use Apache to create a Mysql server and corresponding IP is 127.0.0.1. and it doesn't work with it. Ethernet shield and laptop are connected to the router. I think the problem is mac address or ip address which i used. I take the mac address from: cmd -> ipconfig -> Ipv4 address - 192,168,0,102. I take the mac address from : cmd -> getmac -> Physical Address. Commented Apr 7, 2015 at 12:52

2 Answers 2

1

First you have to grant permission to the user!

Type the query in MySQL like:

GRANT ALL ON test_arduino.* TO root@'192.168.0.102' IDENTIFIED BY 'fifa2005';
RSM
1,4571 gold badge11 silver badges26 bronze badges
answered Mar 18, 2016 at 9:30
1

Can't comment, so I put here. In your Arduino you shall use unique MAC address, that can't be same as in your laptop or any other node in network segment. And it can't be just any number because some bits have different meanings, so to be safe, use MAC from Ethernet shield samples (BTW in production units, MAC addresses have to be purchased, but unfortunately Ethernet shield comes without it).

127.0.0.1 is a "virtual" localhost address (alternative address used to access the services within same host), in your case you shall use MySQL server (your laptop?) host address, so

> server_addr(192,168,0,102);

is right.

answered Mar 19, 2016 at 16:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.