Skip to navigation

Set up MySQL on a Banana Pi

MySQL is an SQL database which is often used for web applications.  MySQL is one of the most widely used databases, so it's worth learning how to use it.  Follow this link for a basic introduction to MySQL on Raspberry Pi servers.  I'm using Raspbian in these tutorials, but these instructions should work well on Ubuntu.

Install MySQL server and Python libraries

This command installs MySQL server, and the Python libraries necessary to write scripts that can access the database:

sudo apt-get install mysql-server python-mysqldb

You will be prompted to enter a password for the database server's root account.  When you've entered a password, you'll be prompted to enter it again for confirmation.  The MySQL server should start automatically after it's installed.

Secure MySQL

There are some settings that can be tweaked to improve the server's security.  The following command guides you through this process:



In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): <your password>
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] n
 ... skipping.

By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MySQL comes with a database named 'test' that anyone can access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
 ... Failed!  Not critical, keep moving...
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

Access the database from the command line

Make sure you can connect to the server using the MySQL command line client.  Type this command to start a MySQL shell:

mysql -u root -p

Enter the root password that you used when you installed MySQL.  We can use the command line client to create a database and create a MySQL user:

mysql> CREATE DATABASE my_db;
Query OK, 1 row affected (0.00 sec)

mysql> CREATE USER 'db_user'@'localhost' IDENTIFIED BY 'mypassword';
Query OK, 0 rows affected (0.00 sec)

The CREATE USER command creates a user named 'db_user' with password 'mypassword'.  This user is created with permission to make database requests from the same host where the database is running.  The next command grants all permissions to db_user to make requests to my_db from the localhost:

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

Query OK, 0 rows affected (0.00 sec)

mysql> quit

The FLUSH PRIVILEGES command saves the changes made using the GRANT command.

Accessing the database from a Python script

I've written a script that connects to the database server and performs a few basic operations like creating tables and adding records to those tables.  It's a very simple script that's intended to demonstrate how to use MySQL with Python.  You can download the script here.  Transfer it to your home directory on your Pi.  Make it executable and run it with these commands:

chmod +x 

The first time you run this script you'll see some warnings generated by the MySQL server:

./ Warning: Unknown table 'pages'
  curs.execute('DROP TABLE IF EXISTS pages')
./ Warning: Unknown table 'categories'
  curs.execute('DROP TABLE IF EXISTS categories')
./ Warning: Unknown table 'users'
  curs.execute('DROP TABLE IF EXISTS users')

When you run the script in future you should see output like this:

Setting up database...
Dropped tables

Created tables

Added User

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

The script deletes any tables that were created last time the script ran. Then it creates new tables, adds a record to one of them.  

Share this page:

comments powered by Disqus