# Databases

### MySQL

<details>

<summary>Install</summary>

default directory on Linux `/var/lib/mysql`

```sh
wget https://...mysql.rpm

# install by yum or RPM
sudo yum install mysql-server
# or
sudo yum install https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
sudo yum install mysql-community-server

# start service
serivce mysqld start

# check the status
service mysqld status

# validate and get the temporary password
cat /var/log/mysqld.log
```

</details>

<details>

<summary>Restart</summary>

```bash
# ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
mysql.server restart
```

</details>

<details>

<summary>Login</summary>

<pre class="language-sh"><code class="lang-sh"># validate and get the temporary password
cat /var/log/mysqld.log
sudo grep 'temporary password' /var/log/mysqld.log

<strong># login to mysql server by temporary password
</strong>mysql -u [user] -p [password]
mysql -u root -p'PrYJ0.g(OStY'

# check password policy
mysql> SHOW VARIABLES LIKE 'validate_password%';
</code></pre>

</details>

<details>

<summary>User</summary>

```sql
# change the default user's password
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpass17!';

# create new user
mysql> CREATE USER 'phuong'@'localhost' IDENTIFIED BY 'newpass17!';

# if you want a user to connect to all system, do not specify the host
mysql> CREATE USER 'phuong'@'%' IDENTIFIED BY 'newpass17!';
```

</details>

<details>

<summary>Privileges</summary>

```sql
# grant permission
mysql> GRANT [permission] ON [DB.table] TO 'phuong'@'%';

# show granted permission
mysql> SHOW GRANTS FOR 'phuong'@'hostname';
```

![](https://286463586-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F4PHaS9B0QkS2WKcFwT0s%2Fuploads%2FSPuSiDoohtpqgvEIypmm%2Fimage.png?alt=media\&token=4a6ce947-1e03-4788-8a87-d0c3d2c5d1d4)

</details>

### MongoDB

#### Structure

<figure><img src="https://286463586-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F4PHaS9B0QkS2WKcFwT0s%2Fuploads%2FcHtH7wzwVyQgfok5zowE%2Fimage.png?alt=media&#x26;token=b345f95a-d42a-40b6-b31a-c8cc0208567a" alt=""><figcaption><p>MongoDB Server</p></figcaption></figure>

<details>

<summary>Install</summary>

```sh
# install by yum
yum install mongodb-org

# check log
cat /var/log/mongodb/mongod.log
```

</details>

<details>

<summary>Configuration</summary>

The configuration file located at `/etc/mongod.conf`

</details>
