Raspberry Pi OS (formerly Raspbian) is a versatile operating system for the Raspberry Pi, offering a wide range of functionalities. One of the most common use cases for the Raspberry Pi is as a web server, and MySQL is often used to manage databases for websites and applications.
MySQL being owned by Oracle has a dual license, with a public GPL version and a private business version and MariaDB is fully licensed under GPL. It means, MySQL is owned by Oracle, while MariaDB is entirely open-source. Performancewise, MariaDB is often faster than MySQL and can handle more work. Hence, we will focus on installing MariaDB on our Raspberry Pi OS to fully utilize it’s limited resources.
Prerequisites:
- A Raspberry Pi running Raspberry Pi OS (you can use Raspberry Pi 4 or any earlier model except Pi zero, though the 4 will offer the best performance).
- A stable internet connection.
Step 1: Update the Pi OS
First we will make sure that we have the lastest version of Pi OS installed on our Raspberry Pi so as to avoid it package conflicts using the following command:
sudo apt-get update && apt-get dist-upgrade -y
This will update the package list and install the latest updates for your Raspberry Pi OS.
Step 2: Installing MariaDB
Now that your system is up to date, it’s time to install MariaDB. Use the following command to install the MariaDB server:
sudo apt-get install mariadb-server -y
This will download and install the MariaDB server along with its required dependencies. The -y flag automatically answers “yes” to any prompts during the installation process.
Step 3: Securing the Installation Server
Once MariaDB is installed, it’s a good idea to run the mysql_secure_installation script to improve security. This script will help you set up a root password, remove insecure default settings, and improve the overall security of your database server installation. Run the following command:
sudo mysql_secure_installation
You’ll be prompted to configure the following:
- Enter current password for root (enter for none): Press Enter (since no password is set by default).
- Set a root password?: Choose “Y” to set a strong password for the database server root user.
- Remove anonymous users?: Choose “Y” to improve security.
- Disallow root login remotely?: Choose “Y” to restrict remote login for the root user (recommended for security).
- Remove test database and access to it?: Choose “Y” to remove the test database.
- Reload privilege tables now?: Choose “Y” to apply the changes.
Once the script has finished, the database server will be secured and ready for use.
Step 4: Verify the Installation
To verify that MariaDB has been successfully installed and is running, check its status with the following command:
sudo systemctl status mariadb
You should see output indicating that MariaDB is active and running. If the service isn’t running for some reason, you can start it using:
sudo systemctl start mariadb
If you want want the service to automatically start each time you start the device, you can just enable the service using the following command:
sudo systemctl enable mariadb
Step 5: Login and Check the server
To begin using MariaDB, log in to the MariaDB root account by typing:
sudo mysql -u root -p
You will be prompted for the root password that you set during the mysql_secure_installation process. After entering the password, you should be logged into the MySQL shell.
Once inside the MySQL shell, you can run SQL commands. For example, you can check the MariaDB version by running:
SELECT VERSION();
To exit the MySQL shell, type:
exit;
Using the above steps you can install and verify your installation for the MariaDB on your Raspberry Pi OS and you can access it locally from the device shell. To enable access to the server remotely, we need to make a few more changes which will cover in another article. We will also cover how to create specific users with specific database access.
With MariaDB running, you can start managing databases for your web applications, local projects, or even turn your Raspberry Pi into a lightweight server for other purposes.
Enjoy working with MySQL on your Raspberry Pi! If you encounter any issues, the MySQL logs (/var/log/mysql/error.log) can be helpful for troubleshooting.