MySql user management (transfer)

Mysql create and delete users

Reprinted from: http://www.cnblogs.com/fly1988happy/archive/2011/12/15/2288554.html

 

 

Add users, create new databases, authorize users, delete users, and change passwords in MySql (note that each line is followed by a ; indicating the end of a command statement):

1. Create a new user

  • Login to MYSQL:

  @>mysql -u root -p

  @> password

  • Create user:

  mysql> insert into mysql.user(Host,User,Password) values("localhost","test",password("1234"));

  This creates a user named: test with password: 1234.

  Note : "localhost" here means that the user can only log in locally and cannot log in remotely on another machine. If you want to log in remotely, change "localhost" to "%", which means you can log in from any computer. You can also specify a machine that can log in remotely.

  • Then log in:

  mysql>exit;

  @>mysql -u test -p

  @> enter password

  mysql> login successful

2. Authorize the user

  Authorization format: grant permission on database.* to username@login host identified by "password"; 

  • Log in to MYSQL (with ROOT privileges), here log in as ROOT:

  @>mysql -u root -p

  @> password

  • First create a database (testDB) for the user:

  mysql>create database testDB;

  • Authorize the test user to have all permissions of the testDB database (all permissions of a database):

   mysql>grant all privileges on testDB.* to test@localhost identified by '1234';

   mysql>flush privileges;//Refresh the system privilege table

  Format: grant permission on database.* to username@login host identified by "password"; 

  • If you want to assign partial permissions to a user, you can write:

  mysql>grant select,update on testDB.* to test@localhost identified by '1234';

  mysql>flush privileges; //Refresh the system privilege table

  • Authorize the test user to have certain permissions on all databases :   

  mysql>grant select,delete,update,create,drop on *.* to test@"%" identified by "1234";

     //The test user has select, delete, update, create, drop permissions for all databases.

  // @"%"  means authorize all non-localhost hosts, excluding localhost. (The localhost address is set to 127.0.0.1. If it is set to a real local address, I don't know if it is possible, there is no verification.)

//Authorize localhost: add a sentence grant all privileges on testDB.* to test@localhost identified by '1234';

3. Delete user

 @>mysql -u root -p

 @> password

 mysql>Delete FROM user Where User='test' and Host='localhost';

 mysql>flush privileges;

 mysql>drop database testDB; //Delete the user's database

Delete account and permissions: >drop user username@'%';

        >drop user username@localhost; 

4. Modify the specified user password

  @>mysql -u root -p

  @> password

  mysql>update mysql.user set password=password('新密码') where User="test" and Host="localhost";

  mysql>flush privileges;

5. List all databases

mysql>show database;

6. Switch database

mysql>use 'database name';

7. List all tables

mysql>show tables;

8. Display the data table structure

mysql>describe 表名;

9. Delete databases and data tables

mysql>drop database database name;

mysql>drop table data table name;

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327016498&siteId=291194637