How to Install MySQL on Ubuntu
Install MySQL on Ubuntu with sudo apt update then sudo apt install mysql-server. Ubuntu starts the service and gives root socket authentication, so run sudo mysql, set a password with ALTER USER, then mysql_secure_installation. You know it worked when systemctl status mysql shows active (running) and mysql -u root -p connects.
For developers setting up MySQL 8.0 on an Ubuntu 22.04 or 24.04 VPS.
knowledge
- Read time
- 5 min
- Sections
- 8
- Updated
- 14 Nov 2026
- Written by
- WebJi® team
Last editorial update: 15 November 2026.
What the Ubuntu mysql-server package actually installs
On Ubuntu 24.04 and 22.04, the mysql-server package installs MySQL 8.0 Community Server, a systemd unit named mysql.service, and the command-line client. The service starts on its own, listens on 127.0.0.1:3306, and the root account is configured for socket authentication with no password, which is deliberate Ubuntu packaging, not an oversight.
- Configuration:
/etc/mysql/my.cnfand/etc/mysql/mysql.conf.d/mysqld.cnf - Data directory:
/var/lib/mysql— copy it only while the service is stopped - Error log:
/var/log/mysql/error.log;journalctl -u mysqlshows the same events - root@localhost: authenticates through the Unix socket, which is why no password prompt ever appears after install
That last point explains most of the confusion around this install. The root account is locked to local socket login until you change it, and sudo mysql is the way in until then.
Install the server and confirm it is running
Three commands take a fresh Ubuntu 22.04 or 24.04 machine to a running MySQL 8.0 server. Run them in order and read the output of the third before going further. If you are doing this on a new VPS, add a sudo user first so you are not working as root throughout — the steps are in our guide to adding a sudo user on Ubuntu.
sudo apt update
sudo apt install mysql-server
systemctl status mysql
You want Active: active (running) and a main PID for mysqld. Confirm the port with sudo ss -tlnp | grep 3306; on a default install it should show 127.0.0.1:3306, not 0.0.0.0:3306.
If the unit is inactive, start it with sudo systemctl start mysql and read sudo journalctl -u mysql -n 50. A failed first start is almost always a data directory that already holds files from an older install, and the log line names the file it refused.
Set the root password Ubuntu never asks for
Log in with sudo mysql, which uses the socket and needs no password, then change the account so it also accepts a password. MySQL 8.0's default authentication plugin is caching_sha2_password, so name it explicitly in the statement:
sudo mysql
ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'your-strong-password';
FLUSH PRIVILEGES;
exit
Now run sudo mysql_secure_installation. Per the MySQL 8.0 manual, it sets a password for root, removes anonymous-user accounts, removes root accounts reachable from outside the local host, and drops the test database, with an optional validate_password component you can accept or decline. Answer yes to the removal steps on any server that faces the internet.
From this point mysql -u root -p works and sudo mysql may stop working, because the socket plugin is no longer the account's method. Store the password in a password manager before closing the terminal — recovery otherwise means restarting mysqld with skip-grant-tables.
Create the database and user your application needs
Applications should never connect as root; create one database and one user per app and grant only that schema. Four statements do the whole job:
CREATE DATABASE app_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'app'@'localhost' IDENTIFIED BY 'another-strong-password';
GRANT ALL PRIVILEGES ON app_db.* TO 'app'@'localhost';
FLUSH PRIVILEGES;
utf8mb4is the charset WordPress and most PHP applications expect.'app'@'localhost'keeps the account local to the machine.- Verify with
SHOW GRANTS FOR 'app'@'localhost';— that output is the check that matters.
Test the account from the shell with mysql -u app -p app_db. It worked when the prompt changes to mysql> and SHOW TABLES; returns an empty set rather than an access-denied error. If your site runs on shared hosting instead of your own server, the same task is done from the panel — see creating a MySQL database in cPanel.
Open port 3306 only when you mean to
A default MySQL install is not reachable from another machine, and that is the correct state for a web server. If a remote client genuinely needs access, change one setting and restrict the source address at the same time.
In /etc/mysql/mysql.conf.d/mysqld.cnf, set bind-address = 0.0.0.0 only if the database must answer on a public interface, or set the private IP when the app server sits on the same network. Restart with sudo systemctl restart mysql, then allow the port from one address:
sudo ufw allow from 203.0.113.10 to any port 3306
Remote accounts also need a host part that matches: CREATE USER 'app'@'203.0.113.10' is safer than 'app'@'%'. A database port open to the whole internet is found by scanners within hours, so pair any exposure with a firewall rule and strong passwords.
Errors you will hit, and the fix
Five messages account for nearly every failed MySQL install on Ubuntu. Read the exact text, apply the fix, and retest before touching anything else.
| Message | Cause | Fix |
|---|---|---|
| Access denied for user 'root'@'localhost' | Root uses socket authentication, not a password | Use sudo mysql, then ALTER USER to add a password if you need one |
| Can't connect to local MySQL server through socket | The service is stopped or crashed on start | sudo systemctl start mysql, then read journalctl -u mysql |
| Unit mysql.service not found | MariaDB is installed and owns the unit name | Check with dpkg -l, purge MariaDB, then reinstall mysql-server |
| ERROR 1819 password policy | validate_password is enforcing length and mix | Choose a longer password or lower policy with SET GLOBAL validate_password.policy=LOW |
| mysql: command not found | Only the server package is present | sudo apt install mysql-client |
One more worth knowing: a start that fails after a disk resize usually points at /var/lib/mysql ownership. MySQL must own that directory, and sudo chown -R mysql:mysql /var/lib/mysql fixes the permission-denied line the log reports.
Back up before you tune anything
Take a logical dump before changing configuration, because a dump is portable across versions and a copied data directory is not.
mysqldump --single-transaction --routines --triggers app_db > app_db.sql
mysql -u app -p app_db < app_db.sql
Then tune one value at a time. innodb_buffer_pool_size changes performance most on a small server; aim for roughly half the RAM on a database-only machine, less on one that also runs the web stack. Restart, watch SHOW GLOBAL STATUS for a day, and revert if the change did not help. For a broader strategy, the 3-2-1 backup rule explained for websites shows where these dumps fit.
Where this database should run
A database on the same server as the site is fine until memory pressure arrives; then MySQL and PHP compete for the same RAM and both slow down. If you are installing MySQL on a box that also serves sites, watch free memory and swap before adding another database.
WebJi® VPS hosting starts at ₹499 a month before 18% GST, with each configuration scoped and quoted individually so you get the RAM your database needs, and separate MySQL database hosting exists for the split where the database moves off the web server. Support is available 24/7 by email at support@webji.services in Hindi and English, with a published average first reply under 60 minutes.
Frequently asked questions
Why does sudo mysql work when mysql -u root -p fails?
Ubuntu configures root@localhost with the auth_socket plugin, which trusts the local Unix socket instead of a password. sudo mysql satisfies that check. To switch to password login, run ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'your-password'; then FLUSH PRIVILEGES;
How do I install MySQL on Ubuntu 24.04 specifically?
Run sudo apt update, then sudo apt install mysql-server. Ubuntu 24.04 installs MySQL 8.0, enables mysql.service, and starts it on 127.0.0.1:3306. Confirm with systemctl status mysql, then reach root through sudo mysql because of socket authentication.
How do I reset the MySQL root password if socket login is already gone?
Stop MySQL, restart mysqld with skip-grant-tables, connect without a password, run the ALTER USER statement with a new password, then restart the service normally. Keep the new password in a password manager immediately; this recovery path is the slow alternative.
How do I allow remote connections to MySQL on Ubuntu?
Set bind-address in /etc/mysql/mysql.conf.d/mysqld.cnf to the private IP you want, restart mysql, create the user with that host instead of localhost, and allow port 3306 from that one address in ufw. Never open 3306 to every source.
Where is the MySQL error log on Ubuntu?
The error log is /var/log/mysql/error.log. The same startup failures appear in the systemd journal via sudo journalctl -u mysql. Restart with sudo systemctl restart mysql and check both to confirm the service came back cleanly.
Last editorial update: 15 November 2026. Prices follow the dated sources on this page; checkout confirms current charges.
Ready to get started with WebJi®?
Join 12,000+ businesses across India and overseas. INR pricing, 18% GST shown before payment and 24/7 email support in Hindi and English — average first reply under 60 minutes.