How do I open up my MySQL on my Raspberry Pi for Outside / Remote Connections?
Asked Answered
O

3

27

I have a Raspberry Pi that stores temperature data for homebrewing activity. I am making a Spring MVC application on my computer and I want to tap the data. Both my Pi and my computer are on the local network. I can SSH and FTP into my RPi perfectly.

mysql --192.168.1.102 --u root -p db

Causes a "Can't connect to MySQL server on '192.168.1.102'".

My Java application isn't connecting either, obviously.

SHOW VARIABLES WHERE VARIABLE_NAME = 'port' ;

returns the default port, 3306.

Is there a setting that must be enabled to allow remote connections into MySQL?

Obnubilate answered 11/9, 2013 at 6:7 Comment(0)
O
45

I have recently had the same problem myself. I got it working by doing the following:

Edit MySQL configuration

By default, MySQL is not configured to accept remote connections. You can enable remote connections by modifying the configuration file:

sudo nano /etc/mysql/my.cnf

Find the [mysqld] section. The line you need to alter is bind-address, which should be set to the default value of 127.0.0.1. You want to edit this line to instead show the IP of your RPi on the network (which would seem to be 192.168.1.102 from your example). Write the changes.

Restart the MySQL service

sudo service mysql restart

Setup MySQL permissions

Connect to your MySQL instance as root:

mysql -p -u root

Create a user:

CREATE USER '<username>'@'<ip_address>' IDENTIFIED BY '<password>';  
  • The apostrophes ( ' ) in the syntax are required
  • The IP address is the IP address of the device on the network you are trying to connect from

Grant permissions to the relevant databases and tables:

GRANT ALL PRIVILEGES ON <database>.* TO '<username>'@'<ip_address>' IDENTIFIED BY '<password>';
  • The parameters are those you used to create your user in the previous step
  • The * will grant access to all tables within the specified database. Alternatively you could specify a specific table
  • You'd probably want to firm up the security by only granting relevant privileges, but that should be enough to test that it works

That should hopefully do it!

Ornithic answered 21/9, 2013 at 16:29 Comment(2)
Why is the whole of stack overflow filled with bullshit when the underappreciated answer is right here?Hispanic
Don't you need to do in addition flush privileges; ?Triplet
D
6

The following worked for me, courtesy of a comment found on this instructable:

  1. Grant access to your remote machine using: GRANT ALL ON *.* TO 'root'@'192.168.1.%' IDENTIFIED BY 'your_password_here'; (I used 192.168.1.% so that any computer on my network can connect to it)
  2. Go into the my.cnf file (sudo nano /etc/mysql/my.cnf) file and look for "bind-address" and comment this out (put a # in front of the line)
  3. Reload MySQL config (service mysql reload)
  4. Restart MySQL server (service mysql restart)
Dimorph answered 29/1, 2014 at 22:3 Comment(0)
M
4

If your issue is not able to remotely connect with MySQL on Raspberry Pi, then try below steps. I had the same issue and got it resolved by performing below commands.

1) sudo nano /etc/mysql/my.cnf

2) # bind-address = 127.0.0.1 // comment this line out

bind-address = 0.0.0.0          //add this line just below above line 

3) sudo /etc/init.d/mysql restart //restart mysql

4) sudo mysql -u root -p //login to mysql cli as user 'root'

5) GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'beta' WITH GRANT OPTION;

Here 'root' is the mysql user and 'beta' is the password and here privileges are set for 'root'.Change it accordingly and execute above query in mysql cli.

Good Luck !!!

Mcadoo answered 10/10, 2016 at 9:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.