# Database war
Mysql & Mongodb
Users and Remote Connection


Hey my friends, a new post about this war. I don't know which is better, but I'll try to figure it out. I hope y'all enjoy this series. Here we go!

This time I'm going to talk about how add users and connection to the databases MYSQL and Mongodb


### Mysql

First, you need to edit this file, if you need to connect from remote IP.

```
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
```

![Selection_456.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614626303214/Dh2zGPgDM.png)


![Selection_457.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614626325093/yamCvlxov.png)


Restart the database service.
```
sudo systemctl restart mysql
```

This command adds security to your installation.

```
sudo mysql_secure_installation
```
![Selection_458.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614626334609/JUL0Ywyky.png)


![Selection_459.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614626344956/3jMaQe2At.png)

![Selection_460.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614626351694/Je9YpJ5Vt.png)

![Selection_461.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614626379708/NW29KG50y.png)

![Selection_462.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614626389657/67g5yPc8B.png)


![Selection_463.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614626398932/TsontTbpR.png)

![Selection_464.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614626406730/g1fnXTPOP.png)

This command is to connect to mysql server.

```
sudo mysql
```

![Selection_468.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614626598684/6GmsLzuDT.png)


Into mysql environment you can list all users.
```
SELECT user,authentication_string,host FROM mysql.user;

```

You should change the way to connect to mysql server.

```
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Sunny%01';

```

You must run this command when the user existed.

```
FLUSH PRIVILEGES;
```

![Selection_470.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614626681483/vMAVppYAr.png)

The best option is to create a particular user to remote access. This character '%' you can change for your specify IP.

```
CREATE USER 'luffy'@'%' IDENTIFIED BY 'Sunny%01';
```
![Selection_471.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614626727307/qnDRMw0hP.png)

Here you add all permissions to the database.

```
GRANT ALL PRIVILEGES ON *.* TO 'luffy'@'%' WITH GRANT OPTION;
```

![Selection_472.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614626751279/aj6ob_9Se.png)

With this command you can access from remote IP.
```
 mysql -h 192.168.0.7 -u luffy -p
```

### MongoDB

This command is to connect to Mongodb server.
```
mongo
```

![Selection_465.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614626457543/huB5CgPTL.png)

This command selects the collection "admin"
```
use admin
```

To create a particular user. This role is to be a superuser admin to the database.
```
db.createUser({
  user:"luffy",
  pwd:"Sunny%01",
  roles:[{role:"root"}]
 })
```

![Selection_473.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614626953568/20fLYmn0H.png)

You need to edit this file, if you need to connect from remote IP.
```
sudo nano /etc/mongod.conf
```

![Selection_466.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614626472289/2ytLef1P7.png)

![Selection_467.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614626481943/um-Dkya9I.png)

Restart the database service.
```
sudo systemctl restart mongod
```

With this command you can access from remote IP.
```
mongo --host "192.168.0.7:27017" -u "luffy" -p "Sunny%01"
```

Conclusion

- Commands: Mysql has a language called SQL and Mongodb use JSON structure to get information.

Would you like to learn about queries in databases?

I hope you enjoy my post and remember that I am just a Dev like you!



