文章目录1. 用户管理1.1 用户1.1.1 用户信息1.1.2 创建用户1.1.3 删除用户1.1.4 修改用户密码1.2 数据库的权限1.2.1 给用户授权1.2.2 回收权限1. 用户管理如果我们只能使用root用户这样存在安全隐患。这时就需要使用MySQL的用户管理。1.1 用户1.1.1 用户信息MySQL中的用户都存储在系统数据库mysql的user表中mysql use mysql; Database changed mysql select host,user,authentication_string from user; --------------------------------------------------------------------- | host | user | authentication_string | --------------------------------------------------------------------- | localhost | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | | localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | | localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | --------------------------------------------------------------------- -- 可以通过desc user初步查看一下表结构字段解释host 表示这个用户可以从哪个主机登陆如果是localhost表示只能从本机登陆user 用户名authentication_string 用户密码通过password函数加密后的*_priv 用户拥有的权限1.1.2 创建用户语法create user 用户名登陆主机/ip identified by 密码;案例mysql create user whblocalhost identified by 12345678; Query OK, 0 rows affected (0.06 sec) mysql select user,host,authentication_string from user; --------------------------------------------------------------------- | user | host | authentication_string | --------------------------------------------------------------------- | root | % | *A2F7C9D334175DE9AF4DB4F5473E0BD0F5FA9E75 | | mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | | mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | | whb | localhost | *84AAC12F54AB666ECFC2A83C676908C8BBC381B1 | -- 新增用户 --------------------------------------------------------------------- 4 rows in set (0.00 sec) -- 此时便可以使用新账号新密码进行登陆啦 -- 备注可能实际在设置密码的时候因为mysql本身的认证等级比较高一些简单的密码无法设置会爆出如下报错 -- ERROR 1819 (HY000): Your password does not satisfy the current policy requirements -- 解决方案https://blog.csdn.net/zhanaolu4821/article/details/93622812 -- 查看密码设置相关要求SHOW VARIABLES LIKE validate_password%; -- 这个大家下来自己玩玩 -- 关于新增用户这里需要大家注意不要轻易添加一个可以从任意地方登陆的user。1.1.3 删除用户语法drop user 用户名主机名案例mysql select user,host,authentication_string from user; --------------------------------------------------------------------- | user | host | authentication_string | --------------------------------------------------------------------- | root | % | *A2F7C9D334175DE9AF4DB4F5473E0BD0F5FA9E75 | | mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | | mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | | whb | localhost | *84AAC12F54AB666ECFC2A83C676908C8BBC381B1 | --------------------------------------------------------------------- 4 rows in set (0.00 sec) mysql drop user whb; -- 尝试删除 ERROR 1396 (HY000): Operation DROP USER failed for whb% -- 直接给个用户名不能删除它默认是%表示所有地方可以登陆的用户 mysql drop user whblocalhost; -- 删除用户 Query OK, 0 rows affected (0.00 sec) mysql select user,host,authentication_string from user; --------------------------------------------------------------------- | user | host | authentication_string | --------------------------------------------------------------------- | root | % | *A2F7C9D334175DE9AF4DB4F5473E0BD0F5FA9E75 | | mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | | mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | --------------------------------------------------------------------- 3 rows in set (0.00 sec)1.1.4 修改用户密码语法自己改自己密码set passwordpassword(新的密码); -- 自己来试试root用户修改指定用户的密码set password for 用户名主机名password(新的密码)案例mysql select host,user, authentication_string from user; --------------------------------------------------------------------- | host | user | authentication_string | --------------------------------------------------------------------- | % | root | *A2F7C9D334175DE9AF4DB4F5473E0BD0F5FA9E75 | | localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | | localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | | localhost | whb | *84AAC12F54AB666ECFC2A83C676908C8BBC381B1 | --------------------------------------------------------------------- 4 rows in set (0.00 sec) mysql set password for whblocalhostpassword(87654321); Query OK, 0 rows affected, 1 warning (0.00 sec) mysql select host,user, authentication_string from user; --------------------------------------------------------------------- | host | user | authentication_string | --------------------------------------------------------------------- | % | root | *A2F7C9D334175DE9AF4DB4F5473E0BD0F5FA9E75 | | localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | | localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | | localhost | whb | *5D24C4D94238E65A6407DFAB95AA4EA97CA2B199 | --------------------------------------------------------------------- 4 rows in set (0.00 sec)1.2 数据库的权限MySQL数据库提供的权限列表1.2.1 给用户授权刚创建的用户没有任何权限。需要给用户授权。语法grant 权限列表 on 库.对象名 to 用户名登陆位置 [identified by 密码]说明权限列表多个权限用逗号分开grant select on ... grant select, delete, create on .... grant all [privileges] on ... -- 表示赋予该用户在该对象上的所有权限*.*: 代表本系统中的所有数据库的所有对象表视图存储过程等库.*: 表示某个数据库中的所有数据对象(表视图存储过程等)identified by可选。 如果用户存在赋予权限的同时修改密码,如果该用户不存在就是创建用户案例-- 使用root账号 -- 终端A mysql show databases; -------------------- | Database | -------------------- | information_schema | | 57test | | bit_index | | ccdata_pro | | innodb_test | | musicserver | | myisam_test | | mysql | | order_sys | | performance_schema | | scott | | sys | | test | | vod_system | -------------------- 14 rows in set (0.00 sec) mysql use test; Database changed mysql show tables; ---------------- | Tables_in_test | ---------------- | account | | student | | user | ---------------- 3 rows in set (0.01 sec) -- 给用户whb赋予test数据库下所有文件的select权限 mysql grant select on test.* to whblocalhost; Query OK, 0 rows affected (0.01 sec) -- 使用whb账号 -- 终端B mysql show databases; -------------------- | Database | -------------------- | information_schema | -------------------- 1 row in set (0.00 sec) -- 暂停等root用户给whb赋完权之后在查看 mysql show databases; -------------------- | Database | -------------------- | information_schema | | test | -- 赋完权之后就能看到新的表 -------------------- 2 rows in set (0.01 sec) mysql use test; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql show tables; ---------------- | Tables_in_test | ---------------- | account | | student | | user | ---------------- 3 rows in set (0.00 sec) mysql select * from account; --------------------- | id | name | blance | --------------------- | 2 | 李四 | 321.00 | | 3 | 王五 | 5432.00 | | 4 | 赵六 | 543.90 | | 5 | 赵六 | 543.90 | --------------------- 4 rows in set (0.00 sec) -- 没有删除权限 mysql delete from account; ERROR 1142 (42000): DELETE command denied to user whblocalhost for table account -- 备注特定用户现有查看权限 mysql show grants for whb%; ----------------------------------------------- | Grants for whb% | ----------------------------------------------- | GRANT USAGE ON *.* TO whb% | | GRANT ALL PRIVILEGES ON test.* TO whb% | ----------------------------------------------- 2 rows in set (0.00 sec) mysql show grants for root%; ------------------------------------------------------------- | Grants for root% | ------------------------------------------------------------- | GRANT ALL PRIVILEGES ON *.* TO root% WITH GRANT OPTION | ------------------------------------------------------------- 1 row in set (0.00 sec)注意如果发现赋权限后没有生效执行如下指令flush privileges;1.2.2 回收权限语法revoke 权限列表 on 库.对象名 from 用户名登陆位置示例-- 回收whb对test数据库的所有权限 -- root身份终端A mysql revoke all on test.* from whblocalhost; Query OK, 0 rows affected (0.00 sec) -- whb身份终端B mysql show databases; -------------------- | Database | -------------------- | information_schema | | test | -------------------- 2 rows in set (0.00 sec) mysql show databases; -------------------- | Database | -------------------- | information_schema | -------------------- 1 row in set (0.00 sec)