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.
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
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.
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;
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.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.Run mysql --version in the terminal or execute SELECT VERSION(); inside the MySQL prompt.
Server settings live in /etc/mysql/mysql.conf.d/mysqld.cnf and logs are written to /var/log/mysql/error.log.
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.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.