Blog · WebJi® India

How to Install MySQL on Ubuntu

Quick answer: 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.
✓ In business since 2005✓ INR + GST billing✓ 24/7 Email Support — avg reply < 60 min
Indian trader monitoring candlestick charts on multiple screens in evening light — how to install mysql on ubuntu

What the Ubuntu package actually installs

The mysql-server package on Ubuntu 24.04 and 22.04 installs MySQL 8.0 Community Server, a systemd unit called mysql.service, and a command-line client. The service starts on its own, listens on 127.0.0.1:3306, and the root account is set to socket authentication with no password at all.

  • Configuration lives in /etc/mysql/my.cnf and /etc/mysql/mysql.conf.d/mysqld.cnf.
  • Data files live in /var/lib/mysql. Copy that folder only while the service is stopped.
  • The error log is /var/log/mysql/error.log, and journalctl -u mysql shows the same events.
  • root@localhost authenticates through the Unix socket, which is why a password prompt never appears after install.

That last point explains most of the confusion around this install. On Ubuntu the root account is not password-less by mistake; it is deliberately locked to local socket login, and it stays that way until you change it.

Install the server and confirm it is running

Three commands take you from a fresh Ubuntu 22.04 or 24.04 machine to a running server. Run them in order and read the output of the third before you go further.

  1. Run sudo apt update to refresh the package index, then sudo apt install mysql-server. The install pulls in the server, client, and shared libraries.
  2. Check the unit with systemctl status mysql. You want Active: active (running) and a main PID for mysqld.
  3. 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 then 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. From the mysql> prompt run ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'your-strong-password'; and exit.

Now run sudo mysql_secure_installation. It asks whether to keep the password you just set, whether to remove anonymous users, whether to disallow remote root login, whether to drop the test database, and whether to reload the grant tables. Answer yes to the last four 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. Keep the password in a password manager before you close the terminal, since recovery means starting mysqld with skip-grant-tables.

Create the database and user your application needs

Applications should not connect as root. Create one database and one user per app, then grant only that database. The five statements below are the whole job.

  • CREATE DATABASE app_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; sets the charset WordPress and most PHP apps expect.
  • CREATE USER 'app'@'localhost' IDENTIFIED BY 'another-strong-password'; keeps the account local to the machine.
  • GRANT ALL PRIVILEGES ON app_db.* TO 'app'@'localhost'; limits the user to one schema.
  • FLUSH PRIVILEGES; applies the grant immediately on MySQL 8.
  • SHOW GRANTS FOR 'app'@'localhost'; prints exactly what you granted, which is the check that matters.

Test the new 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 instead of an access-denied error.

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 it to 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 with sudo ufw allow from 203.0.113.10 to any port 3306.

Remote accounts 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 a strong password policy.

Errors you will hit, and the fix

Five messages account for nearly every failed MySQL install on Ubuntu. Read the exact text, make the change in the right-hand column, and retest before touching anything else.

MessageCauseFix
Access denied for user 'root'@'localhost'Root uses socket authentication, not a passwordUse sudo mysql, then ALTER USER to add a password if you need one
Can't connect to local MySQL server through socketThe service is stopped or crashed on startsudo systemctl start mysql, then read journalctl -u mysql
Unit mysql.service not foundMariaDB is installed and owns the mysql unit nameCheck with dpkg -l, purge MariaDB, then reinstall mysql-server
ERROR 1819 password does not satisfy current policyvalidate_password is enforcing length and character mixChoose a longer password or lower the policy with SET GLOBAL validate_password.policy=LOW
mysql: command not foundOnly the server package is presentsudo 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 line the log reports as permission denied.

Back up before you tune anything

Take a logical dump before you change 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 gives you one restorable file, and mysql -u app -p app_db < app_db.sql puts it back.

Then tune one value at a time. innodb_buffer_pool_size is the setting that changes performance most on a small server, and it should be roughly half the RAM on a database-only machine, not on one that also runs the web stack. Restart, watch SHOW GLOBAL STATUS for a day, and revert if the change did not help.

Where this database should run

A database on the same VPS 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 server you also use for hosting, watch free memory and swap before you add another site.

WebJi sells VPS servers and separate MySQL database hosting for that split, and both are quoted by email at support@webji.services before payment rather than sold from a fixed price table. Support is 24/7 by email in Hindi and English with an average first reply under 60 minutes.

Frequently Asked Questions

How do I install MySQL on Ubuntu 24.04?

Run sudo apt update, then sudo apt install mysql-server. Ubuntu installs MySQL 8.0, enables mysql.service, and starts it on 127.0.0.1:3306. Confirm with systemctl status mysql, then run sudo mysql to reach the root account through socket authentication.

Why does sudo mysql work when mysql -u root -p fails?

Ubuntu configures root@localhost to authenticate with the auth_socket plugin, which trusts the local Unix socket instead of a password. sudo mysql satisfies that check. To use a password instead, run ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'your-password'.

How do I set or reset the MySQL root password on Ubuntu?

Log in with sudo mysql while socket authentication still works, then run ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'new-password' and FLUSH PRIVILEGES. If socket login is already gone, restart mysqld with skip-grant-tables, change the account, and restart normally.

How do I create a database and user in MySQL?

Run CREATE DATABASE app_db CHARACTER SET utf8mb4; then CREATE USER 'app'@'localhost' IDENTIFIED BY 'password'; then GRANT ALL PRIVILEGES ON app_db.* TO 'app'@'localhost'; and FLUSH PRIVILEGES. Verify with SHOW GRANTS FOR 'app'@'localhost'.

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.

How do I restart MySQL and where is the error log?

Restart with sudo systemctl restart mysql and check the result with systemctl status mysql. The error log is /var/log/mysql/error.log, and sudo journalctl -u mysql shows the same startup failures in the systemd journal.

Need help with this?

WebJi® support does it for you — free. Talk to our Jaipur team →