mysql中用户的增删改查命令总结:
mysql用户的增删改查 | 命令示例 |
---|---|
增(创建用户) | create user chushiyan; create user chushiyan@localhost identified by “123456”; grant select on test.* to [email protected] identified by “123456”; |
删(删除用户) | drop user chushiyan@localhost; |
改(修改用户名和密码、设置密码过期) | rename user chushiyan@localhost to chushiyan0415@localhost; set password for chushiyan@localhost=password(“12345678”); alter user chushiyan@localhost password expire; |
查(查询用户) | select user,host,password from mysql.user where user = “chushiyan”; |
具体详情如下。
用户名长度不能超过16个字符。
用户名和密码大小写敏感。比如,chushiyan和Chushiyan是两个用户名。
# 创建用户chushiyan,
create user chushiyan;
执行上面的命令后,会在名为mysql的库中的user表中插入一条记录:
mysql> select user,host,password from mysql.user where user = 'chushiyan';
+-----------+------+----------+
| user | host | password |
+-----------+------+----------+
| chushiyan | % | |
+-----------+------+----------+
1 row in set (0.35 sec)
注释:
项 | 说明 |
---|---|
host列 | 首先,我们要知道,mysql不仅通过用户名、密码验证用户,同时还要根据host(来源主机)验证。比如用户jack,host值为192.168.0.11,那就只能从该IP主机连接Mysql,如果用其他IP主机即使用户名密码都正确,也无法连接mysql host列值为%,是通配符,对应任意长度的任意字符。跟查询语句中的%是一样的,如模糊查询语句:select * from user where name like ‘%shi%’ 。 因为命令create user chushiyan; 中没有指定host(来源主机),所以默认是%,即该用户可以从任意主机连接Mysql。 |
password列 | password为空,因为命令create user chushiyan; 中没有指定。这样的话,不用密码也能登陆mysql。比如直接通过 mysql -uchushiyan 就能连接mysql。 当然,不设置密码是非常危险的。 |
create user chushiyan@localhost identified by '123456';
执行上面的命令后,查询mysql.user表:
mysql> select user,host,password from mysql.user where user = 'chushiyan';
+-----------+-----------+-------------------------------------------+
| user | host | password |
+-----------+-----------+-------------------------------------------+
| chushiyan | % | |
| chushiyan | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+-----------+-------------------------------------------+
2 rows in set (0.00 sec)
第一条记录是前面插入的记录。第二条才是现在插入的记录。
注释:
项 | 说明 |
---|---|
user列 | 用户名可以重复。mysql数据库根据’user’@‘host’来确认记录是否唯一。 这里已经存在chushiyan@’%’,如果执行的是create user chushiyan identified by ‘123456’; 就必然报错,因为没指定host,就会再创建chushiyan@’%’ |
host列 | 来源主机。这里指定了localhost |
password列 | 加密后的密码。共41位。mysql4.1之前是16位 |
创建用户chushiyan0415同时指定host、password,并且赋予他对test库的查询权限:
grant select on test.* to [email protected] identified by '123456';
上面这个命令,其实就是下面这两行命令的简写:
create user [email protected] identified by '123456';
grant select on test.* to [email protected];
总结grant主要是用来给用户赋予权限,但是顺带手又可以用来创建用户。
执行上面的命令后,查询mysql.user表:
mysql> select user,host,password from mysql.user where user like 'chushiyan%';
+---------------+---------------+-------------------------------------------+
| user | host | password |
+---------------+---------------+-------------------------------------------+
| chushiyan | % | |
| chushiyan | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| chushiyan0415 | 192.168.0.150 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+---------------+---------------+-------------------------------------------+
3 rows in set (0.00 sec)
附录:grant赋予用户增加、删除、修改、查询的权限:
grant insert on test.* to [email protected] ;
grant delete on test.* to [email protected] ;
grant update on test.* to [email protected] ;
grant select on test.* to [email protected] ;
或者直接写成:
grant all on test.* to [email protected] ;
语法:
DROP USER user, [user], ...
示例:
drop user 'chushiyan0415'@'192.168.0.150';
drop user chushiyan@localhost;
语法格式:
RENAME USER <旧用户> TO <新用户>
示例:
rename user 'chushiyan'@'localhost' to 'chushiyan0415'@'localhost';
rename user 'chushiyan0415'@'localhost' to 'chushiyan0415'@'192.168.0.150';
Mysql中的用户没有太多属性,主要就是user、host、password,后两者还不是必须的。所以用户的修改主要就就是修改用户密码。所以在mysql5.6.6版本之前都没有alter user命令,提供了set password命令:
set password for chushiyan@localhost=password('12345678');
注释:
set password | 固定的语法 |
for chushiyan@localhost | for:指定要修改的用户。如果是修改当前用户,可以省略。 |
password() | 密码函数,对字符串进行加密。mysql.user表中存放的是加密后的密码, |
mysql5.6.6版本之后提供了alter user命令,主要用于设置密码过期。什么是密码过期?密码过期不是用户过期,用户仍然可以登录,只是无法做任何操作。
设置密码过期示例:
alter user chushiyan@localhost password expire;
使用select语句查询mysql.user表即可:
select user,host,password from mysql.user where user = 'chushiyan';