# User and Database Management

Command syntax will vary between MySQL Versions

====================================================================================

### User Management

##### Show all users and hosts

```
SELECT user, host FROM mysql. user;
```

##### Add User

```
CREATE USER 'name'@'localhost' IDENTIFIED BY 'password.';
```

##### Granting Privileges

Available privileges:

<table border="1" id="bkmrk-create-allows-the-us" style="border-collapse: collapse; width: 100%; height: 268.172px;"><colgroup><col style="width: 50%;"></col><col style="width: 50%;"></col></colgroup><tbody><tr style="height: 29.7969px;"><td style="height: 29.7969px;">CREATE</td><td style="height: 29.7969px;">Allows the user to create new databases and tables.</td></tr><tr style="height: 29.7969px;"><td style="height: 29.7969px;">ALTER</td><td style="height: 29.7969px;">Allows the user to alter (modify) existing tables.</td></tr><tr style="height: 29.7969px;"><td style="height: 29.7969px;">DROP</td><td style="height: 29.7969px;">Allows the user to drop (delete) databases and tables.</td></tr><tr style="height: 29.7969px;"><td style="height: 29.7969px;">INSERT</td><td style="height: 29.7969px;">Allows the user to insert new rows into tables.</td></tr><tr style="height: 29.7969px;"><td style="height: 29.7969px;">UPDATE</td><td style="height: 29.7969px;">Allows the user to update existing rows in tables.</td></tr><tr style="height: 29.7969px;"><td style="height: 29.7969px;">DELETE</td><td style="height: 29.7969px;">Allows the user to delete rows from tables.</td></tr><tr style="height: 29.7969px;"><td style="height: 29.7969px;">SELECT</td><td style="height: 29.7969px;">Allows the user to read (select) data from tables.</td></tr><tr style="height: 29.7969px;"><td style="height: 29.7969px;">REFERENCES</td><td style="height: 29.7969px;">Allows the user to create foreign key constraints when defining tables.</td></tr><tr style="height: 29.7969px;"><td style="height: 29.7969px;">RELOAD</td><td style="height: 29.7969px;">Allows the user to execute the `FLUSH` statement, which reloads various server configurations and clears or refreshes caches.</td></tr></tbody></table>

Grant specific privilege ( or multiple comma separated):

```
GRANT PRIVILEGETYPE ON database.table TO 'username'@'host';
FLUSH PRIVILEGES;
```

Grant all:

```
GRANT ALL ON database.table TO 'username'@'host';
FLUSH PRIVILEGES;
```

##### Delete a user

```
DROP USER 'name'@'host';
```

<span style="color: rgb(187, 187, 187); font-family: var(--font-heading, var(--font-body)); font-size: 1.4em; font-weight: 400;">Change user password</span>

```
ALTER USER 'user'@'host' IDENTIFIED BY 'NewPassword';
FLUSH PRIVILEGES;
```

====================================================================================

### Database Management

##### Create a database

```
CREATE DATABASE name;
```

\------------------------------------------------------------------------------------------------------------------------------------------------

##### Delete a database

```
DROP DATABASE name;
```

====================================================================================