Skip to content

MySQL is a popular open-source relational database management system, well-suited for web and server applications. Installing it on a Raspberry Pi can turn this compact device into a robust server for managing data. This guide provides a straightforward approach to setting up MySQL on a Raspberry Pi.

Update and upgrade your system

Before proceeding, ensure your Raspberry Pi is up to date:

sudo apt update
sudo apt upgrade

Install MySQL Server

To install MySQL, use the following command:

sudo apt install mysql-server

After installation, secure your MySQL installation:

sudo mysql_secure_installation

Follow the prompts to set a root password and configure security options.

Access MySQL as the root user

Initially, access MySQL with the root user:

sudo mysql -u root -p

Enter the root password when prompted.

Create a new database user

For security reasons, it’s recommended to create a new user:

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;

Replace username and password with your preferred credentials.

Create a new database

Create a database for your applications:

CREATE DATABASE exampledb;

Replace exampledb with your desired database name.

Access the database

To access and manage your database, log in with your new user:

mysql -u username -p

Enter the password when prompted.

Manage MySQL service

To control the MySQL service, use the following commands:

sudo systemctl start mysql    # To start MySQL service
sudo systemctl stop mysql     # To stop MySQL service
sudo systemctl enable mysql   # To enable MySQL on boot
sudo systemctl disable mysql  # To disable MySQL on boot

Conclusion

With MySQL installed on your Raspberry Pi, you now have a powerful tool for managing data. Whether for web applications, IoT projects, or learning SQL, your Raspberry Pi is now ready for database management tasks.

Written by

Robert Cooper avatar

Robert Cooper

Senior Engineer at Basedash

Robert Cooper is a senior engineer at Basedash who builds full-stack product systems across SQL data infrastructure, APIs, and frontend architecture. His work focuses on application performance, developer velocity, and reliable self-hosted workflows that make data operations easier for teams at scale.

View full author profile →

Looking for an AI-native BI tool?

Basedash lets you build charts, dashboards, and reports in seconds using all your data.