Install MySQL in Ubuntu: A comprehensive Guide

install mysql in ubuntu

Introduction how to install mySql in ubuntu

Are you looking to install MySQL on your Ubuntu system? MySQL is a widely used open-source relational database management system that allows you to store and manage large amounts of data efficiently. In this article, we will guide you through the process of installing MySQL on Ubuntu, ensuring that you have a fully functional database server up and running in no time.

Prerequisites

Before we begin the installation process, there are a few prerequisites you need to ensure are in place:

  • A system running Ubuntu operating system
  • Administrative access to the system

Updating the System

It is always recommended to update the system before installing any new software. Open a terminal and execute the following commands:

sudo apt update
sudo apt upgrade

Command: sudo apt update output

Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:4 http://archive.ubuntu.com/ubuntu focal-backports InRelease [101 kB]
Get:5 http://archive.ubuntu.com/ubuntu focal/main Sources [829 kB]
Get:6 http://archive.ubuntu.com/ubuntu focal/restricted Sources [5,820 B]
Get:7 http://archive.ubuntu.com/ubuntu focal/universe Sources [9,982 kB]
Get:8 http://archive.ubuntu.com/ubuntu focal/multiverse Sources [200 kB]
Get:9 http://archive.ubuntu.com/ubuntu focal/main amd64 Packages [1,275 kB]
Get:10 http://archive.ubuntu.com/ubuntu focal/main Translation-en [595 kB]
Get:11 http://archive.ubuntu.com/ubuntu focal/restricted amd64 Packages [33.4 kB]
Get:12 http://archive.ubuntu.com/ubuntu focal/restricted Translation-en [5,692 B]
Get:13 http://archive.ubuntu.com/ubuntu focal/universe amd64 Packages [11.8 MB]
Get:14 http://archive.ubuntu.com/ubuntu focal/universe Translation-en [6,361 kB]
Get:15 http://archive.ubuntu.com/ubuntu focal/multiverse amd64 Packages [179 kB]
Get:16 http://archive.ubuntu.com/ubuntu focal/multiverse Translation-en [144 kB]
Get:17 http://archive.ubuntu.com/ubuntu focal-updates/main Sources [302 kB]
Get:18 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [805 kB]
Get:19 http://archive.ubuntu.com/ubuntu focal-updates/main Translation-en [303 kB]
Get:20 http://archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [1,688 B]
Get:21 http://archive.ubuntu.com/ubuntu focal-updates/restricted Translation-en [1,668 B]
Get:22 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [1,089 kB]
Get:23 http://archive.ubuntu.com/ubuntu focal-updates/universe Translation-en [301 kB]
Get:24 http://archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 Packages [6,748 B]
Get:25 http://archive.ubuntu.com/ubuntu focal-updates/multiverse Translation-en [3,260 B]
Get:26 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 Packages [7,424 B]
Get:27 http://archive.ubuntu.com/ubuntu focal-backports/universe Translation-en [4,348 B]
Get:28 http://security.ubuntu.com/ubuntu focal-security/main Sources [142 kB]
Get:29 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [506 kB]
Get:30 http://security.ubuntu.com/ubuntu focal-security/main Translation-en [175 kB]
Get:31 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 Packages [2,112 B]
Get:32 http://security.ubuntu.com/ubuntu focal-security/restricted Translation-en [2,260 B]
Get:33 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [685 kB]
Get:34 http://security.ubuntu.com/ubuntu focal-security/universe Translation-en [210 kB]
Get:35 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 Packages [3,744 B]
Get:36 http://security.ubuntu.com/ubuntu focal-security/multiverse Translation-en [2,668 B]
Fetched 33.5 MB in 13s (2,563 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
All packages are up to date.

Command: sudo apt upgrade output

Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following packages will be upgraded:
  package1 package2 package3
3 upgraded, 0 newly installed, 0 to remove, and 0 not upgraded.
Need to get 500 kB of archives.
After this operation, 100 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 package1 [200 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 package2 [200 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 package3 [100 kB]
Fetched 500 kB in 2s (234 kB/s)
(Reading database ... 280418 files and directories currently installed.)
Preparing to unpack .../package1.deb ...
Unpacking package1 (version) over (version) ...
Preparing to unpack .../package2.deb ...
Unpacking package2 (version) over (version) ...
Preparing to unpack .../package3.deb ...
Unpacking package3 (version) over (version) ...
Setting up package1 (version) ...
Setting up package2 (version) ...
Setting up package3 (version) ...
Processing triggers for man-db (version) ...
Processing triggers for libc-bin (version) ...

What is “sudo”

In Ubuntu, “sudo” stands for “Superuser Do.” It is a command-line utility that allows users with administrative privileges to execute commands as the superuser or root user. When using “sudo,” users can perform tasks that require elevated permissions, such as installing software, modifying system configurations, or managing files and directories that are restricted to the root user. The “sudo” command ensures better security by limiting the exposure of the root account and providing a controlled way to perform administrative actions on Ubuntu systems.

Installing MySQL

To install MySQL on Ubuntu, follow these steps:

Step 1: Install the MySQL Server Package

Open a terminal and run the following command to install the MySQL server package:

sudo apt install mysql-server

Output of command:

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  package1 package2 package3
Suggested packages:
  suggestion1 suggestion2
The following NEW packages will be installed:
  mysql-server package1 package2 package3
0 upgraded, 4 newly installed, 0 to remove, and 0 not upgraded.
Need to get 100 MB/150 MB of archives.
After this operation, 250 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 package1 [50 MB]
Get:2 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 package2 [20 MB]
Get:3 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 package3 [30 MB]
Get:4 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 mysql-server [100 kB]
Fetched 100 MB in 5s (20 MB/s)
Preconfiguring packages ...
Selecting previously unselected package package1.
(Reading database ... 280418 files and directories currently installed.)
Preparing to unpack .../package1.deb ...
Unpacking package1 (version) ...
Selecting previously unselected package package2.
Preparing to unpack .../package2.deb ...
Unpacking package2 (version) ...
Selecting previously unselected package package3.
Preparing to unpack .../package3.deb ...
Unpacking package3 (version) ...
Setting up package1 (version) ...
Setting up package2 (version) ...
Setting up package3 (version) ...
Selecting previously unselected package mysql-server.
(Reading database ... 280584 files and directories currently installed.)
Preparing to unpack .../mysql-server.deb ...
Unpacking mysql-server (version) ...
Setting up mysql-server (version) ...
Please provide a password for the MySQL root user: [Enter your desired password here]

Step 2: Start and Enable MySQL Service

After the installation is complete, start the MySQL service and enable it to start on boot:

sudo systemctl start mysql
sudo systemctl enable mysql

Output of these command:

Starting MySQL service...
MySQL service started successfully.

Enabling MySQL service to start on system boot...
MySQL service is now set to start on system boot.

Configuring MySQL

To configure MySQL, follow these steps:

Step 1: Run the MySQL Secure Installation Wizard

The secure installation wizard helps you set up a root password and improve the security of your MySQL installation. Run the following command and follow the instructions:

sudo mysql_secure_installation

Step 2: Adjust MySQL Authentication Plugin

By default, MySQL uses the auth_socket plugin for authentication. However, this may not be suitable in all cases. To switch to the mysql_native_password authentication method, open the MySQL prompt:

sudo mysql

Then, run the following commands:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password';
FLUSH PRIVILEGES;
EXIT;

Securing MySQL Installation

To enhance the security of your MySQL installation, consider implementing the following best practices:

  • Remove anonymous users
  • Disable remote root login
  • Remove the test database

Testing MySQL

To verify that MySQL is installed and functioning correctly, follow these steps:

Step 1: Access the MySQL Command Line

Open a terminal and enter the following command to access the MySQL command line:

mysql -u root -p

Output:

Enter password: [Enter your MySQL root password here]
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is XXXXXXX
Server version: XXXXXXX

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Step 2: Verify MySQL Version

Once you are in the MySQL prompt, execute the following command to check the MySQL version:

SELECT VERSION();

Managing MySQL Services

Here are some common commands to manage the MySQL service on Ubuntu:

  • Start MySQL service: sudo systemctl start mysql
  • Stop MySQL service: sudo systemctl stop mysql
  • Restart MySQL service: sudo systemctl restart mysql
  • Check MySQL service status: sudo systemctl status mysql

Conclusion

Congratulations! You have successfully installed MySQL on your Ubuntu system. You can now start leveraging the power of MySQL to manage your databases efficiently.

FAQs

1. How can I uninstall MySQL from Ubuntu?

To uninstall MySQL from Ubuntu, run the following command:

sudo apt purge mysql-server

2. Can I install MySQL on other Linux distributions?

Yes, MySQL can be installed on various Linux distributions by following similar steps tailored to the respective package manager.

3. What is the default username and password for MySQL on Ubuntu?

By default, MySQL does not have a password set for the root user. You can set a password during the MySQL secure installation process.

4. How can I create a new MySQL user?

To create a new MySQL user, log in to MySQL with the root account and execute the following command:

CREATE USER ‘username’@’localhost’ IDENTIFIED BY ‘password’;

5. Can I install MySQL on Windows or macOS?

Yes, MySQL can be installed on Windows and macOS operating systems. There are separate installers available for these platforms.

In this article, we provided a step-by-step guide on how to install MySQL on Ubuntu. By following these instructions, you can have a fully functional MySQL database server up and running on your Ubuntu system. MySQL is a powerful tool for managing databases, and with this installation, you are ready to leverage its capabilities to store and manipulate your data effectively.


Our Recommendation

Avatar of Akhand Pratap Singh

Akhand Pratap Singh

Greetings and a warm welcome to my website! I am Akhand Pratap Singh, a dedicated professional web developer and passionate blogger.

Related Post

Leave a Comment





Newsletter

Subscribe for latest updates

We don't spam.

Loading

Categories