Lesson

Install MySQL on Linux

10 min reading
Free Course

Install MySQL on Linux: Ubuntu and Debian Setup

Installing MySQL on Linux takes three package commands and a short security setup. Most Ubuntu and Debian servers include MySQL 8 in their default apt repositories, complete with automatic systemd service management.

Step 1: Install MySQL Server via APT

Update your local package index first so apt fetches the newest security patches, then install mysql-server:

sudo apt update
sudo apt install -y mysql-server

Enable and verify the systemd service so MySQL starts automatically whenever the server reboots:

sudo systemctl enable --now mysql
sudo systemctl status mysql

Step 2: Harden Security Settings

Run the built-in hardening wizard to disable anonymous users, restrict remote root logins, and remove the default test database:

sudo mysql_secure_installation

Answer Y to each prompt and set a password validation policy appropriate for your environment.

Step 3: Create a Dedicated Application User

Never connect your web application using the root account. Open the MySQL prompt with sudo mysql and create a least-privilege user tied to one database:

-- Create a utf8mb4 database for your application
CREATE DATABASE demo_app_db
    CHARACTER SET utf8mb4
    COLLATE utf8mb4_0900_ai_ci;

-- Create a dedicated local user with a strong password
CREATE USER 'demo_app_user'@'localhost'
    IDENTIFIED BY 'ChangeMe!Strong2026';

-- Grant privileges only on demo_app_db
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, INDEX
    ON demo_app_db.*
    TO 'demo_app_user'@'localhost';

FLUSH PRIVILEGES;

Common Linux Setup Gotchas

  • Root Auth Socket Confusion: On Ubuntu, root uses auth_socket by default, meaning sudo mysql works without a password while plain mysql -u root -p fails. Creating a dedicated application user avoids changing root auth.
  • Remote Connections Blocked: By default, bind-address in /etc/mysql/mysql.conf.d/mysqld.cnf is 127.0.0.1 for safety; keep it local unless connecting over a private VPC.

Frequently Asked Questions

How do I check which MySQL version is installed on Ubuntu?

Run mysql --version in the terminal or execute SELECT VERSION(); inside the MySQL prompt.

Where does MySQL store configuration files on Ubuntu Linux?

Server settings live in /etc/mysql/mysql.conf.d/mysqld.cnf and logs are written to /var/log/mysql/error.log.

Why should applications avoid connecting as root?

If an application credential leaks or suffers SQL injection, a scoped user limits damage to a single database instead of exposing every database on the host.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
MySQL Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum