Consultas y Transformaci贸n Avanzada de Datos
Dise帽o y Optimizaci贸n Avanzada de Bases de Datos con SQL y MySQL
Construcci贸n de Base de Datos en MySQL: Terminal, Tablas y Datos Simulados
Transformaci贸n de Datos con SQL: De Consultas Simples a Superqueries
Columnas Generadas en SQL
Expresiones Regulares y Slugs en SQL y MySQL: Casos de Uso Reales
Automatizaci贸n, Reutilizaci贸n y Eficiencia en Consultas
Vistas y Tablas Virtuales en SQL
Consultas Anidadas y Vistas Materializadas
Triggers y Vistas Materializadas en MySQL (Kinda)
Automatizaci贸n de Bases de Datos con Triggers en MySQL
Llaves Primarias e 脥ndices
Trabajo con Datos Avanzados (JSON)
Uso de JSON en MySQL: Almacenamiento Eficiente de Datos Estructurados
B煤squedas Avanzadas con JSON en MySQL: Indexaci贸n y Optimizaci贸n
Joins en SQL: Conecta Tablas y Mejora tus Consultas
Motores de Almacenamiento y Encodings
Gesti贸n de Usuarios y Permisos en SQL
Gesti贸n Avanzada y An谩lisis de Bases de Datos
Information Schema en MySQL: An谩lisis Interno de tu Base de Datos
Mysqldump
Domina las Migrations: Evoluciona y Gestiona tu Base de Datos con Eficiencia
Optimiza tus Decisiones con Metabase
You don't have access to this class
Keep learning! Join and start boosting your career
Managing users and permissions in MySQL is a fundamental aspect of maintaining the security and integrity of our databases. Many security and data loss problems can be avoided simply by implementing a proper granular permissions strategy, rather than sharing administrator credentials. Let's see how we can effectively and securely create and manage users in MySQL.
One of the biggest problems in development and production environments is the sharing of administrative credentials. This practice, although common, represents a serious security risk. When an administrator shares their username and password, they lose the ability to track who made what changes to the database.
MySQL provides a robust system for creating users with specific permissions. This allows:
To create a new user in MySQL, we use the following syntax:
CREATE USER 'username'@'host' IDENTIFIED BY 'password';
For example:
CREATE USER 'becoplatzi'@'%' IDENTIFIED BY 'beco123';
In this command:
becoplatzi
is the user's name%
is a wildcard that allows connection from any hostbeco123
is the passwordWe can limit where a user can connect from:
'username'@'localhost'
.'username'@'%'
.'username'@'192.168.0.101'
.This ability to restrict by location adds an additional layer of security to our databases.
Once the user has been created, we must assign specific permissions to the user. The most common permissions are:
To assign these permissions, we use the GRANT
command:
GRANT permission1, permission2, ... ON database_name.table_name TO 'username'@'host' [WITH GRANT OPTION];
For example:
GRANT CREATE, ALTER, DROP, INSERT, UPDATE, DELETE, SELECT, REFERENCES, INDEX ON platzisql.* TO 'becoplatzi'@'%' WITH GRANT OPTION;
The WITH GRANT OPTION
option allows the user to grant his own permissions to other users.
For a DBA (Database Administrator):
GRANT CREATE, ALTER, DROP, INSERT, UPDATE, DELETE, SELECT, REFERENCES, INDEX ON database.* TO 'dba_user'@'host';
For an application:
GRANT INSERT, UPDATE, SELECT ON database.* TO 'app_user'@'host';
For a data analyst:
GRANT SELECT, INDEX ON database.* TO 'analyst_user'@'host';
For a developer:
GRANT CREATE, ALTER, INSERT, SELECT, REFERENCES, INDEX ON database.* TO 'dev_user'@'host';
To see the existing users in MySQL:
SELECT host, user FROM mysql.user;
To connect to a specific user:
mysql -u username -p
Once logged in, we can check which databases we have access to:
SHOW DATABASES;
And to use a specific database:
USE database_name;SHOW TABLES;
Important note: MySQL uses its own tables to manage users and permissions. These tables are located in the mysql
database, which makes the system transparent and robust.
Proper management of users and permissions is critical to maintaining the security and integrity of our databases. Implementing a clear strategy for assigning permissions according to specific roles will help us prevent problems and maintain effective control over who can do what in our system. What user management strategies do you use in your projects? Share your experience in the comments.
Contributions 0
Questions 0
Want to see more contributions, questions and answers from the community?