sudo mysql -u root -p
The sudo mysql -u root -p command is used to access MySQL as the root user with administrative privileges. After running the command, you will be prompted to enter the MySQL root user password.
If you have not set a password for the MySQL root user, the command may fail. If this is the case, you can either set a password or access MySQL without the -p (no password) option.
The SQL CREATE USER command is used to create a new user in MySQL with a username and password.
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
After creating the user, you need to grant permissions to it.
If you want to grant all permissions for a specific database, use:
GRANT ALL PRIVILEGES ON database_name.* TO 'user_name'@'localhost';
You can also specify permissions, such as SELECT, INSERT, UPDATE, DELETE, etc.
GRANT permission ON database_name.* TO 'user_name'@'localhost';
To grant permissions only on a specific table
GRANT ALL PRIVILEGES ON database_name.table_name TO 'user_name'@'localhost';
To grant permissions across all databases
GRANT ALL PRIVILEGES ON *.* TO 'user_name'@'localhost';
GRANT ALL PRIVILEGES ON *.* TO 'user_name'@'localhost' WITH GRANT OPTION;
The FLUSH PRIVILEGES command is used in MySQL to reload the permission tables, making effective the changes you made to user permissions, whether with the GRANT, REVOKE, or CREATE USER command.
FLUSH PRIVILEGES;
SHOW GRANTS FOR 'username'@'localhost';
The SHOW GRANTS FOR username'@'localhost; command displays the permissions associated with the specified user in MySQL. It is useful for checking the privileges a user has over the database.
The REVOKE command is used to remove specific privileges from a user in MySQL.
REVOKE ALL PRIVILEGES ON database_name.* FROM 'user_name'@'localhost';
SELECT User, Host FROM mysql.user;
The SELECT User, Host FROM mysql.user; command is used to query the mysql.user table in MySQL, which stores information about all users created in the system.
SELECT USER();
The SELECT USER(); command in MySQL returns the username and hostname you are using in the current session. It is a function that shows which user account was used to connect to the database, in the format user@host.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3