VMware ESXi and vSphere Cluster Management

How to Install MySQL Server on Ubuntu Linux

Learn how to install MySQL Server on Ubuntu with APT, authenticate as an administrator, verify the service, and secure the installation.

MySQL is a relational database management system (RDBMS). It stores structured data in tables and uses SQL, or Structured Query Language, to create, query, and manage that data. Installing MySQL Server enables your Ubuntu computer to store databases locally, manage database users, and accept SQL queries from the command line or applications.

This procedure is for Ubuntu and other Debian-derived Linux distributions that use APT. Package names, service names, and commands may differ on distributions that use another package manager.

Prerequisites

  • Access to a terminal.
  • An account with sudo privileges. sudo runs an approved command with elevated administrative privileges.
  • An internet connection.
  • Configured package repositories. A package repository is a software source from which APT downloads packages and updates.

You should also be comfortable running basic Linux commands and entering your password when sudo requests it. The password is not displayed while you type.

Update the APT package index

APT is the package management tool used by Ubuntu and other Debian-based systems. First, refresh the local list of available packages:

sudo apt update

This command refreshes package metadata; it does not install MySQL. Updating first helps APT locate the current package version and its dependencies.

Install MySQL Server

Install the MySQL server package with:

sudo apt-get install mysql-server

APT resolves and installs the required dependencies along with the MySQL Server package. When APT displays a confirmation such as Do you want to continue? [Y/n], type Y and press Enter.

The equivalent modern APT syntax is:

sudo apt install mysql-server

Both commands install the Ubuntu-provided MySQL server package. The installation may configure and start the service automatically.

Understand the administrator authentication prompt

Depending on the Ubuntu release and MySQL package version, setup may ask you to create or confirm a password for the MySQL administrative account. If a password prompt appears, enter a strong password, press Enter, and enter it again when asked to confirm it.

The MySQL administrative account is commonly named root, but it is separate from the Linux root account. The two identities control different systems:

AccountControlsAuthentication contextWhy the distinction matters
Linux root userThe operating system, files, processes, and servicesLinux login and privilege mechanisms, often reached with sudoLinux root privileges do not automatically mean that a MySQL password is configured
MySQL root administrative accountMySQL databases, users, privileges, and server settingsMySQL authentication, such as a password or Unix socket authenticationIt is a database identity and does not grant control over Linux files or processes

Newer Ubuntu packages may configure the MySQL administrative account for Unix socket authentication. Unix socket authentication is a local method that allows an authorized operating-system user to authenticate to MySQL through the local socket without entering a MySQL password at the prompt. Therefore, an installation that does not ask for a MySQL root password can be behaving normally.

Installation and verification commands

GoalCommandExpected result
Refresh package metadatasudo apt updateAPT downloads current package information
Install MySQL Serversudo apt-get install mysql-serverAPT installs MySQL Server and required dependencies
Check service statussudo systemctl status mysqlThe service status shows whether MySQL is active
Connect locally as administratorsudo mysqlThe MySQL command-line client opens using local socket authentication when configured
Check server versionSELECT VERSION();MySQL returns the running server version
Run security configurationsudo mysql_secure_installationAn interactive hardening utility starts

Verify that MySQL is running

Check the service managed by systemd. systemd is the service manager used by modern Ubuntu releases.

sudo systemctl status mysql

Look for a status such as active (running). The status view may open in a pager; press q to return to the shell.

If the installation uses Unix socket authentication, connect locally as the MySQL administrator with:

sudo mysql

At the mysql> prompt, request the server version:

SELECT VERSION();

A version value confirms that the client connected to the running server and that the server accepted a SQL query. Leave the MySQL client cleanly with:

exit

If password authentication has been configured for the MySQL administrative account, use:

mysql -u root -p

Enter the MySQL account password when prompted, then run SELECT VERSION(); and exit.

Secure the new installation

Run the post-installation security utility:

sudo mysql_secure_installation

Review every prompt instead of accepting settings blindly. Depending on the package version and current configuration, the utility can help you:

  • Remove anonymous MySQL accounts.
  • Disable unnecessary remote administrative access.
  • Remove the default test database and related access.
  • Reload privilege tables so security changes take effect.

Choose a secure administrator authentication method. If password authentication is used, select a strong, unique password. For a local development server, Unix socket authentication can be appropriate because it limits administrative access to authorized local operating-system users. Avoid exposing an administrative account to remote clients unless there is a specific, well-controlled requirement.

Manage the MySQL service with systemd

Use these commands to control the MySQL service:

sudo systemctl status mysql
sudo systemctl start mysql
sudo systemctl stop mysql
sudo systemctl restart mysql
sudo systemctl enable mysql
  • status displays the current service state.
  • start starts MySQL if it is stopped.
  • stop stops the server.
  • restart stops and starts the service again. Use it after a configuration change when the change requires a service restart.
  • enable configures MySQL to start automatically when the system boots. Enabling a service does not necessarily start it immediately; use start if needed.

Troubleshoot common problems

APT cannot locate the mysql-server package

Refresh package metadata with sudo apt update. If the package is still unavailable, check that the required Ubuntu repositories are enabled and confirm that the system is Ubuntu or another compatible Debian-derived distribution. A non-APT distribution requires its own package manager and package names.

The installation reports insufficient permission

Run the installation command through an account authorized to use sudo. If your account is not authorized, ask a system administrator to perform the installation or grant appropriate access. Do not work around the permission check by changing ownership of system directories.

The installer does not request a MySQL root password

This is common when the Ubuntu package configures Unix socket authentication. Try:

sudo mysql

Only change the authentication method if an application or administrative workflow specifically requires password-based access.

mysql -u root -p returns access denied

The MySQL root account may use socket authentication rather than password authentication, or the supplied password may be incorrect. Try local access with sudo mysql. Before changing credentials, confirm which authentication method the account is intended to use.

MySQL is installed but not running

Inspect the service state:

sudo systemctl status mysql

Look for an error describing a configuration problem, failed startup, or port conflict. Resolve the reported problem, then start or restart the service:

sudo systemctl start mysql
sudo systemctl restart mysql

A remote client cannot connect

A default installation may listen only on a local interface. Remote connections can also be blocked by a firewall or denied because the MySQL user has no privileges for the remote host. Treat remote access as an advanced configuration task: verify the network binding, firewall policy, and host-specific MySQL privileges, and avoid exposing the server unnecessarily.

Next steps

After the service is active and the security setup is complete, MySQL is ready for database and user creation. Continue by learning how to connect with the MySQL command-line client, create databases and tables, create application users with limited privileges, and configure an application with the correct host, port, database name, username, and authentication method.

For this installation procedure, see Install MySQL on Linux.