Install MySQL on Linux (Ubuntu)
Learn how to install the MySQL server on Ubuntu with APT, handle initial authentication prompts, and verify the service from the terminal.
This lesson explains how to install MySQL Server on an Ubuntu-based Linux system. MySQL is a relational database management system that stores and serves structured data. The installation uses Ubuntu's APT package manager from a terminal.
You need a Linux account with sudo privileges. You also need network access to the configured Ubuntu package repositories so APT can download MySQL and its dependencies.
Installation requirements
| Requirement | Why it is needed |
|---|---|
| Ubuntu or compatible Debian-based Linux system | The instructions use Ubuntu's Debian-style packages and APT tools. |
| Terminal access | Commands are entered from a shell. |
| sudo privileges | Installing system software and controlling services require administrative permissions. |
| Configured package repositories or internet access | APT must obtain the MySQL package and its dependencies. |
If you are new to terminal commands, review starting the command-line interface first. The Linux root user mentioned later is the MySQL administrative account; it is distinct from the Linux root account.
Install the MySQL server package
- Open a terminal.
- Run the installation command:
sudo apt-get install mysql-serversudo runs the operation with elevated privileges. apt-get is an APT package-management tool, and mysql-server is the Ubuntu package name for the MySQL server.
APT resolves dependencies, calculates the required downloads, and displays a summary of the packages that will be installed. Review the list and the amount of disk space involved. When APT asks whether you want to continue, enter Y and press Enter.
Do you want to continue? [Y/n]During installation, APT may configure and start the MySQL service automatically. The installed software includes the MySQL Server process, which is the database service running on your Linux machine.
Respond to initial setup prompts
MySQL package behavior can differ between Ubuntu releases and package configurations. Some installations display a prompt for the MySQL root password. If it appears, choose a strong password, enter it when requested, and enter it a second time to confirm it exactly.
Other current Ubuntu installations configure the MySQL administrative root account to use local socket authentication. This method authenticates a local administrative connection through the operating system socket and can allow a privileged Linux user to access MySQL without entering a separate MySQL password. In this setup, no root-password prompt is displayed.
| Installer behavior | What the learner should do |
|---|---|
| Password prompt appears | Enter a strong MySQL root password, then enter it again when prompted. |
| No password prompt appears because local socket authentication is configured | Do not assume the installation failed. Use sudo mysql for local administrative access. |
Confirm that MySQL is installed
Check the service state
Use systemctl to inspect the MySQL system service:
sudo systemctl status mysqlLook for a state such as active (running). The service name is mysql, even though the package name is mysql-server.
If the service is installed but not running, start it with:
sudo systemctl start mysqlRun the status command again to confirm that it is active.
Connect locally as an administrator
On systems using socket authentication, open a local administrative MySQL session with:
sudo mysqlA successful connection displays a MySQL prompt. To confirm that the server responds, enter a simple query:
SELECT VERSION();Finish the session with:
exitIf the installer configured password-based authentication instead, connect by specifying the MySQL root account and requesting its password:
mysql -u root -pEnter the MySQL root password when prompted. Once connected, the same SELECT VERSION(); query verifies that the server is responding. For more command-line connection practice, see Access MySQL.
Troubleshooting
APT asks for confirmation
APT is waiting for approval to download and install MySQL and its dependencies. Enter Y and press Enter to continue. If the package list is unexpected, enter N or press Ctrl+C, review the output, and investigate before continuing.
The installer asks for a root password
This usually means the package version or setup flow uses password-based authentication for the MySQL root account. Choose a strong password, type it carefully, and repeat it exactly when requested. Store it securely.
No root-password prompt appears
This is commonly expected when Ubuntu configures local socket authentication. Use sudo mysql for local administrative access rather than assuming that you missed a password prompt.
The MySQL service is not running
First inspect its state:
sudo systemctl status mysqlTry starting it:
sudo systemctl start mysqlIf it still fails, read the status details and service logs to identify the startup problem. A service that is not active cannot accept local database connections.
APT cannot find mysql-server
APT may have stale package metadata, unavailable repositories, or no network connection. Refresh the package metadata and try the installation again:
sudo apt-get update
sudo apt-get install mysql-serverAlso verify that the standard Ubuntu repositories are configured and that the machine can reach them.
What to remember
- Ubuntu uses APT to install the
mysql-serverpackage. - The installation requires sudo privileges and access to configured package repositories.
- APT resolves dependencies and asks you to approve the installation with
Y. - Some configurations request a MySQL root password twice; others use local socket authentication instead.
- Use
sudo systemctl status mysqlto check the service andsudo mysqlto test local administrative access on socket-authenticated systems.
After installation, you can continue with creating a database, creating a MySQL user, or learning how to query a database.