在登录连接mysql的时候,通常使用root用户连接,但是如果我们想单独设置一个用户并且连接数据库后只能查看数据应该怎么做呢,这里就详细说明一下怎样给用户授权并且查看授权用户所对应的权限.
CREATE USER '用户名'@'来源地址' [IDENTIFIED BY [PASSWORD] '密码'];
'用户名':指定将创建的用户名.
'来源地址':指定新创建的用户可在哪些主机上登录,可使用IP地址、网段、主机名的形式,本地用户可用localhost,允许任意主机登录
可用通配符%
'密码':若使用明文密码,直接输入'密码',插入到数据库时由Mysql自动加密;
若使用加密密码,需要先使用SELECT PASSWORD('密码');获取密文,再在语句中添PASSWORD '密文';
若省略“IDENTIFIED BY"部分,则用户的密码将为空(不建议使用)
CREATE USER 'user1'@'localhost' IDENTIFIED BY '123456';
SELECT PASSWORD('abc123');
CREATE USER 'user2'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9';
创建后的用户保存在mysql 数据库的user表里
USE mysql;
SELECT User,authentication_string,Host from user;
例:
mysql> select user,authentication_string,host from user;
+-----------+-------------------------------------------+--------------+
| user | authentication_string | host |
+-----------+-------------------------------------------+--------------+
| root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | localhost |
| mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost |
| zhangsan | *6691484EA6B50DDDE1926A220DA01FA9E575C18A | localhost |
| lisi | *6691484EA6B50DDDE1926A220DA01FA9E575C18A | 192.168.3.12 |
| lisi | *6691484EA6B50DDDE1926A220DA01FA9E575C18A | 192.168.3.2 |
| lisi | *6691484EA6B50DDDE1926A220DA01FA9E575C18A | 192.168.3.1 |
| lz | *6691484EA6B50DDDE1926A220DA01FA9E575C18A | 192.168.3.% |
+-----------+-------------------------------------------+--------------+
7 rows in set (0.00 sec)
RENAME USER 'zhangsan'@'localhost' TO 'lisi'@'localhost';
例:
mysql> rename user 'lisi'@'192.168.3.12' to 'lisi'@'localhost';
Query OK, 0 rows affected (0.04 sec)
DROP USER 'lisi'@'localhost' ;
例:
mysql> drop user 'lisi'@'192.168.3.2';
Query OK, 0 rows affected (0.01 sec)
SET PASSWORD = PASSWORD('abc123');
SET PASSWORD FOR 'user1'@'localhost' = PASSWORD('abc123T');
例:
mysql> set password for 'lisi'@'192.168.3.1' = password('abc123');
Query OK, 0 rows affected, 1 warning (0.00 sec)
修改/etc/my.cnf 配置文件,免密登陆mysql
vim /etc/my.cnf
[mysqld]
skip-grant-tables #添加,使登录mysql不使用授权表
systemctl restart mysqld
mysql #直接登录
然后使用SQL语句修改密码
UPDATE mysql.user SET AUTHENTICATION_STRING = PASSWORD('abc123') where user='root';
FLUSH PRIVILEGES;
quit
mysql -u root -pabc123
PS:最后再把/etc/my.cnf 配置文件里的skip-grant-tables 删除,并重启mysql服务
#########格式##########
grant 权限列表 on 数据库名.表名 to '用户名'@'来源地址' identified by'密码'
#含义:
'用户名':指定将创建的用户名.
'来源地址':指定新创建的用户可在哪些主机上登录,可使用IP地址、网段、主机名的形式,本地用户可用localhost,允许任意主机登录
可用通配符%
'密码':若使用明文密码,直接输入'密码',插入到数据库时由Mysql自动加密;
若使用加密密码,需要先使用SELECT PASSWORD('密码');获取密文,再在语句中添PASSWORD '密文';
若省略“IDENTIFIED BY"部分,则用户的密码将为空(不建议使用)
例:
#授权zhangsan用户对zone.dsj在本地有查看的权限
mysql> grant select on zone.dsj to 'zhangsan'@'localhost' identified by 'abc123';
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> exit //退出数据库
Bye
[root@c7-2 /]#mysql -uzhangsan -pabc123; //使用zhangsan用户登陆
mysql> show databases; //查看库
+--------------------+
| Database |
+--------------------+
| information_schema |
| zone |
+--------------------+
2 rows in set (0.00 sec)
mysql> use zone; //切换到zone 库
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> show tables; //查看表
+----------------+
| Tables_in_zone |
+----------------+
| dsj |
+----------------+
1 row in set (0.00 sec)
mysql> select * from dsj; //查询数据记录
+----+------+-------+---------+
| id | name | score | passwd |
+----+------+-------+---------+
| 1 | lz | 80 | 12345 |
| 2 | gl | 81 | 12345 |
| 3 | cpw | 82 | 12345 |
| 4 | zc | 84 | 23112 |
| 5 | jzy | 85 | 536464 |
| 6 | xf | 86 | 4537856 |
+----+------+-------+---------+
6 rows in set (0.00 sec)
#尝试删除失败,因为只给了查询的权限
mysql> delete from dsj;
ERROR 1142 (42000): DELETE command denied to user 'zhangsan'@'localhost' for table 'dsj'
######授权zhangsan用户对zone数据库的所有权限
mysql> grant all privileges on zone.* to 'zhangsan'@'localhost' identified by'abc123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
##########授予远程连接权限################
#允许lz用户在3.段远程连接数据库,并对数据库拥有所有权限
mysql> grant all privileges on *.* to 'lz'@'192.168.3.%' identified by'abc123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
授权用户权限是all privilege。all privilege权限如下:
insert(插入数据)
select (查询数据)
update (更新表的数据)
delete(删除表中数据)
create (创建库,表)
drop(删除库,表)
refernces
index(建立索引)
alter(更改表属性)
create temp orary tableslock tables (锁表)
execute
create view (创建视图)
show view(显示视图)
create routine(创建存储过程)
alter routine(修改存储过程)
event(事件)
trigger on(创建触发器)
#######格式########
SHOW GRANTS FOR 用户名@来源地址;
###例:
mysql> show grants for 'lz'@'192.168.3.%';
+--------------------------------------------------------+
| Grants for lz@192.168.3.% |
+--------------------------------------------------------+
| GRANT USAGE ON *.* TO 'lz'@'192.168.3.%' |
| GRANT ALL PRIVILEGES ON "zone".* TO 'lz'@'192.168.3.%' |
+--------------------------------------------------------+
2 rows in set (0.00 sec)
#############格式################
REVOKE 权限列表 ON 数据库名.表名 FROM 用户名@来源地址;
#例:
mysql> revoke all privileges on "zone".* from 'lz'@'192.168.3.%';
Query OK, 0 rows affected (0.00 sec)
#再次查看
mysql> show grants for 'lz'@'192.168.3.%';
+------------------------------------------+
| Grants for lz@192.168.3.% |
+------------------------------------------+
| GRANT USAGE ON *.* TO 'lz'@'192.168.3.%' |
+------------------------------------------+
1 row in set (0.00 sec)
用户权限管理主要有以下作用: