How to create a Linux VM and Install MySQL Server using Cloudshell
Tags: mysql, azure, virtualmachine
Published: 2023-04-27 16:27:51

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](#create-virtual-machine)
- [Connect to Virtual Machine](#connect-to-virtual-machine)
- [Install MySQL](#install-mysql)
- [Connect to MySQL](#connect-to-mysql)

##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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5qqbb6gtdolj2sbcfaml.png)

- Once you click on it, You will see a create button that has a drop down, select on Azure Virtual Machine.
![create](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lqmldwykrfqb9n3ayfv8.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9lb58cbykr8qydtce4os.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4agtrggpks122e50be9n.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i2kve1smnhjmdkzvnvlz.png)

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

![deploy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d0wuchiwup85ejn1ezv4.png)




##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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wylg01w352madnj859bm.png)

- Once you click connect, a new page will be opened that has the SSH guide to connect to the VM.
![SSH](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ar4jp719qnqx2m8ej1aq.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q16i877600v4wq1myp5e.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/29e9jtzqpyzlamu2eh7p.png)

- 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 _<keyname>.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!