首页 > 代码库 > 3.MYSQL之创建新用户

3.MYSQL之创建新用户

登录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 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

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> 

 

3.MYSQL之创建新用户