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.cnfand/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/, andmysql/error. log journalctl -u mysqlshows 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.
- Run
sudo apt updateto refresh the package index, thensudo apt install mysql-server. The install pulls in the server, client, and shared libraries. - Check the unit with
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 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_ and exit.
Now run sudo mysql_secure_. 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_sets the charset WordPress and most PHP apps expect.unicode_ ci; CREATE USER 'app'@'localhost' IDENTIFIED BY 'another-keeps the account local to the machine.strong-password' ; 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/, 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. 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.
| Message | Cause | Fix |
|---|---|---|
| Access denied for user 'root'@'l | Root uses socket authent | 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 mysql unit name | Check with dpkg -l, purge MariaDB, then reinstall mysql-server |
| ERROR 1819 password does not satisfy current policy | validate_ | Choose a longer password or lower the policy with SET GLOBAL validate_ |
| 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 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- 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_ 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.
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_
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_
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/
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/

