Creating and granting permission to MySQL users

David Dalton
7 Mar 202309:15

Summary

TLDRThis video tutorial covers how to manage database user access by creating roles and assigning permissions. The process begins by setting up a user role, granting specific privileges such as select, insert, update, and delete on the 'world' database. The tutorial demonstrates how to create a user, assign a password, and link them to the role. It also shows how to test the user's access, verify permissions, and revoke or grant additional privileges as needed, emphasizing secure and controlled access to the database.

Takeaways

  • 👤 Creating users and granting permissions is essential when managing databases beyond using an administrator or root user.
  • 🛠 Permissions should generally be assigned to roles (similar to groups in Windows Server) rather than individual users.
  • 🌍 The script demonstrates creating a user and granting them specific permissions on the 'world' database.
  • ⚙️ A role called 'world_user' is created using 'CREATE ROLE world_user' to handle permissions for the world database.
  • 📜 Permissions such as SELECT, INSERT, UPDATE, and DELETE are granted to the 'world_user' role using the 'GRANT' command.
  • 🧑‍💻 The 'CREATE USER' command is used to create a new user, assign them a password, and set their default role as 'world_user'.
  • 🔐 Password options, such as requiring the user to change the password on first login, can be set during user creation.
  • 🔍 The 'SHOW GRANTS' command allows a user to see the specific permissions assigned to them.
  • 🚫 Users with limited roles can be restricted from performing certain actions, such as creating or dropping tables in the database.
  • 🔄 Permissions can be updated or revoked at any time using the 'GRANT' and 'REVOKE' commands, making database security flexible.

Q & A

  • Why is it important to create users and assign permissions rather than using the root user for database access?

    -Using the root or administrator account for all database access is risky because it grants unrestricted access to everything. Creating specific users with defined roles and permissions allows better security and control, limiting access to only what is necessary for each user.

  • What is the first step in creating a user with specific permissions for a database?

    -The first step is to create a role, which groups permissions together. In this case, the script creates a role called 'World_user' that will later be assigned permissions for accessing the 'world' database.

  • How are permissions granted to a role in the script?

    -Permissions are granted to a role using the `GRANT` command, followed by specifying the privileges (e.g., SELECT, INSERT, UPDATE, DELETE) and the scope (in this case, all tables in the 'world' database).

  • What is the purpose of the `SHOW PRIVILEGES` command in this context?

    -The `SHOW PRIVILEGES` command lists all the available permissions that can be granted to a role or user. It helps the administrator understand the types of permissions they can assign, such as SELECT, CREATE, and DROP.

  • How can you grant permissions to specific tables rather than an entire database?

    -To grant permissions to specific tables, you can specify the table names after the database name. For example, you could write `GRANT SELECT ON world.country TO World_user` to give access only to the 'country' table in the 'world' database.

  • What is the difference between the `GRANT` and `REVOKE` commands?

    -The `GRANT` command gives permissions to a role or user, while the `REVOKE` command removes permissions that have been previously granted.

  • How is a new user created and assigned to a role?

    -A new user is created using the `CREATE USER` command, where the username and password are specified. After that, the `DEFAULT ROLE` is set for the user to assign them to a specific role, such as 'World_user'.

  • What does setting a 'default role' for a user accomplish?

    -Setting a default role for a user automatically assigns the user to that role upon creation, ensuring that the user inherits the permissions of the role without needing to manually add them later.

  • How can you verify the roles and permissions assigned to a user?

    -You can verify the roles assigned to a user by using the `SELECT CURRENT_ROLE()` command to see the user's active role. Additionally, the `SHOW GRANTS` command will display all the privileges that the user has been granted.

  • What happens if a user attempts to access a table or perform an action they do not have permission for?

    -If a user tries to access a table or perform an action they do not have permission for, the system will deny access. For example, attempting to create a table without the `CREATE` privilege will result in a permission error.

Outlines

00:00

👨‍💼 Creating Users and Assigning Roles

This section discusses creating users and assigning permissions to them. The author explains how they have been using the database as the root user, but as the system grows, other users need to access the database. Instead of assigning permissions to individual users, roles are created, similar to groups in Windows Server. The example walks through creating a 'world_user' role and assigning permissions such as SELECT, INSERT, UPDATE, and DELETE on the 'world' database. The author introduces the `CREATE ROLE` and `GRANT` commands, explaining how permissions can be applied to specific tables if necessary.

05:02

🔑 Exploring Database Privileges

The author highlights various database privileges, using the `SHOW PRIVILEGES` command to display a list. They explain privileges like ALTER, DROP, CREATE, and SELECT and where they apply (e.g., to tables, views, or entire databases). This step is crucial for determining which privileges should be assigned to roles. The process of granting these privileges to the 'world_user' role is demonstrated, followed by an explanation of how privileges can be revoked using the `REVOKE` command.

🧑‍💻 Creating Users and Setting Default Roles

This part covers creating a new user and assigning them to the previously created 'world_user' role. The author uses the `CREATE USER` command and specifies a password for the user. Additionally, they mention setting options like password expiration and default roles. The user creation process is completed with an example, followed by a demonstration of how to review user roles in the administration section of the database interface.

🛠️ Connecting as a New User and Verifying Permissions

After creating the user, the author shows how to connect to the database using the new user's credentials. They verify the connection and the user's current role using the `SELECT CURRENT_ROLE` and `SHOW GRANTS` commands, confirming the correct permissions have been assigned. The user is then able to perform operations like selecting data from the 'country' table in the 'world' database, demonstrating their ability to interact with the assigned tables.

🚫 Restricted Access to Other Databases

Here, the author verifies that the new user does not have access to other databases or certain operations, like creating or dropping tables. They attempt to access the 'Sakia' database and create a table, but receive 'access denied' errors. This confirms that the role-based permissions have been correctly enforced, allowing the user only limited access to the 'world' database.

🛡️ Managing Database Security with Roles and Permissions

In the conclusion, the author summarizes how to secure databases by creating roles, granting permissions, and assigning users to those roles. They reiterate the flexibility of the `GRANT` and `REVOKE` commands for managing permissions dynamically. This provides a comprehensive approach to securing databases by controlling what users can and cannot do through role-based access control.

Mindmap

Keywords

💡User

A 'user' refers to an individual who accesses the database. In the script, the user is someone who interacts with the database but doesn't have the same level of permissions as an administrator. The speaker explains the process of creating a user and assigning specific roles and permissions to control their access.

💡Role

A 'role' is a collection of permissions that can be assigned to users. Instead of assigning privileges to each user individually, roles allow for easier management. The script explains how a role, such as 'World_user,' is created and granted permissions, which can then be assigned to users.

💡Grant

'Grant' is the SQL command used to assign specific privileges to a role or user. It allows control over what actions a user or role can perform within the database. For example, the speaker grants the 'select,' 'insert,' 'update,' and 'delete' privileges on the 'world' database to the 'World_user' role.

💡Privileges

Privileges define what actions users or roles are allowed to perform in the database. They include actions like 'select,' 'insert,' 'update,' and 'delete.' The speaker demonstrates how to view available privileges using the 'show privileges' command and how to grant specific privileges to a role.

💡Database

A 'database' is a structured collection of data. In the script, the speaker focuses on the 'world' database, explaining how to manage user access to it. Databases store data in tables, and different privileges can be assigned to control what users can do with this data.

💡Permissions

'Permissions' refer to the access rights given to users or roles, controlling what they can do within the database. Permissions are granted through SQL commands and can be revoked if necessary. The script covers how permissions like 'select' and 'insert' are assigned to the 'World_user' role.

💡Revoke

'Revoke' is the SQL command used to remove previously granted privileges from a user or role. The script mentions how the 'revoke' command can be used to withdraw access rights, ensuring tighter control over database security.

💡Schema

A 'schema' is the organizational structure of a database, which includes tables, views, and other database objects. In the video, the speaker refers to schemas when showing the privileges assigned to users and roles within the 'world' database.

💡Password

A 'password' is a security measure used to authenticate a user before they can access the database. The speaker explains how to create a user with an initial password and the option to force the user to change it upon first login, adding an extra layer of security.

💡Tables

In a database, 'tables' are the structures that store data in rows and columns. The speaker explains how users are granted permissions to specific tables or entire databases. For example, they grant permissions on all tables in the 'world' database to the 'World_user' role.

Highlights

Creating users and granting permissions using roles instead of individual user permissions.

The process starts by creating a role with the necessary permissions for a specific database.

Viewing the available privileges to determine which can be granted to a role.

Granting specific permissions such as SELECT, INSERT, UPDATE, and DELETE on a database or specific tables within the database.

Demonstrating how to assign permissions to all tables in a database using database_name.* syntax.

Creating a new user and setting a password with an option to expire the password immediately.

Assigning the created role as the default role for the new user.

Using SHOW GRANTS to view the permissions assigned to a specific user or role.

Demonstrating how to test the user's connection to verify access and permissions to the database.

Explaining how users can view their current role using SELECT current_role().

Restricting access to other databases outside the scope of the assigned role permissions.

Blocking actions such as creating or dropping tables, ensuring the user only has specific privileges as granted.

Illustrating how permissions can be revoked using the REVOKE command, similar to how they are granted.

Highlighting the flexibility to modify roles by adding or removing permissions as needed.

The importance of using roles for managing permissions in a secure and organized manner, preventing over-permissioning of users.

Transcripts

play00:01

okay let's talk about creating uh users

play00:05

and then granting permissions to users

play00:08

because up till this point we've pretty

play00:10

much just been using this as

play00:11

administrator or as the root user which

play00:14

is great because you've got all power

play00:16

and it works wonderful for you to do it

play00:17

but sooner or later you're going to want

play00:19

to give users access to your database as

play00:21

well now typically we won't Grant

play00:24

permissions to individual users just

play00:26

like with anything else right so in

play00:29

Windows Server we don't grab give

play00:31

permissions individual users we give

play00:33

them to groups well it's the same thing

play00:34

here except instead of groups we have

play00:37

roles so here's what I want to do I'll

play00:39

let you know where we're going and then

play00:40

we'll work on getting there I want to

play00:42

create a user and I want to give them

play00:46

permission to the world database not any

play00:49

of my other databases that are over here

play00:50

just to my world database and I want

play00:53

them to be able to select data insert

play00:55

data update data and delete records so

play00:59

here's what I'm going to do I'm going to

play01:00

start by creating a role so it's create

play01:04

role World underscore user

play01:08

and execute and that creates our roles

play01:12

so now that we have the role we can

play01:14

grant permissions to it so

play01:16

we have a bunch of uh privileges we can

play01:19

actually uh view so or that we can

play01:22

actually use so I'm just going to do a

play01:24

show privileges

play01:25

just so you can see what all they are

play01:28

and so here we have permissions the

play01:31

privilege the context and then the

play01:33

comments so alter applies to tables it

play01:36

allows us to alter a table uh drop can

play01:39

apply to databases or tables and allows

play01:42

us to drive databases tables and Views

play01:44

uh create we can give the permissions to

play01:48

create views to create databases tables

play01:51

or indexes we can give permissions to

play01:54

select and so you'll see a bunch of them

play01:56

here and then as we scroll down you're

play01:58

also going to see a bunch of defined

play02:02

server admin privileges as well

play02:06

so that's where you can look and see

play02:08

which privileges you can actually assign

play02:11

so now that we've seen that I want to

play02:13

assign certain permissions and I'm going

play02:15

to do this using the grant so it's Grant

play02:18

and then you give the Privileges you

play02:20

want

play02:21

in this case I want select I want I

play02:25

don't want alter I do want update I want

play02:28

insert and I want delete

play02:31

now I need to Define where these

play02:34

privileges are going to go so I'm going

play02:37

to Grant these permissions on and then

play02:41

I'm going to tell it what I want so I

play02:43

want this on world dot asterisk so

play02:47

basically every table in the world

play02:48

database now if I didn't want I could

play02:51

specify

play02:53

world.country and then I could do it on

play02:55

help if I spoke country correctly

play02:58

world.country language and then not give

play03:00

them permissions on cities or whatever I

play03:02

want to do right so in this case I'm

play03:04

doing it for the entire world database

play03:06

but that's how you would specify

play03:07

specific trade tables and I want to give

play03:11

these to the world underscore user role

play03:16

so I'm going to execute that and that

play03:19

gives me my grant now

play03:21

by the way to get rid of uh privileges

play03:24

it's revoke so Grant gives privileges

play03:27

revoke uh gets rid of privileges

play03:31

so I need to create a user and assign

play03:33

them to that role so here we have create

play03:36

user and then I'm going to get the

play03:37

username and I'm just going to use mine

play03:39

if I can spell my own name correctly

play03:42

create user and then I'm going to

play03:45

specify a password so it's identified

play03:48

and this is a little bit weird

play03:49

identified by and then I'm going to put

play03:51

in the password

play03:53

and then you can also set an option for

play03:55

password expire if you want the password

play03:58

to start out immediately expired so

play04:00

we'll do this with a one-time password

play04:02

right and then they'll log in and then

play04:04

the first thing they'll have to do is

play04:06

change their password

play04:07

identified by give them the specific

play04:10

password and then I'm going to set the

play04:12

default role is going to be

play04:16

world

play04:18

user

play04:20

probably helpful to space there too so

play04:22

I'm creating the user I'm giving them

play04:24

username identified by and I'm going to

play04:27

identifying the password and then

play04:29

setting a default role and so I execute

play04:33

that

play04:34

okay now I've created the role created

play04:37

the user and I do want to show you

play04:38

something else real quick while we're

play04:40

here we've been sitting on the schemas

play04:42

let's come over to Administration and

play04:45

look at users and computers and here

play04:47

you're going to see the world user role

play04:50

that I created

play04:51

so we have

play04:55

um

play04:56

World user here are our administrative

play04:59

roles which their iron on here are the

play05:02

schema Privileges and you'll see on the

play05:04

world they have these Privileges and

play05:06

then for the user David we'll see that

play05:09

they have basically nothing except for

play05:12

the fact that they are part of that role

play05:15

and if I want to expire a password by

play05:17

the way I can do that right here Force

play05:19

user to change password after next logon

play05:21

okay

play05:23

I'm not going to play with this for the

play05:24

moment though

play05:25

I'm going to Endeavor to close that and

play05:29

then I am going to close my connection

play05:31

now I'm going to create a new connection

play05:33

to the local one and this is going to be

play05:37

just David

play05:38

and username David

play05:42

and I'm going to test my connection put

play05:45

in my password

play05:47

and hit OK and successfully made the SQL

play05:50

connection so life is good okay now let

play05:54

me go ahead and connect

play05:56

and I've been playing around in here

play05:58

before so I am going to see if this

play06:01

works now I'm going to start

play06:04

by viewing my current role so that is

play06:07

Select current underscore role open and

play06:12

close parentheses and it tells me that

play06:15

currently I'm a member of world user at

play06:18

and then that little parentheses

play06:19

remember that's the or not parentheses

play06:22

punctuation mark That's the equivalent

play06:25

of an asterisk so it's a wild card that

play06:27

means everything so I'm World user at

play06:29

and I can connect from any computer so

play06:31

that's my current role and I can view

play06:33

the permissions that I have by using the

play06:37

command show grants

play06:40

and that will show me I have Grant usage

play06:44

to everything Grant select insert update

play06:46

on world 2 me from any location Grant

play06:51

World user everything to David okay so

play06:54

this gives me my grants so now I should

play06:56

be able to select asterisk from country

play07:01

and I should have permission to view

play07:05

whoops I need to select the database

play07:07

first use World there we go and then

play07:12

we'll select

play07:14

asterisk from country and now I should

play07:19

be able to see everything in country I

play07:22

should be able to see everything in my

play07:24

cities so I should have access to

play07:27

everything at this point in the world

play07:28

database now let me try to connect to

play07:31

another one view my schemas over here

play07:35

oh look I only see one thing in my

play07:37

schemas now I know there is a Sakia

play07:39

database

play07:40

so let me

play07:42

use Sakia

play07:46

even though I'm not seeing it over here

play07:47

I know it's there and it says no access

play07:50

is denied to user security so you can

play07:53

see that we have things actually blocked

play07:56

okay

play07:58

I also should not have the permission to

play08:01

drop a table

play08:02

or to create a table or anything like

play08:04

that so I'm going to create table

play08:10

oh let's give it a name of test

play08:13

and then I'm just going to try to create

play08:15

an empty table

play08:17

and it says no the create command denied

play08:20

to user so there you see we actually

play08:23

have block out permissions and we've

play08:26

given this user some permissions but not

play08:28

all permissions so that's how we're

play08:32

going to work on securing our databases

play08:34

as when we start adding users into them

play08:37

so we create the roles we Grant the

play08:40

permissions that we want to the roles

play08:42

once we Grant give the permissions then

play08:45

we'll create the users add the users to

play08:48

the roles and they should be ready to go

play08:51

with what they need remember you can

play08:53

always go back and we can grant more to

play08:55

the role using the Grant and we can

play08:58

revoke if we need to using the revoke

play09:01

minute it's the same thing revoke

play09:02

permission on to

play09:05

and specify what it is we want to revoke

play09:08

okay hopefully that gives you what you

play09:10

need to start working on securing your

play09:12

databases

Rate This

5.0 / 5 (0 votes)

相关标签
Database SecuritySQL RolesUser PermissionsCreate UsersGrant PrivilegesRevoke PrivilegesDatabase AccessSQL ManagementSecure DatabasesSQL Best Practices
您是否需要英文摘要?