赞
踩
MySQL中的用户,都存储在系统数据库mysql的user表中
- mysql> select host,user,authentication_string from user;
- +-----------+---------------+-------------------------------------------+
- | host | user | authentication_string |
- +-----------+---------------+-------------------------------------------+
- | localhost | root | |
- | localhost | mysql.session | |
- | localhost | mysql.sys | |
- +-----------+---------------+-------------------------------------------+
- 3 rows in set (0.00 sec)
user是个表结构
字段解释:
语法:
create user '用户名'@'登陆主机/ip' identified by '密码';
案例:
- mysql> create user 'dgz'@'localhost' identified by '123456';
- ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
- # 如果出现上面问题可以先刷新一下
- mysql> flush privileges;
- Query OK, 0 rows affected (0.01 sec)
-
- mysql> create user 'dgz'@'localhost' identified by '123456';
- Query OK, 0 rows affected (0.00 sec)
- mysql> select host,user,authentication_string from user;
- +-----------+---------------+-------------------------------------------+
- | host | user | authentication_string |
- +-----------+---------------+-------------------------------------------+
- | localhost | root | *03CDB52F58482A55A757B2FDFA03F1A14EDA5D99 |
- | localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
- | localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
- | localhost | dgz | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
- +-----------+---------------+-------------------------------------------+
- 4 rows in set (0.00 sec)
语法:
drop user '用户名'@'主机名
案例:
- mysql> drop user 'dgz'@'localhost';
- Query OK, 0 rows affected (0.00 sec)
-
- mysql> select host,user,authentication_string from user;
- +-----------+---------------+-------------------------------------------+
- | host | user | authentication_string |
- +-----------+---------------+-------------------------------------------+
- | localhost | root | *03CDB52F58482A55A757B2FDFA03F1A14EDA5D99 |
- | localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
- | localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
- +-----------+---------------+-------------------------------------------+
- 3 rows in set (0.00 sec)
语法:
自己改自己密码
set password=password('新的密码');
root用户修改指定用户的密码
set password for '用户名'@'主机名'=password('新的密码');
直接修改mysql库中的user表
- mysql> update user set authentication_string=password('222222') where user='dgz';
- Query OK, 1 row affected, 1 warning (0.00 sec)
- Rows matched: 1 Changed: 1 Warnings: 1
-
- mysql> select host,user,authentication_string from user;
- +-----------+---------------+-------------------------------------------+
- | host | user | authentication_string |
- +-----------+---------------+-------------------------------------------+
- | localhost | root | *03CDB52F58482A55A757B2FDFA03F1A14EDA5D99 |
- | localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
- | localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
- | localhost | dgz | *A0C1808B1A47CECD5C161FEE647F5427F4EB6F98 |
- +-----------+---------------+-------------------------------------------+
- 4 rows in set (0.00 sec)
- mysql> set password for 'dgz'@'localhost'=password('111111');
- Query OK, 0 rows affected, 1 warning (0.00 sec)
-
- mysql> select host,user,authentication_string from user;
- +-----------+---------------+-------------------------------------------+
- | host | user | authentication_string |
- +-----------+---------------+-------------------------------------------+
- | localhost | root | *03CDB52F58482A55A757B2FDFA03F1A14EDA5D99 |
- | localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
- | localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
- | localhost | dgz | *FD571203974BA9AFE270FE62151AE967ECA5E0AA |
- +-----------+---------------+-------------------------------------------+
- 4 rows in set (0.00 sec)
MySQL数据库提供的权限列表:
刚创建的用户没有任何权限。需要给用户授权。
语法:
grant 权限列表 on 库.对象名 to '用户名'@'登陆位置' [identified by '密码']
说明:
- grant select on ...
- grant select, delete, create on ....
- grant all [privileges] on ... -- 表示赋予该用户在该对象上的所有权限
- [root@VM-8-2-centos ~]# mysql -udgz -p
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- +--------------------+
- 1 row in set (0.00 sec)
-
- # dgz用户看不到其他数据库
- # root端开通权限
- mysql> grant all on test3.* to 'dgz'@'localhost';
- Query OK, 0 rows affected (0.00 sec)
也可以只开通单个权限,如果只想让用户查看,那么可以只开通select权限
mysql> grant select test3.* to 'dgz'@'localhost';
特定用户现有查看权限
- mysql> show grants for 'dgz'@'localhost';
- +--------------------------------------------------------+
- | Grants for dgz@localhost |
- +--------------------------------------------------------+
- | GRANT USAGE ON *.* TO 'dgz'@'localhost' |
- | GRANT ALL PRIVILEGES ON `test3`.* TO 'dgz'@'localhost' |
- +--------------------------------------------------------+
- 2 rows in set (0.00 sec)
注意:如果发现赋权限后,没有生效,执行如下指令:
flush privileges;
语法:
revoke 权限列表 on 库.对象名 from '用户名'@'登陆位置';
示例:
- mysql> revoke all on test3.* from 'dgz'@'localhost';
- Query OK, 0 rows affected (0.00 sec)
-
- mysql> show grants for 'dgz'@'localhost';
- +-----------------------------------------+
- | Grants for dgz@localhost |
- +-----------------------------------------+
- | GRANT USAGE ON *.* TO 'dgz'@'localhost' |
- +-----------------------------------------+
- 1 row in set (0.00 sec)
用户dgz查看不到test3库了
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- +--------------------+
- 1 row in set (0.00 sec)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。