MySQL_Roles_and_Users_Management__1734785003
MySQL_Roles_and_Users_Management__1734785003
One difference between roles and users is that CREATE ROLE creates an authorization identifier
that is locked by default, whereas CREATE USER creates an authorization identifier that is
unlocked by default.
User Managements:
To create a new user in the MySQL database, you use the CREATE USER statement.
The username is the name of the user while the hostname is the name of the host from which the
user connects to the MySQL Server.
The hostname part of the account name is optional. If you omit the hostname, the user can
connect from any host.
username@%
If the username and hostname contain special characters, such as spaces or hyphens, you
need to enclose the username and hostname separately in quotes, like this:
'user-name'@'hostname'
In addition to the single quote ('), you can use backticks ( `) or double quotation mark (").
specify the password for the user after the IDENTIFIED BY keywords.
Example:
MySQL GRANT:
The CREATE USER statement creates a user account with no privileges. It means that the user
account can log in to the MySQL Server but cannot do anything such as selecting a database and
querying data from tables.
To enable the user account to work with database objects, you need to grant it privileges. You use
the GRANT statement to assign one or more privileges to the user account.
Global Privileges:
Global privileges apply to all databases in a MySQL Server. To assign all global privileges, you use
the *.* syntax, for example:
The account user naveen@localhost can manage all databases of the current MySQL Server.
Database privileges:
Table privileges:
Table privileges apply to all columns in a table. To assign table-level privileges, you use the ON
database_name.table_name syntax. For example:
In this example, naveen@localhost can manage rows from the employees table in the
classicmodels database.
If you skip the database name, MySQL uses the default database or issues an error if there is no
default database.
Column privileges:
Column privileges apply to individual columns within a table. You must specify the column or
columns for each privilege. For example:
Stored routine privileges apply to stored procedures and stored functions. For example:
Proxy user privileges allow one user to be a proxy for another. The proxy user gets all the privileges
of the proxied user. For example:
MySQL REVOKE:
The REVOKE statement revokes one or more privileges from a user account.
1)The basic syntax of the REVOKE statement that revokes one or more privileges from user
accounts:
2)To revoke all privileges from a user, you use the following form of the REVOKE ALL
statement:
Roles management:
In MySQL, a role is a named collection of privileges that can be granted to user accounts or other
roles. This simplifies privilege management by allowing administrators to assign a set of privileges
to multiple users through a single role, rather than granting privileges individually to each user.
USE crm;
NOTE: If you omit the host part, it defaults to ‘%’ which means any host.
Creating users:
Activating Roles:
when you granted roles to a user account, it didn’t automatically activate the roles when the user
account connects to the database server.
To specify which roles should be active each time a user account connects to the database
server, you can use the SET DEFAULT ROLE statement.
NOTE:
The USAGE means that the USER can log in to the database but has no privilege.
Dropping roles and users:
Lock the user to prevent anyone from using it to connect to the server: