How to uninstall MySQL

Installing MySQL 8.0 On Ubuntu 24.04 : Step by step guide

Learn how to install MySQL on Ubuntu 24.04 LTS Linux operating in this detailed guide. Get MySQL up and running on your Linux system with easy-to-follow instructions.

Introducion

MySQL is an open-source relational database management system (RDBMS) developed by Oracle. It is based on SQL (Structured Query Language), which allows users to perform database operations like querying, updating, and managing data. It is commonly used in web applications, such as content management systems (CMS) like WordPress, e-commerce platforms, and even social networks. MySQL is the world’s most popular open-source database management system. On this article we will learn how to install MySQL on Ubuntu 24.04 LTS operating system.

How To Install MySQL On Ubuntu 24.04

Whether you’re running a website, an application, or just learning how to use MySQL, this tutorial will walk you through the necessary steps. In this short article we will learn how to install MySQL from the basic. The installation steps on this short tutorial will be consist of several section, namely :

  1. Update the Ubuntu Package Index
  2. Install MySQL Server
  3. Secure MySQL Installation
  4. Start and Enable MySQL Service
  5. Log into MySQL and Create Sample Database

Prerequisites

Before installing MySQL on your Ubuntu system, ensure you have:

  • A user with sudo privileges: You’ll need administrative rights to install and configure MySQL.
  • An updated Ubuntu system: Make sure your Ubuntu system is up-to-date.

Step 1: Update the Ubuntu Package Index

First, ensure that your Ubuntu repositories are up-to-date. This is a crucial step before installing any new software to avoid conflicts or outdated packages. For this purpose we will submit the following command line :

$ sudo apt update

Step 2: Install MySQL Server

Once the package index is updated, you are ready to install MySQL. Use the following command to install the MySQL server package on Ubuntu.

$ sudo apt install mysql-server -y

This command will install MySQL along with any required dependencies. During the installation, you may be prompted to confirm the installation by typing Y and hitting Enter.The output will be as shown below :

ramansah@dev02:~$ sudo apt install mysql-server -y
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
libaio1t64 libcgi-fast-perl libcgi-pm-perl libevent-core-2.1-7t64
libevent-pthreads-2.1-7t64 libfcgi-bin libfcgi-perl libfcgi0t64 libhtml-template-perl
libmecab2 libprotobuf-lite32t64 mecab-ipadic mecab-ipadic-utf8 mecab-utils
mysql-client-8.0 mysql-client-core-8.0 mysql-common mysql-server-8.0
mysql-server-core-8.0
Suggested packages:
libipc-sharedcache-perl mailx tinyca
The following NEW packages will be installed:
libaio1t64 libcgi-fast-perl libcgi-pm-perl libevent-core-2.1-7t64
libevent-pthreads-2.1-7t64 libfcgi-bin libfcgi-perl libfcgi0t64 libhtml-template-perl
libmecab2 libprotobuf-lite32t64 mecab-ipadic mecab-ipadic-utf8 mecab-utils
mysql-client-8.0 mysql-client-core-8.0 mysql-common mysql-server mysql-server-8.0
mysql-server-core-8.0
0 upgraded, 20 newly installed, 0 to remove and 183 not upgraded.
Need to get 29.3 MB of archives.
After this operation, 242 MB of additional disk space will be used.

. . .

update-alternatives: using /var/lib/mecab/dic/ipadic-utf8 to provide /var/lib/mecab/dic/debian (mecab-dictionary) in auto mode
Setting up mysql-server-8.0 (8.0.39-0ubuntu0.24.04.2) ...
update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Renaming removed key_buffer and myisam-recover options (if present)
mysqld will log errors to /var/log/mysql/error.log
mysqld is running as pid 4127
Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service → /usr/lib/systemd/system/mysql.service.
Setting up mysql-server (8.0.39-0ubuntu0.24.04.2) ...
Processing triggers for man-db (2.12.0-4build2) ...
Processing triggers for libc-bin (2.39-0ubuntu8.3) ...

Then we will inspect the MySQL version, by submitting command line :

$ mysql –version

The output will be shown below :

ramansah@dev02:~$ mysql --version
mysql Ver 8.0.39-0ubuntu0.24.04.2 for Linux on x86_64 ((Ubuntu))

Step 3: Secure MySQL Installation

After the installation is complete, it’s essential to run the mysql_secure_installation script to enhance the security of your MySQL instance.  This interactive script will guide you through a series of steps:

  • Set the root password: You will be asked to set a root password for MySQL.
  • Remove anonymous users: This improves security by restricting access to only authenticated users.
  • Disable root login remotely: This blocks the root user from logging in from remote systems.
  • Remove test database: Test databases are often insecure and should be removed.
  • Reload privilege tables: This ensures that the changes take effect immediately.

We will submit following command line : sudo mysql_secure_installation, the output will be as shown below.

ramansah@dev02:~$ sudo mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.

Skipping password set for root as authentication with auth_socket is used by default.
If you would like to use password authentication instead, this can be done with the "ALTER_USER" command.
See https://dev.mysql.com/doc/refman/8.0/en/alter-user.html#alter-user-password-management for more information.

By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Dropping test database...
Success.

- Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

Step 4: Start and Enable MySQL Service

Now that MySQL is installed and secured, you can start and enable the MySQL service to ensure it runs every time the system boots. We will enable, start and show status of MySQL server.

$ sudo systemctl enable mysql
$ sudo systemctl start mysql
$ sudo systemctl status mysql

managing MySql services

Step 5: Log into MySQL and Create Sample Database

After the service is running, you can log into the MySQL shell using the following command:

$ sudo mysql -u root -p

The output will be as shown below :

ramansah@dev02:~$ sudo mysql -u root -p
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.39-0ubuntu0.24.04.2 (Ubuntu)

Copyright (c) 2000, 2024, 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>

Log in to MySQL

This will log you into the MySQL shell as the root user. From here, you can begin creating databases, adding users, or configuring MySQL further based on your project needs.

Step 6 : Creating Database On MySQL

In this section, we will learn how to create a new database on MySQL server. We have passed through several steps until we have fresh MySQL server engine on Ubuntu 24.04 LTS operating system. To continue our journey in learning MySQL, we will create a sample database and all objects inside it. We will have several tasks for this, namely :

  1. Create database
  2. Create database user with strong password
  3. Grant privilege to a new user created for new databasecreated.
  4. Refresh all privilege
  5. Create table and insert new data

6.1 Create Database

mysql> CREATE DATABASE ramansahdb;
Query OK, 1 row affected (0.01 sec)

Show database :

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| performance_schema |
| ramansahdb         |
+--------------------+
3 rows in set (0.01 sec)

6.2 Create database user

mysql> CREATE USER 'my_user'@'localhost' IDENTIFIED BY 'MdkOI3$djiu';
Query OK, 0 rows affected (0.01 sec)

6.3 Grant privilege

mysql> GRANT ALL PRIVILEGES ON ramansahdb.* TO 'my_user'@'localhost';
Query OK, 0 rows affected (0.02 sec)

6.4 Refresh all privilege

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)
mysql> exit
Bye

6.5 Create a New table

In our scenario, we will create a new table called as cust_test on ramansahdb database.

mysql> use ramansahdb;
Database changed
mysql> CREATE TABLE cust_test (
-> id INT AUTO_INCREMENT PRIMARY KEY,
-> name VARCHAR(255) NOT NULL,
-> phonenumber VARCHAR(14),
-> created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-> );
Query OK, 0 rows affected (0.04 sec)

then insert data to a new table created :

mysql> INSERT INTO cust_test (name, phonenumber) VALUES 
-> ('Jhon Due', '081111111111'),
-> ('Ivan Sie', '081111111112'),
-> ('Irvine Joe','081111111113');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0

then select data which was inserted :

mysql> select * from cust_test;
+----+------------+--------------+---------------------+
| id | name       | phonenumber  | created_at          |
+----+------------+--------------+---------------------+
| 1  | Jhon Due   | 081111111111 | 2024-09-11 17:07:38 |
| 2  | Ivan Sie   | 081111111112 | 2024-09-11 17:07:38 |
| 3  | Irvine Joe | 081111111113 | 2024-09-11 17:07:38 |
+----+------------+--------------+---------------------+
3 rows in set (0.01 sec)

Conclusion

Congratulations! You have successfully installed MySQL on Ubuntu. Whether you’re setting up a development environment or preparing a production server, MySQL offers the scalability and performance needed for managing your databases efficiently. Remember to keep your MySQL server secure by following best practices, such as disabling remote root access and regularly updating your software.

(Visited 37 times, 1 visits today)

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *