hanhuibk

MySQL-用户和权限管理

用户
mysql用户信息存在哪张表中
mysql.user

查看用户的信息
MariaDB [(none)]> select user,host,password from mysql.user;

删除用户
drop user 用户名@主机;
MariaDB [(none)]> drop user root@\'ula.example.com\';
MariaDB [(none)]> delete from mysql.user where user!=\'root\';
Query OK, 6 rows affected (0.00 sec)


创建用户
创建无密码无限制登陆ip的账户
MariaDB [(none)]> create user \'xiaomi\';

创建本地用户并设置密码
MariaDB [(none)]> create user xiao@localhost identified by \'xiao\';
Query OK, 0 rows affected (0.00 sec)

创建可以远程登陆的用户
服务器ip地址:192.168.1.1
1)创建远程用户
语法:create user 用户名@可以登陆的主机ip identified by \'密码子符串\';
MariaDB [(none)]> create user remote@192.168.1.71 identified by \'88\';
Query OK, 0 rows affected (0.00 sec)

可以登陆的主机ip:客户端的ip地址
192.168.1.71 ip
\'192.168.1.%\' 192.168.1网段
\'%\' 表示用户可以从任何地址访问
\'%.uplooking.com\'

你提供一个可以远程登陆的用户叫apple 允许192.168.1网段的主机使用apple用户访问你的mysql数据库
MariaDB [(none)]> create user apple@\'192.168.1.%\' identified by \'apple\';

客户端登陆服务器:
语法:mysql -u 用户名 -h 服务器的ip地址 -p密码
mysql -u remote -h 192.168.1.1 -p88 //能够远程登陆成功,那么说明远程用户创建成功

权限
全局层级
全局权限适用于一个给定服务器中的所有数据库。这些权限存储在mysql.user表中。

数据库层级
数据库权限适用于一个给定数据库中的所有目标。这些权限存储在mysql.db和mysql.host表中。

表层级
表权限适用于一个给定的表中的所有列。这些权限存储在mysql.tables_priv表中。

列层级
列权限适用于一个给定的表中的单一列。这些权限存储在mysql.colummns_priv表中。

授权 grant
语法:grant 权限列表 on 库名.表名 to 用户@主机 identified by \'密码字符串\';

权限列表
alter
create
delete
drop
index
insert
select
update
file 读或写服务器上的文件
all 所有权限
show databases

1)授全部权限
MariaDB [(none)]> grant all on *.* to user1@192.168.1.71 identified by \'test\';
MariaDB [(none)]> select * from mysql.user\G;
MariaDB [(none)]> flush privileges; //刷新权限

2)授部分权限
1.zhangqi 查询 插入 更新 数据库test中所有表数据的权限。
MariaDB [(none)]> grant select,insert,update on test.* to zhangqi@\'192.168.1.%\' identified by \'zhangqi\';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> select * from mysql.db where user=\'zhangqi\'\G;

2.给数据库开发人员xiaoli 创建表 索引 权限
MariaDB [(none)]> grant create,index on *.* to xiaoli@\'192.168.1.71\' identified by \'xiaoli\';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> select * from mysql.user where user=\'xiaoli\'\G;

3.给root_test用户查看mysql.user表的权限
MariaDB [(none)]> grant select on mysql.user to root_test identified by \'root_test\';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> select * from mysql.tables_priv where user=\'root_test\'\G;

4.给root_test1用户只可以查看mysql.user user和host列的权限
MariaDB [(none)]> grant select(user,host) on mysql.user to root_test1 identified by \'root_test1\';

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

查看权限
查看当前用户自己的权限
MariaDB [(none)]> show grants;

查看其他用用户权限
MariaDB [(none)]> show grants for root_test;

授权经验原则
权限控制主要出于安全考虑,因此需要遵循一个几个经验原则
1.只授予能满足需要的最小权限,防止用户干坏事。比如说用户只是需要查询,那就只给select权限就可以了。不要给用户赋予update,insert或者delete。
2.创建用户的时候限制用户的登陆主机
3.初始化数据库的时候删除没有密码的用户。
4.为每个用户设置满足密码复杂度的密码
5.定期清理不需要的用户。回收权限或者删除用户

回收权限
语法:
revoke 权限 on 库名.表名 from 用户@主机;
MariaDB [(none)]> revoke all on *.* from root_test@\'%\'; //回收全部权限
MariaDB [(none)]> revoke insert on *.* from root_test@\'%\'; //回收部分权限

删除用户
MariaDB [(none)]> delete from mysql.user where user!=\'root\';

修改密码
1.使用mysqladmin命令
[root@ULA mysql]# mysqladmin -u root -p123 password \'456\'

2.使用set password命令
MariaDB [(none)]> set password for \'root\'@\'localhost\'=password(\'1\');

3.使用update直接编辑user表
MariaDB [(none)]> update mysql.user set password=password(\'2\') where user=\'root\' and host=\'localhost\';
MariaDB [(none)]> flush privileges;








分类:

技术点:

相关文章: