How to create and set up a MySQL user from the command line

Source: Internet
Author: User

I want to create a new user account on the MySQL server and give him the appropriate permissions and resource restrictions. How do I create and set up a MySQL user from the command line?

To access a MySQL server, you need to log in with a user account in order to proceed. Each MySQL user account has many properties associated with it, such as user name, password, and permissions and resource restrictions. Permissions define what a particular user can do in the MySQL server, and resource restrictions set a user license for a range of server resources. Creating or updating a user involves the management of all attributes of the user account.

The following shows how to create and set up a MySQL user in Linux.

First log in to the MySQL server as root.

$ mysql-u Root-p

When the verification prompt appears, enter the password for the root account of MySQL.

Create a MySQL user

Use the following command to create a user with a user name and password of "MyUser" and "MyPassword" respectively.

mysql> CREATE USER ' myuser ' @ ' localhost ' identified by ' mypassword ';

Once the user is created, all account details, including encrypted passwords, permissions, and resource restrictions, are stored in a table named user, which is present in the special MySQL database.

Verify that the account is created successfully by running the following command

Mysql> SELECT Host, user, password from mysql.user WHERE user= ' myuser ';

Give MySQL user permissions

A new MySQL user does not have any access rights, which means that you cannot do anything in the MySQL database. You have to give the user the necessary privileges. The following are some of the available permissions:

All: All available permissions

Create: Creating libraries, tables, and indexes

Lock_tables: Lock Table

Alter: Modify Table

Delete: Deleting a table

Insert: Inserting a table or column

SELECT: Retrieving data for a table or column

Create_view: Creating a View

Show_databases: List Database

Drop: Delete libraries, tables, and views

Run the following command to give the "myuser" user specific permissions.

Mysql> GRANT <privileges> on <database>.<table> to ' myuser ' @ ' localhost ';

The,<privileges> in the above command represents a comma-delimited list of permissions. If you want to assign permissions to any database (or table), use an asterisk (*) instead of the name of the database (or table).

For example, CREATE and INSERT permissions are assigned to all databases/tables:

Mysql> GRANT CREATE, INSERT on *. * to ' myuser ' @ ' localhost ';

Verify full permissions given to the user:

mysql> SHOW GRANTS for ' myuser ' @ ' localhost ';

Give all the permissions to all databases/tables:

Mysql> GRANT All on * * to ' myuser ' @ ' localhost ';

You can also delete the user's existing permissions. Use the following command to revoke the existing permissions for the "MyUser" account:

Mysql> REVOKE <privileges> on <database>.<table> from ' myuser ' @ ' localhost ';

Add Resource limits to users

In MySQL, you can set MySQL resource usage limits for individual users. The available resource limits are as follows:

Max_queries_per_hour: Maximum number of requests per hour allowed

Max_updates_per_hour: Maximum number of updates allowed per hour

Max_connections_per_hour: Maximum allowable connections per hour (with MySQL global variable: max_user_connections together determines the number of simultaneous connections to the database by the user)

Max_user_connections: Simultaneous connection to the server

Use the following command to add a resource limit for the "MyUser" account:

Mysql> GRANT USAGE on <database>.<table> to ' myuser ' @ ' localhost ' with <resource-limits>;

In <resource-limits> you can specify multiple resource limits that are separated using spaces.

For example, increase the Maxqueriesperhour and Maxconnectionsperhour resource limits:

Mysql> GRANT USAGE on *. * to ' myuser ' @ ' localhost ' with max_queries_per_hour-Max_connections_per_hour 6;

To verify the user's resource limits:

mysql> SHOW GRANTS for ' myuser ' @ ' localhost;

One final important step in creating and setting up a MySQL User:

mysql> FLUSH privileges;

This changes will take effect. Now the MySQL user account is ready to use.

Free pick up brother even it education original Linux OPS engineer video/Detailed Linux tutorials, details Inquiry official website customer Service: http://www.itxdl.cn/linux/

or hooking up with Q2430675018.

Welcome to the Linux Communication Group 478068715

How to create and set up a MySQL user from the command line

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.