In the previous article about setting up MySQL on a Banana Pi I covered the basics of setting up MySQL, and I used a script called db-script.py to access the database. The next step is to set up remote access so that the database can be used from a second Banana Pi. Db-script.py will run on that Pi and access the database on the original Pi via the local network.
Set up another Banana Pi with Raspbian, and give it a static IP address. In my case, the server's IP address is 192.168.0.8, so I set the client Pi's IP address to 192.168.0.9. I have a monitor, keyboard and mouse connected to the server, so I'm going to use ssh to login to the client.
On the server, modify the script used to access the database. On line 73 of db-script.py, replace 'localhost' with the IP address of your MySQL server:
Save this modification, and transfer the file to the client Pi using the scp command:
Db-script.py will be copied to /home/bananapi on the second Pi. Log into the client node via ssh, and make the script executable:
Install the MySQL client and Python libraries on the client:
You need to edit the settings for the MySQL server to allow it to accept requests from other IP addresses besides the loopback address. On the Pi running the MySQL server, open the MySQL configuration file in a text editor:
Look for the bind address and change it to 0.0.0.0:
Restart the MySQL server:
Change the database user settings to allow remote access for user db_user:
The FLUSH PRIVILEGES command commits the changes to the datbase.
Log into the client node via ssh:
Now run the database script on the client:
You should see the same output as when you ran the script on the server.
Share this page: