MySQL数据库的权限配置是确保数据库安全的重要步骤。通过配置适当的权限,您可以控制哪些用户可以访问数据库、执行特定操作等。以下是一些常见的MySQL数据库权限配置步骤:
1.创建用户
#创建用户,仅本机可登录
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
#创建一个test用户,密码为test123,指定IP远程登录:
create user 'test'@'192.168.0.1' identified by 'test123'
#创建一个test用户,密码为test123,任何IP都可以远程登录:
create user 'test'@'%' identified by 'test123'
#修改指定用户密码,where条件请自行修改
update mysql.user set password=password('新密码') where User="test" and Host="localhost";
授权生效:flush privileges;
2.查看用户、连接权限
#查看指定用户和指定ip的权限
show grants for root@localhost;
show grants for 'root'@'192.168.0.1';
#查看root用户所有权限
select * from mysql.db where user='root'\G;
#查看所有用户
select * from user;
3.创建权限
#赋予root用户连接权限:不限IP、不限数据库、不限权限
grant all privileges on *.* to 'root'@'%' identified by '密码';
#赋予root连接权限:限IP、限zabbix数据库、限select,update,delete,create,drop权限
grant select,update,delete,create,drop privileges on zabbix.* to 'root'@'192.168.0.1' identified by '密码';
#授权test用户连接权限:限testDB数据库的user表
grant all privileges on testDB.user to 'test'@'%' identified by '密码';
#使授权生效
flush privileges;
4.修改权限
#已存在用户新增update权限,该用户仅可连接zabbix数据库
grant update on zabbix.* to root%192.168.0.1;
#已存在用户取消update权限,该用户仅可连接zabbix数据库
revoke update on zabbix.* from 'root'@'192.168.0.1';
#撤销已经赋予用户的所有权限
revoke all on *.* from 'root'@'192.168.0.1';
#重新赋予所有权限
grant all on *.* to 'root'@'192.168.0.1';
#授权生效
flush privileges;