A.Kallas
A.Kallas

Reputation: 75

How to connect to MySQL database using IP not "localhost"?

If I have a node js server on my Raspberry pi. And a MySQL database on my laptop. Both the raspberry pi and my laptop are connected to the same LAN.

Laptop IP: 192.168.1.2

Raspberry pi IP: 192.168.1.3

Part 1, How can I send data from the raspberry pi server to the MySQL database on my laptop?

At first I thought that all I have to do is to change the host field from "localhost" to "192.168.1.2", but this does not seem to work because:

I created a node js server on my laptop (as I do not have a raspberry pi right now) and tried to connect to the database on it to store data, It worked perfectly. But when I tried to change the host field from "localhost" to "192.168.1.2" which is the laptop address on the LAN it failed to connect and threw an error:

ER_HOST_NOT_PRIVILEGED: Host 'Anwar-PC' is not allowed to connect to this MySQL server

The code:

var connection = mysql.createConnection({
  host: 'localhost',
  user: 'user1',
  password: 'password1',
  database: 'database1'
});

And I created a user by doing the following:

drop user user1@localhost;
flush privileges;
create user user1@localhost identified by 'password1';
GRANT USAGE ON *.* to user1@localhost;
GRANT ALL ON database1.* TO user1@localhost;

Part 2, I would like to do the same scenario if both the raspberry pi and the database are connected to the Internet rather than LAN.

I hope that I explained the problem correctly. Thanks in advance.

Upvotes: 0

Views: 7002

Answers (2)

A.Kallas
A.Kallas

Reputation: 75

For Part 1: I checked the following question and the problem was solved

Host 'xxx.xx.xxx.xxx' is not allowed to connect to this MySQL server.

I think it will work the same way when using a raspberry pi.

Upvotes: 1

Yaman Jain
Yaman Jain

Reputation: 1247

Instead of localhost give the IP using cli

shell> mysql --host=192.168.1.2 --user=myname --password=mypass mydb OR

shell> mysql -h 192.168.1.2 -u myname -pmypass mydb

refer here

Upvotes: -1

Related Questions