X

How to Install MySQL on Ubuntu 24.04

Spread the love

In this blog post, we will explain how to install MySQL on Ubuntu 24.04. MySQL is an open-source relational database management system written in C and C++ developed and maintained by the Oracle Corporation. MySQL offers a variety of features, such as speed, security, and replication, and it is one of the most popular databases. The data types provided by MySQL are int, tinyint, long, char, float, double, datetime, etc. In this tutorial, we will show you how to install MySQL, manage the MySQL services, and some simple operations about creating databases, users, etc.

This process will take up to 15 minutes. Let’s get started!

Prerequisites

  • A server running Ubuntu 24.04 OS
  • User privileges: root or non-root user with sudo privileges

Update the System

We assume that you have a fresh installation of Ubuntu 24.04, so it is recommended that the packages be updated to their latest version before we take any actions on the server.

sudo apt update -y && sudo apt upgrade -y

Install MySQL server

Once, the system is up to date, we can proceed with the MySQL installation. To install MySQL on your server execute the following command:

sudo apt install mysql-server

After this command, you should allow some time for the installation to complete.

Manage the MySQL service

Once the installation is completed we can proceed with managing the MySQL service.

To start and enable the service:

sudo systemctl start mysql && sudo systemctl enable mysql

You should receive the following output:

root@host:# sudo systemctl start mysql && sudo systemctl enable mysql
Synchronizing state of mysql.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable mysql

To check the status of the service execute the command below:

sudo systemctl status mysql

You should get the following output:

root@host:~# sudo systemctl status mysql
● mysql.service - MySQL Community Server
     Loaded: loaded (/usr/lib/systemd/system/mysql.service; enabled; preset: enabled)
     Active: active (running) since Sat 2024-06-08 16:39:34 CDT; 2min 40s ago
   Main PID: 43182 (mysqld)
     Status: "Server is operational"
      Tasks: 37 (limit: 4613)
     Memory: 365.3M (peak: 379.7M)
        CPU: 2.968s
     CGroup: /system.slice/mysql.service
             └─43182 /usr/sbin/mysqld

Jun 08 16:39:32 host.test.vps systemd[1]: Starting mysql.service - MySQL Community Server...
Jun 08 16:39:34 host.test.vps systemd[1]: Started mysql.service - MySQL Community Server.

To restart the service you can use the following command:

sudo systemctl restart mysql

To stop the MySQL service:

sudo systemctl stop mysql

Create MySQL Database and User

Since we installed the MySQL database we will provide you with some simple tasks about creating a database and user in MySQL and assigning permissions between them. First log into the MySQL console with the following command:

mysql

You should see the following screen:

root@host:~# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.36-2ubuntu3 (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>

To create a database execute the following command:

create database testdb;

After successful creation, you will get the following output:

mysql> create database testdb;
Query OK, 1 row affected (0.01 sec)

Now, let’s create a user:

create user testinguser@localhost IDENTIFIED BY 'StrongPasswordHere';

You will get the following output:

mysql> create user testuser;
Query OK, 0 rows affected (0.03 sec)

We have the database and the user. With the next command, we will grant access to the user on the database.

grant all ON testdb.* TO 'testinguser'@'localhost';
flush privileges;
 

There are many MySQL commands such as dumping and importing database, repairing tables, and optimizing MySQL configuration which will not be explained in this tutorial since it was only for the installation and the most common commands for creating database and user. Of course, you do not have to do this installation alone. You only need to sign up for our monthly server management or per-incident server support and submit a support ticket. Our admins will help you with any aspect of the MySQL installation and configuration on your server.

If you liked this post on how to install MySQL on Ubuntu 24.04, please share it with your friends and leave a comment below. Thanks.

Categories: Ubuntu
admin:
Related Post