DEV Community

Ivy Jeptoo
Ivy Jeptoo

Posted on

How to create a Linux VM and Install MySQL Server using Cloudshell

Data management is critical to every organization, and MySQL database has proven to be a reliable tool for storing and retrieving information. Pairing MySQL with Azure VMs provides a robust, flexible environment for running your workloads. In this article, we'll explore the process of setting up VMs and using MySQL on Azure VMs.

The features provided by Azure Virtual Machine include:-

  • Create, start, stop, restart or terminate Virtual Machine instances.

  • Implementation of Load Balancing and Auto Scaling for multiple Virtual Machine.

  • Provides additional storage to Virtual Machine instances

  • Manages Network Connectivity to Virtual Machine.

Why MySQL? - It is a relational database that stores and manages data. It is known for its speed, reliability and ease of use.
MySQL is a good fit for running on Azure Virtual Machine because:

  1. Flexibility: it allows you to customize the VM to your specific requirements(choosing CPU, memory config, storage size).

  2. Compatibility: it is compatible with a range of programming languages hence ease of integration in the environment.

  3. Scalability: it can handle large data volumes thus easy to take advantage of automatic scaling, load balancing in Azure.

  4. Security: it have great security features like SSL encryption, user authentication and access control running it on Azure adds Azure Security features advantages.

TABLE OF CONTENT

Create Virtual Machine

  • Search for Virtual Machine on the search bar and click on it, you should also see it under Azure Services.
    vm service

  • Once you click on it, You will see a create button that has a drop down, select on Azure Virtual Machine.
    create

  • On the Basics, Provide a resource group in which you VM will belong to, you can create one as well.

  • Give your VM a unique name.

  • Select a region you'd want to deploy you VM

  • We'll go with the standard security

  • Choose an Operating System you'd want under image
    config1

  • The size is how big we want our VM to be, be sure to select your desired size.

  • Leave the default authentication type (SSH Public Key)

  • You can use the default username or provide a new one.

  • Under SSH public key source we are going to generate a new key pair.

  • Be sure to provide a Key Pair Name

  • Once all is set click on Review and Create and when the validation is passed click on Create.

config2

  • There will be a Generate new key pair pop up where you will download the key pair that we will use later on in connecting to the VM.

Key Pair

  • Once the deployment is complete click on Go to Resource so as to see the newly created VM.

deploy

Connect to Virtual Machine

  • We are going to connect to the VM using Azure Cloud Shell which is used for managing Azure resources from anywhere with an inernet connection

  • On the top of VM page, click on the connect button.

  • At the top right click on the first icon to open the cloudshell.
    Connect

  • Once you click connect, a new page will be opened that has the SSH guide to connect to the VM.
    SSH

  • Upon clicking the cloud shell icon, at the bottom of the page you'll see a section that requires a storage mounting. Click on the create button which will open the shell terminal successfully.
    mount storage

  • We need to upload our private key(we downloaded earlier) to the cloud shell.
    Click the upload icon then upload the file.

  • After it has uploaded you will see the notification at the bottom right of the page.
    upload

  • To confirm the file has been uploaded run ls and you will be sure to see your file name.

  • To ensure we have read-only access to private key run chmod 400 <keyname>.pem.
    Replace .pem with your actual file name.
    Confirm you have read-only access running ls -ltrh

  • Provide a path to the private key. Run pwd to get path of the SSH private key then run ssh -i <private key path>/<filename>.pem azureuser@52.191.61.164
    Replace private key path with the path and also the private key file name.

  • Confirm that you want to continue with the connection by clicking yes and you will soon be connected!

  • You can confirm you are connected to the VM by running hostname.

Install MySQL

  • sudo apt-get update
  • sudo apt-get install mysql-server
  • mysql -V to confirm installation

Connect to MySQL

We are going to connect to the root user account since we have not created any user account

  • sudo mysql too start MySQL client with admin privileges.

Create a new User

  • CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';

    Replace newuser with desired username for the new user and password with a unique password.
    @ localhost specifies that the user account can only be used to connect to MySQL server from the local machine.(it is a recommended approach in azure).

  • Grant the new user necessary privileges to the databases and tables on the server GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';

  • You can now connect to MySQL server using the new user account created by running: mysql -u newuser -p -h hostname .
    Replace the username with the username of the MySQL user account created earlier, hostname with the hostname/IP address of the machine running MySQL server
    It will prompt you to enter the password for the new user account.

  • Once connected you can create a new database CREATE DATABASE dbname;
    Replace dbname with your new database name.

N/B
Remember to stop your Virtual Machine from running to avoid incurring extra costs!

Congratulations!! You have just create your own Virtual Machine and connect MySQL server to it!

Top comments (0)