DEV Community

Cover image for How to Install MySQL on Linux/UNIX and Windows
Labby for LabEx

Posted on

How to Install MySQL on Linux/UNIX and Windows

Introduction

MindMap

This article covers the following tech skills:

Skills Graph

MySQL is one of the most popular open-source relational database management systems (RDBMS). It is developed, distributed, and supported by Oracle Corporation. MySQL is used by many database-driven web applications, including Drupal, Joomla, phpBB, and WordPress. It is also used by many popular websites, including Facebook, Twitter, Flickr, and YouTube.

In this lab, we will learn how to install MySQL on Linux/UNIX and Windows. We will also learn how to start and stop the MySQL server, and how to use the MySQL client to connect to the server.

Install MySQL on Linux/UNIX

MySQL is mostly installed as part of LAMP (Linux, Apache, MySQL, PHP/Python/Perl) stack. It uses SQL (Structured Query Language) for data management.

The installation of MySQL on Ubuntu is very simple. We have installed MySQL for you. In our case, we list all you need to do when you install MySQL server independently on Ubuntu Linux. The actual operation may vary slightly depending on the version. You can just omit this chapter and start the next part, it doesn't affect anything. After the installation, we will be able to start it from the Linux terminal.

It follows the steps below:

  • Update the package index.
  • Install MySQL-Server.
  • Execute the included security script.

At this point, your Ubuntu system should be up and running. Launch the terminal from the launcher. Run the following command on the terminal to update the package index:

sudo apt-get update
Enter fullscreen mode Exit fullscreen mode

Next, run the following command to install the mysql-server:

sudo apt-get install mysql-server -y
Enter fullscreen mode Exit fullscreen mode

Wait for the installation to complete. During the installation, you will be prompted to set the password for the MySQL root user. Enter the password and confirm it. This will install MySQL on your system. Once the installation is complete, the MySQL service will start automatically. You can verify it by running the following command:

sudo service mysql start
Enter fullscreen mode Exit fullscreen mode
* Starting MySQL database server    [ OK ]
Enter fullscreen mode Exit fullscreen mode

Then, run the following command to initiate the configuration:

mysql_secure_installation
Enter fullscreen mode Exit fullscreen mode

You will then be taken through a sequence of steps for which you will be expected to type "Y" for "yes" and any other key for "no". If you need additional security measures other than password protection, then you can type "Y", otherwise, type any other key and then hit the enter key to proceed. Do this for every step until you are done when the terminal prompt is returned to you.

Congratulations, you have just set MySQL on your Ubuntu Linux!

Test MySQL

Now that you have installed MySQL on your system, it should be up and running. To confirm this, run the following command on the terminal:

sudo service mysql status
Enter fullscreen mode Exit fullscreen mode

The output should be similar to the following:

* /usr/bin/mysqladmin  Ver 8.0.34-0ubuntu0.22.04.1 for Linux on x86_64 ((Ubuntu))
Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Server version          5.5.5-10.6.12-MariaDB-0ubuntu0.22.04.1
Protocol version        10
Connection              Localhost via UNIX socket
UNIX socket             /var/run/mysqld/mysqld.sock
Uptime:                 5 min 15 sec

Threads: 2  Questions: 74  Slow queries: 0  Opens: 33  Open tables: 26  Queries per second avg: 0.234
Enter fullscreen mode Exit fullscreen mode

The "Threads" line is an indication that MySQL has been installed and it is running.

After a successful installation of MySQL, the initialization of the base tables will have been done, and the server will be up and running.

The mysqladmin binary can help us know the version of MySQL that has been installed on our system. The version can be checked by running the following command:

mysqladmin --version
Enter fullscreen mode Exit fullscreen mode

It should give the following result:

mysqladmin  Ver 8.0.34-0ubuntu0.22.04.1 for Linux on x86_64 ((Ubuntu))
Enter fullscreen mode Exit fullscreen mode

As shown in the above output, I have installed MySQL 8.0.34. If you don't get such output, then a problem must have occurred during the installation process. To solve the issue, repeat all the steps above in order to install MySQL on your system.

Access MySQL by Shell

Now that MySQL is ready on your system, you will need to access it and perform various tasks including creating databases, creating tables, inserting data etc. You can access the MySQL shell from the Linux terminal by running the following command:

mysql -uroot -p
Enter fullscreen mode Exit fullscreen mode

The "-u" option is for the username, which in our case is the root, while the "-p" option is for the password.

You will be prompted to enter the password for the user. Note that we are logging in as the root user, so provide the root password that you created during the installation of MySQL. If you did not set a password, then just press the enter key.

You will then be presented with the MySQL prompt from which you can execute your SQL commands:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 43
Server version: 5.5.5-10.6.12-MariaDB-0ubuntu0.22.04.1 Ubuntu 22.04

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql>
Enter fullscreen mode Exit fullscreen mode

Note that in the above example, we have logged in as the root user. However, you can use the same approach to log in as any other user, provided you have an active account in the DBMS.

After a successful login, a user can perform all activities for which they have been granted privileges. The root user is the top-level user will all the privileges that a user can have on the DBMS. To close the connection to the MySQL database, run the "exit" command on the MySQL prompt as follows:

mysql> exit
Enter fullscreen mode Exit fullscreen mode

This will close the connection to the database, and you will see the prompt for your Ubuntu system.

Whenever you need to clear the terminal, just press the "Ctrl + L" keys and everything will be cleared from the terminal.

Summary

Congratulations on starting the MySQL journey with us! There are many people just like you. Stop by the dicussion forum to see what others have been talking about. Next lab, we will be walking you through MySQL database management. Take some time to digest today's lesson and sit tight, because we're moving to the next milestone soon.


Want to learn more?

Join our Discord or tweet us @WeAreLabEx πŸ˜„

Top comments (0)