MySQL user-related actions

Source: Internet
Author: User
Tags mysql command line

MySQL Default has a root user, but this user rights are too large, generally only in the management of the database when used. If you are connecting to a MySQL database in your project, it is recommended that you create a new user with a smaller permission to connect.

You can create a new user for MySQL by entering the following command in MySQL command line mode:

CREATE USER username IDENTIFIED BY ‘password‘;

The new user is created, but at this point, if logged in as this user, will be an error, because we have not assigned the appropriate permissions for this user, the command to assign permissions are as follows:

GRANT ALL PRIVILEGES ON *.* TO ‘username‘@‘localhost‘ IDENTIFIED BY ‘password‘;

Grant all permissions for the username user on all databases.

GRANT REPLICATION SLAVE ON *.* TO ‘backup‘@‘address‘ IDENTIFIED BY ‘backup_xxx‘;Create accounts for master-slave synchronization data

If you find that the permission you just gave is too large, if we just want to grant it permissions on a database, then we need to switch to the root user to revoke the permissions just now and re-authorize:

REVOKE ALL PRIVILEGES ON *.* FROM ‘username‘@‘localhost‘;

GRANT ALL PRIVILEGES ON dbnames.* TO ‘username‘@‘localhost‘ IDENTIFIED BY ‘password‘;

You can even specify that the user can only perform select and UPDATE commands:

GRANT SELECT, UPDATE ON dbnames.* TO ‘username‘@‘localhost‘ IDENTIFIED BY ‘password‘;

In this way, to log in to MySQL again with username, only the Dbnames database is visible to it, and if you only grant it SELECT permission, then it cannot execute statements other than select.

In addition, each time you adjust permissions, you typically need to perform the following statement refresh permissions:

FLUSH PRIVILEGES;

Delete the user you just created:

DROP USER [email protected];

With the above commands in mind, you can find that the host of the response (that is, the content after the @ symbol) is specified, either by authorization or by revoking the authorization, since the above pass command is actually the user table in the MySQL database and can be viewed with the following command:

SELECT User, Host FROM user;

Of course, this table also contains a lot of other things such as user passwords, permission settings, and so on, especially when you need to be careful.

Forgot root password:

Turn on the firewall to make sure that no one else is connected to the database:

Close MySQL and modify the configuration file (MY.CNF): [mysqld] Add the following: skip-grant-tables .

Restart MySQL to change the root password after connection

MySQL user-related actions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.