User Management
SELECT user, host FROM mysql.user;
Fetches a list of all users and their associated hosts.CREATE USER 'username'@'host' IDENTIFIED BY 'password';
Creates a new user with a specified host and password.CREATE USER 'username'@'host' IDENTIFIED WITH 'plugin' BY 'password';
Creates a user with a specific authentication plugin.SELECT user, host, plugin, authentication_string FROM mysql.user;
Displays detailed information about users.DROP USER 'username'@'host';
Deletes a user from the MySQL server.RENAME USER 'olduser'@'oldhost' TO 'newuser'@'newhost';
Renames an existing user.UPDATE mysql.user SET host = 'newhost' WHERE user = 'username' AND host = 'oldhost';
Updates thehost
for an existing user.ALTER USER 'username'@'host' IDENTIFIED WITH 'plugin' BY 'password';
Changes a user’s authentication plugin and password.ALTER USER 'username'@'host' ACCOUNT LOCK;
Locks a user account to prevent login.ALTER USER 'username'@'host' ACCOUNT UNLOCK;
Unlocks a previously locked user account.
Password Management
ALTER USER 'username'@'host' IDENTIFIED BY 'newpassword';
Changes the password for a user.ALTER USER 'username'@'host' PASSWORD EXPIRE;
Forces the user to change their password on the next login.SET GLOBAL default_password_lifetime = 90;
Sets the global password expiration policy to 90 days.ALTER USER 'username'@'host' IDENTIFIED WITH 'plugin' BY 'password';
Resets the user’s authentication plugin and password.ALTER USER 'username'@'host' REQUIRE SSL;
Enforces SSL for a specific user.ALTER USER 'username'@'host' IDENTIFIED BY '';
Removes the password for the specified user.SHOW VARIABLES LIKE 'validate_password%';
Displays password validation policies.SET GLOBAL validate_password_policy = MEDIUM;
Configures the password strength policy to medium.SET GLOBAL validate_password_length = 12;
Sets the minimum password length.SET GLOBAL validate_password_number_count = 3;
Prevents password reuse for the last 3 passwords.
Database Management
CREATE DATABASE database_name;
Creates a new database.DROP DATABASE database_name;
Deletes a database.SHOW DATABASES;
Lists all databases on the server.USE database_name;
Switches to a specific database for queries.SELECT DATABASE();
Displays the currently selected database.RENAME TABLE old_db.table_name TO new_db.table_name;
Moves a table from one database to another.mysqldump -u root -p database_name > backup.sql;
Backs up a database to a.sql
file (command-line).mysql -u root -p database_name < backup.sql;
Restores a database from a.sql
file.SELECT table_schema AS database_name, SUM(data_length + index_length) / 1024 / 1024 AS size_in_mb FROM information_schema.tables GROUP BY table_schema;
Displays the size of each database.SHOW TABLES FROM database_name;
Lists all tables in a specific database.
Privileges Management
GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'host';
Grants all privileges on a database to a user.GRANT SELECT, INSERT, UPDATE ON database_name.* TO 'username'@'host';
Grants specific privileges to a user.GRANT ALL PRIVILEGES ON *.* TO 'username'@'host' WITH GRANT OPTION;
Grants all privileges on all databases, including the ability to grant privileges.REVOKE SELECT, INSERT ON database_name.* FROM 'username'@'host';
Removes specific privileges from a user.SHOW GRANTS FOR 'username'@'host';
Displays the privileges granted to a user.SELECT * FROM information_schema.user_privileges;
Shows global privileges for all users.REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'username'@'host';
Removes all privileges and the ability to grant privileges from a user.FLUSH PRIVILEGES;
Reloads the privilege tables to apply changes.GRANT SUPER ON *.* TO 'username'@'host';
Grants superuser privileges to a user.REVOKE SUPER ON *.* FROM 'username'@'host';
Removes superuser privileges from a user.
Host Management
CREATE USER 'username'@'%' IDENTIFIED BY 'password';
Allows a user to connect from any host.CREATE USER 'username'@'192.168.1.100' IDENTIFIED BY 'password';
Restricts user access to a specific IP.SELECT user, host FROM mysql.user;
Lists all users and their associated hosts.UPDATE mysql.user SET host = 'newhost' WHERE user = 'username' AND host = 'oldhost';
Changes the host for an existing user.CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
Restricts access to connections fromlocalhost
.DELETE FROM mysql.user WHERE user = 'username' AND host = 'specific_host';
Removes a user’s access from a specific host.CREATE USER 'username'@'192.168.1.%' IDENTIFIED BY 'password';
Allows a user to connect from a subnet.CREATE USER 'username'@'newhost' IDENTIFIED BY 'password';
Grants a user access from multiple IPs by creating separate entries for each host.SHOW GRANTS FOR 'username'@'host';
Displays privileges for a specific user/host pair.UPDATE mysql.user SET host = 'localhost' WHERE user = 'username';
Resets a user’s host tolocalhost
.