Skip to navigation

Setting up remote access to a MySQL database

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 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. will run on that Pi and access the database on the original Pi via the local network. 

Set up the MySQL client

Set up another Banana Pi with Raspbian, and give it a static IP address.  In my case, the server's IP address is, so I set the client Pi's IP address to  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, replace 'localhost' with the IP address of your MySQL server:

    conn = MySQLdb.connect('', 'db_user', 'mypassword', 'my_db')

Save this modification, and transfer the file to the client Pi using the scp command:

scp ./ bananapi@ will be copied to /home/bananapi on the second Pi.  Log into the client node via ssh, and make the script executable:

ssh bananapi@
chmod +x

Install the MySQL client and Python libraries on the client:

sudo apt-get install python-mysqldb mysql-client

Change settings on the MySQL server

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:

sudo leafpad  /etc/mysql/my.cnf 

Look for the bind address and change it to 

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address        =

Restart the MySQL server:

sudo service mysql restart
[ ok ] Stopping MySQL database server: mysqld.
[ ok ] Starting MySQL database server: mysqld . ..
[info] Checking for tables which need an upgrade, are corrupt or were 
not closed cleanly..

Change the database user settings to allow remote access for user db_user:

mysql -u root -p

mysql> GRANT ALL ON my_db.* TO 'db_user'@'' IDENTIFIED BY 'mypassword';
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> quit

The FLUSH PRIVILEGES command commits the changes to the datbase.  

Run the script on the client

Log into the client node via ssh:

ssh bananapi@

Now run the database script on the client:

Setting up database...
Dropped tables

Created tables

Added User

('Steve', 'password', '', datetime.datetime(2014, 8, 30, 0, 0))
DB Connection closed

You should see the same output as when you ran the script on the server.  

Share this page:

comments powered by Disqus