DevOps Prerequisite (Part 6): Database
Tags: devops
Published: 2023-03-24 06:56:52

Here we will learn about 2 databases
MySQL which is a SQL based databases and MongoDB which is a NoSQL database.
**SQL**
It basically stores data in rows and column format

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lei13uboij85m6e4lo6k.png)


If you want to add any data, all of the data get assigned to a column. You can now provide values for them.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/koe825pxp84knfwpkksg.png)

Here you can see the Salary and Grade column and it's not full. But all of the data has now a column which they can fill or not.

**NoSQL**
Here it keeps data like pages.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r02ln7o7kowavuym9kb4.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rq7fn3l78zyjgfabk99n.png)
You can add extra details here which won't impact other datas. Other data won't have that special label or row at all.

This is the benefit of NoSQL.
This is a json representation of NoSQL database.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nhkt08ukyz74yrjub24y.png)

**MySQL basics**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/brvx5k1nnlwd226u0h0t.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rbla6f3ikc74l4ucy5gw.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8fq895w4q9xxhrdpddxr.png)
Use the one time password it generated to login.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4d9b3p4l7rntn4ewqiqq.png)
Now, change your default password to a new one.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1jeiweau52z0aqr8r9ec.png)

You can now check the default databases.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qfd834q0vzhq7lshgdiz.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s20qreho4dsmg0ag5p8x.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ra1pz6g63zppapt9i0vr.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pjk14jm4leefaotiz850.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5sedg4pww5wuofzrqd9o.png)
Creating user

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wtkg3i0ldxlarpa0ypkk.png)
Here after the user name, we specify the host . So that the user can only access that host .

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/usgvmx7tupbp76y1m57d.png)
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fegsvzw9pq3w6p1tutr6.png)
You can pick any of the permissions from here and assign the user.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hugtp4uca62zitb251sw.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j615ezd87r1jmx0or7kh.png)
Labs: 
Installing MySQL in Centos

```
sudo yum install https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

sudo yum install mysql-community-server
```

Start the MySQL

```
sudo service mysqld start
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bq6t1lwigti9yrbi0fy8.png)
Check the temporary password that has been set for root

```
sudo grep 'temporary password' /var/log/mysqld.log
```
Let's change it:
Here we have set the password to P@ssw0rd123 and for this, we checked the demo password and then logged in there. And then changed the password.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/plovjqm1yb5wph8jmxg6.png)
 Let's create a new user and set him a password

```
CREATE USER 'kk_user'@'localhost' IDENTIFIED BY 'S3cure#3214';
```
Here kk_user is the user and he can access the localserver only . We did set it using "localhost" . We have also set password for him and that is "S3cure#3214".

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w8vlpk5j0fr7x0zsfype.png)


Let's grant him all of the access 

```
GRANT ALL PRIVILEGES ON kk_db.* TO 'kk_user'@'localhost';
```
now we have given kk_user the full access to our database which we created (kk_db) by using * after .

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5lkhjfozrktztyf7s7uh.png)


**MongoDB**

Stores data in json like format.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u29n2rbhyuyiqyigt19e.png)
Multiple documents create a document and multiple document creates a database.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t1qoisdksulyaizifq72.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2bic544qqts61kf9zu2g.png)



We can use cloud or server version.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rpt6qtadv4qd8vdzkx2i.png)
Here we are using the server version

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/80jxa3gk47bbnqkgto8l.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zvv29alx8w1rs4naypzo.png)

Check the logs

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbocj0zer24f8eix4do5.png)


You can change the host IP from here.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ktz1ru3gskmeftnobvnl.png)
Connect to mongodb

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xjmbr6hvqgabrfmzb6wx.png)
No access control is enabled here.
Check databases

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/afavpxxkhed0hef0peq7.png)
Create a new database and switch to it

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/574hj9ruj2vis0cldy58.png)
 You can now create collections and insert your data.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zyrrcesegfc4rmayuemf.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fdyrso43oqwgjfu4sqzu.png)


[Install MongoDB on Centos](https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-centos-7)



<img src="https://media0.giphy.com/media/Nfs5y1kzJKfu/giphy.gif?cid=ecf05e472d3w835ptrri5m1p6ifnwl9mi7ope9pifvhpcxf6&rid=giphy.gif&ct=g">