MySQL 用户操作相关命令(用户创建、删除,密码修改)

一、名词解释
‘db’ 可以是“*”指代所有库,也可以是具体数据库;
‘user’ 是待更改的用户名;
‘host’ 可以是完整IP,也可以是主机名,还可以是通配符’%’等形式的IP,比如10.19.7_.%;

二、操作命令
1、登陆MySQL:

[root@Master ~]# mysql -u root -p

2、查询用户信息

// mysql版本7密码是authentication_string
mysql> select user,host,authentication_string from mysql.user;

// mysql版本5.5以下密码是password
mysql> select user,host,password from mysql.user;                

3、创建用户
a.创建对db1数据库有完全操作权限的名为user1的用户,并设置密码为password1

// 限制用户只能在本机上登录,设置host为'localhost',
mysql> grant all privileges on db1.* to 'user1'@'localhost' identified by 'password1';

// 允许该用户在所有主机上登录,设置host为'%',
mysql> grant all privileges on db1.* to 'user1'@'%' identified by 'password1';                

b.创建对db1数据库有查询、插入、修改、删除权限的名为user1的用户,并设置密码为password1

mysql> grant select,insert,update,delete on db1.* to 'user1'@'%' Identified by 'password1';

3、修改密码
a. 使用grant修改密码(用户不存在,则创建;存在则修改密码)

mysql> grant all privileges on db.* to user@host identified by 'password';
mysql> flush privileges; 

b. 使用set password方式来修改账户密码

mysql> set password for user@host=password('password');
mysql> flush privileges;
举例:
    mysql> set password for 'root'@'localhost' = password('新密码');    // 设置本地登录用户密码
    mysql> set password for 'root'@'%' = password('新密码');            // 设置远程登录用户密码

5、删除指定用户

a. mysql> delete from mysql.user where user='user' and host = 'host';
b. mysql> drop user user@host; 
mysql> flush privileges;

6、查看用户的权限

mysql> show grants for 'user1'@'%';   // 查看指定用户的权限
mysql> show grants;    // 查看当前用户的权限

7、赋予用户对数据库的操作权限

mysql> grant all privileges on db.* to user@host;        // 赋予所有权限
mysql> grant select,insert,update,delete on db.* to user@host;     // 赋予指定权限

8、收回用户对数据库的操作权限

mysql> revoke all privileges on db.* user@host;

猜你喜欢

转载自blog.csdn.net/u013241093/article/details/80879200