MySQL adding users, deleting users and authorization
Add users, create new databases, authorize users, delete users, and change passwords in MySql (note that each line is followed by ; to indicate the end of a command statement):
1. Create a new user
1.1 Log in to MYSQL:
@>mysql -u root -p
@>Password
1.2 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: The "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.
1.3 Then log in:
mysql>exit;
@>mysql -u test -p
@>Enter password
mysql>Login successful
2. Authorize users
Authorization format: grant authority on database.* to username@login host identified by "password";
2.1 Log in to MYSQL (with ROOT permissions), log in as ROOT here:
@>mysql -u root -p
@>Password
2.2 First create a database (testDB) for the user:
mysql>create database testDB;
2.3 Authorize the test user to have all permissions on the testDB database (all permissions on a certain database):
mysql>grant all privileges on testDB.* to test@localhost identified by '1234';
mysql>flush privileges;//Refresh the system privileges table
Format: grant permission on database.* to username@login host identified by "password";
2.4 If you want to specify some permissions to a user, you can write like this:
mysql>grant select,update on testDB.* to test@localhost identified by '1234';
mysql>flush privileges; //Refresh the system privileges table
2.5 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 on all databases.
//@"%" indicates authorization for all non-local 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 the 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 password of the specified user
@>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 data table structure
mysql>describe table name;
9. Delete database and data table
mysql>drop database database name;
mysql>drop table data table name;