3.MySQL之创建/删除用户

时间:2023-11-11 15:40:08

登录mysql服务器后可使用grant命令来创建用户并赋予相关权限。

mysql> use mysql;
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> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
-> ON mysql.*
-> TO firstuser@localhost IDENTIFIED BY 'password';
Query OK, 0 rows affected, 1 warning (0.11 sec)

以上命令会在mysql数据库中的user表创建一条用户信息记录,并赋予对数据库mysql的SELECT,INSERT,UPDATE,DELETE,

CREATE,DROP操作权限.MySQL 的SQL语句以分号 (;) 作为结束标识。

这时可以通过以下命令来查看新建的用户是否存在。

mysql> select user,host,authentication_string from user;
+------------------+-----------+-------------------------------------------+
| user | host | authentication_string |
+------------------+-----------+-------------------------------------------+
| root | localhost | *74ED8DCE9B1B4B780E427473DDD66F8BE6398E8E |
| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| debian-sys-maint | localhost | *0E2901479C3B89118B95A2622B7F895DFC60C4A0 |
| zara | localhost | *08F7D6C84224646B57BDF66A6480F337F403F10C |
| firstuser | localhost | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 |
| rick | localhost | *35EB40C575CD0DAA851919E0E3E28C0718879407 |
+------------------+-----------+-------------------------------------------+
6 rows in set (0.00 sec)

注意:在 MySQL5.7 中 user 表的 password项 已换成了authentication_string

可以授予的权值如下表:

说明
alter 修改已存在的数据表(例如增加/删除列)和索引
create 建立新的数据库或数据表
delete 删除表的记录
drop 删除数据表或数据库
index 建立或删除索引
insert 增加表的记录
select 显示/搜索表的记录
update 修改表中已存在的记录
lock tables 允许锁定表
all 以上权限都有
可见,新用户已经存在,此时就可以以用户进行登录了。 
mysql> \q
Bye
deamon@deamon-H55M-S2:~$ mysql -u firstuser -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.16-0ubuntu0.16.04.1 (Ubuntu) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>

删除用户的方式很简单:

mysql> DROP user firstuser@localhost
或者
mysql> delete from user where user = 'firstuser';
 

即可。