MySQL is an open-source relational database management system. It’s one of the most popular databases in use today. This tutorial will guide you through the process of installing MySQL on an Ubuntu 22.04 server.

Step 1: Deploying a Cloud Instance on Shape.host

  • Log in to Shape.host Dashboard:
  • Navigate to the Shape.host website and log in to your account.
  • Create a New Instance:
  • Click on the “Create” button located at the top right corner of the dashboard.
  • From the dropdown menu, select “Instances”.
Shape.host Resources dashboard showing the Create menu and instance rebuild controls.
  • Select Instance Location:
  • Choose the desired location for your server. For this tutorial, we’ll select “New York, USA”.
Shape.host Create New Instance form with the available data center locations.
  • Choose a Plan:
  • Select a plan that fits your requirements. For example, you might choose a plan with 2 cores CPU, 2 GB Memory, and 50 GB SSD disk space.
  • Select an Operating System:
  • Scroll down to the “Choose an image” section and select “Ubuntu 22.04”.
Shape.host instance form with a server plan and Ubuntu image selected.
  • Configure Additional Options:
  • (Optional) You can configure additional options like User Data Configuration and IPv6 Networking.
  • Enter a hostname for your instance, e.g., “Tutorial Ubuntu”.
  • Click on the “Create instance” button to deploy the instance.
Shape.host instance form showing hostname, instance count and the Create Instance button.

Step 2: Connecting to Your Instance

  • Retrieve SSH Credentials:
  • Note the IP address of your newly created instance from the Shape.host dashboard.
  • Connect via SSH:
  • Open a terminal on your local machine.
  • Use the following command to connect to your instance:
    sh ssh root@your_instance_ip
  • Replace your_instance_ip with the actual IP address of your instance.

To complete this tutorial, you will need:

  • An Ubuntu 22.04 server with root access.

Step 3: Installing MySQL

On Ubuntu 22.04, you can install MySQL using the APT package repository. Update your package index on your server if you’ve not done so recently:

apt update
apt upgrade -y
Terminal showing APT package management commands and output.

Then install the mysql-server package:

apt install mysql-server
Terminal showing APT package management commands and output.

This will install MySQL, but won’t prompt you to set a password or make any other configuration changes.

Step 4: Configuring MySQL

For fresh installations, you’ll want to run the included security script. This script changes some of the less secure default options for things like remote root logins and sample users. Run the security script:

mysql_secure_installation
Terminal showing the mysql_secure_installation command.

This will prompt you to set up the VALIDATE PASSWORD plugin, which is used to test the strength of MySQL passwords.

Would you like to setup VALIDATE PASSWORD component?

If you answer “yes,” you’ll be asked to select a level of password validation. Note that if you choose to set up VALIDATE PASSWORD, any passwords you set for MySQL users must be at least as strong as the level you select.

Next, you’ll be prompted to set a password for the MySQL root user.

Please set the password for root here.

Enter and confirm a secure password of your choice.

From there, you can press Y and ENTER to accept the defaults for all the subsequent questions. This will remove anonymous users, disallow remote root login, remove the test database, and reload privilege tables ensuring that your changes take effect.

Step 5: (Optional) Adjusting User Authentication and Privileges

In versions of MySQL prior to 5.7, the MySQL root user is set to authenticate using the mysql_native_password plugin by default. On fresh installations of MySQL 5.7 (and later), the root user is now set to authenticate using the auth_socket plugin by default.

To log into the MySQL as root, use the following command:

mysql -u root -p
Terminal showing sudo commands or output during (Optional) Adjusting User Authentication and Privileges.

If you need to change the authentication method of the MySQL root user to mysql_native_password, run the following command:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Make sure to replace password with your secure password. Then, flush the privileges to ensure that the changes take effect:

FLUSH PRIVILEGES;

Step 6: Testing MySQL

Regardless of how you installed it, MySQL should have started running automatically. To test this, check its status:

systemctl status mysql
Terminal showing systemd service management commands or output.

You’ll see output similar to the following:

● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2022-04-26 15:01:45 UTC; 4min 15s ago

If MySQL isn’t running, you can start it with:

systemctl start mysql

To further verify that MySQL is working as expected, you can connect to the database using the mysqladmin tool, a client that lets you run administrative commands. For example, this command says to connect to MySQL as root (-u root), prompt for a password (-p), and return the version:

mysqladmin -u root -p version
Terminal showing systemd service management commands or output.

Step 7: Setting Up a MySQL Database

After MySQL is installed, you can create a new MySQL database. To do so, first log in to the MySQL root administrative account:

mysql -u root -p
Terminal showing mysql commands or output during Setting Up a MySQL Database.

Then create a new database:

CREATE DATABASE mydatabase;

Next, create a new user and grant them full privileges on the newly created database. Replace username and password with the user and password of your choice:

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON mydatabase.* TO 'username'@'localhost';
FLUSH PRIVILEGES;

Finally, exit the MySQL shell:

exit
Terminal showing SQL database and user configuration commands.

For reliable hosting solutions, consider using Shape.host services. They offer Cloud VPS, providing high performance and stability for your MySQL server.