一般就用GRANT来创建用户和授权
语法格式
GRANT
priv_type [(column_list)]
[, priv_type [(column_list)]] ...
ON [object_type] priv_level
TO user_specification [, user_specification] ...
[REQUIRE {NONE | ssl_option [[AND] ssl_option] ...}]
[WITH {GRANT OPTION | resource_option} ...]
grant all on test.* to "testuser1"@"127.0.0.1" identified by "test";
划了下划线的地方解释一下
all的位置是可使用的命令,还可以是select,update,delete,使用时用逗号分隔;
test.*的位置是哪个库的哪个表,这里代表test数据库中的所有表;
"testuser1"@"127.0.0.1" 是指定用户和来自的ip
"test"是密码为test
以上任何一项前面都有一个介词,记忆时可能会有点难,要多练习。
另外,这条命令会先创建一个用户,然后再进行相应的授权。
如果该用户和ip的组合已经存在,那么就会修改密码;而如果不存在,则创建新的组合(在mysql数据库的user表)
mysql> select user,password,host from mysql.user; +-----------+-------------------------------------------+-------------+ | user | password | host | +-----------+-------------------------------------------+-------------+ | root | *1981B3D87F8E4997E444AC2F469A74D5145EEB7X | localhost | | root | | vmtest.js | | root | *1981B3D87F8E4997E444AC2F469A74D5145EEB7X | 127.0.0.1 | | root | | ::1 | | | | localhost | | | | vmtest.js | | root | *1981B3D87F8E4997E444AC2F469A74D5145EEB7X | 192.168.5.% | | coosh | *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC2X | 127.0.0.1 | | testuser1 | *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC2X | 127.0.0.1 | +-----------+-------------------------------------------+-------------+ 9 rows in set (0.00 sec) mysql> grant select,update on test.* to "coosh"@"192.168.5.%" identified by "XXX"; Query OK, 0 rows affected (0.00 sec) mysql> select user,password,host from mysql.user; +-----------+-------------------------------------------+-------------+ | user | password | host | +-----------+-------------------------------------------+-------------+ | root | *1981B3D87F8E4997E444AC2F469A74D5145EEB7X | localhost | | root | | vmtest.js | | root | *1981B3D87F8E4997E444AC2F469A74D5145EEB7X | 127.0.0.1 | | root | | ::1 | | | | localhost | | | | vmtest.js | | root | *1981B3D87F8E4997E444AC2F469A74D5145EEB7X | 192.168.5.% | | coosh | *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC2X | 127.0.0.1 | | testuser1 | *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC2X | 127.0.0.1 | | coosh | *91A66B6C7D4FABDB9225374DC006BB98E6C6F0CX | 192.168.5.% | +-----------+-------------------------------------------+-------------+ 10 rows in set (0.00 sec)
时间: 2024-12-04 19:48:30