Install MySQL on Linux
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,
rootusesauth_socketby default, meaningsudo mysqlworks without a password while plainmysql -u root -pfails. Creating a dedicated application user avoids changingrootauth. - Remote Connections Blocked: By default,
bind-addressin/etc/mysql/mysql.conf.d/mysqld.cnfis127.0.0.1for 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.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.