创建用户
语法:CREATE USER 用户名@主机 [IDENTIFIED BY '密码'];
例子:创建用户为testuser@localhost 密码为"testuser"
mysql> CREATE USER testuser@localhost IDENTIFIED BY 'testuser';Query OK, 0 rows affected (0.00 sec)
查看授权
mysql> show grants for testuser@localhost;+-----------------------------------------------------------------------------------------------------------------+| Grants for testuser@localhost |+-----------------------------------------------------------------------------------------------------------------+| GRANT USAGE ON *.* TO 'testuser'@'localhost' IDENTIFIED BY PASSWORD '*3A2EB9C80F7239A4DE3933AE266DB76A7846BCB8' |+-----------------------------------------------------------------------------------------------------------------+1 row in set (0.00 sec)
使用testuser登录:
[root@nginx ~]# mysql -u testuser -pEnter password: Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 885Server version: 5.1.72-log MySQL Community Server (GPL)Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>
只能做一些简单的操作,如查询;
mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || test |+--------------------+2 rows in set (0.00 sec)mysql>
无法执行创建等命令
mysql> create database t1;ERROR 1044 (42000): Access denied for user 'testuser'@'localhost' to database 't1'
为用户授权创建命令:
使用root登录
mysql> grant create on *.* to testuser@localhost;Query OK, 0 rows affected (0.00 sec)
使用testuser登录,创建t1数据库,
mysql> create database t1;Query OK, 1 row affected (0.00 sec)
创建表test
mysql> use t1;Database changedmysql> create table test(id int ,name varchar(20));Query OK, 0 rows affected (0.06 sec)
插入表数据
mysql> insert into test values (1,"zhangsan");ERROR 1142 (42000): INSERT command denied to user 'testuser'@'localhost' for table 'test'
没有权限插入
使用root登录为testuser授权
mysql> grant insert on *.* to testuser@localhost;Query OK, 0 rows affected (0.00 sec)
使用testuser登录
mysql> use t1;Database changedmysql> insert into test values(1,"zhangsan");Query OK, 1 row affected (0.00 sec)
查看用户权限
mysql> show grants for testuser@localhost -> ;+--------------------------------------------------------------------------------------------------------------------------+| Grants for testuser@localhost |+--------------------------------------------------------------------------------------------------------------------------+| GRANT INSERT, CREATE ON *.* TO 'testuser'@'localhost' IDENTIFIED BY PASSWORD '*3A2EB9C80F7239A4DE3933AE266DB76A7846BCB8' |+--------------------------------------------------------------------------------------------------------------------------+1 row in set (0.00 sec)
为testuser授权ALTERE
mysql> grant ALTER ON *.* to testuser@localhost;Query OK, 0 rows affected (0.00 sec)
使用testuser登录
mysql> ALTER table test ADD Age tinyint unsigned;Query OK, 1 row affected (0.07 sec)Records: 1 Duplicates: 0 Warnings: 0
使用root检查结果
mysql> desc test;+-------+---------------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+---------------------+------+-----+---------+-------+| id | int(11) | YES | | NULL | || name | varchar(20) | YES | | NULL | || Age | tinyint(3) unsigned | YES | | NULL | |+-------+---------------------+------+-----+---------+-------+3 rows in set (0.01 sec)
使用root授权test只能更新t1库下面的test表里面的Age字段,其他不允许更新。
mysql> grant update(Age) on t1.test to testuser@locahost;Query OK, 0 rows affected (0.00 sec)
使用testuser登录验证
mysql> update test set Age=10 where id=1;Query OK, 1 row affected (0.00 sec)Rows matched: 1 Changed: 1 Warnings: 0
可以修改其他字段无法修改
mysql> update test set name="test" where id=1;ERROR 1142 (42000): UPDATE command denied to user 'testuser'@'localhost' for table 'test'
为test用户授权super权限
使用root登录
mysql> grant super on *.* to testuser@localhost;Query OK, 0 rows affected (0.00 sec)
删除用户
drop user 'username'@'host';
重命名用户
rename user 'oldname' to 'newname';
取消授权
回收insert权限
mysql> REVOKE INSERT ON *.* FROM 'testuser'@'localhost';Query OK, 0 rows affected (0.00 sec)
使用show grants for testuser@localhost;查看权限
mysql> show grants for testuser@localhost;+--------------------------------------------------------------------------------------------------------------------------------+| Grants for testuser@localhost |+--------------------------------------------------------------------------------------------------------------------------------+| GRANT CREATE, ALTER, SUPER ON *.* TO 'testuser'@'localhost' IDENTIFIED BY PASSWORD '*3A2EB9C80F7239A4DE3933AE266DB76A7846BCB8' |+--------------------------------------------------------------------------------------------------------------------------------+1 row in set (0.00 sec)
inser权限已收回。