MySQL on Raspberry Pi – allow connections from non-localhost

If you want to connect to MySQL from any device in your network that isn’t on localhost, then you may encounter an error:

MySQL error: Could not connect to any of the specified hosts

Bear in mind that e.g., a PHP web app or phpmyadmin will continue to work, since (assuming your web server such as Apache is running on the same server as MySQL) they’re technically running on localhost and will be able to connect.

In order to allow this you need to edit your

my.cnf

file, which, in a Raspberry Pi (and I assume therefore Debian) scenario is in a slightly unusual place.

sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf

In here, look for the ‘bind-address’ property.

If you change this to 0.0.0.0 then MySQL will accept TCP connections from anywhere.

Then restart MySQL:

sudo /etc/init.d/mysql restart

(Clearly in this scenario, you then need to ensure your firewall is configured properly to only allow connections from specified places.)

Be the first to comment

Leave a Reply

Your email address will not be published.


*