程序中可能需要保留root用户的权限,进行数据的增删改查,但是平时程序员连接mysql数据库的时候使用root权限容易造成误操作,给mysql造成严重的损失。
于是需要单独创建程序员账户供其在平时工作中查询数据库。
mysql -h host -uroot -p
mysql>
1.创建一个管理员用户,并赋予所有权利&&赋予其他用户的权利
GRANT ALL PRIVILEGES ON *.* TO server@'localhost' IDENTIFIED BY 'test123456' WITH GRANT OPTION;
FLUSH PRIVILEGES;
2.创建一个web程序管理员账户,并赋予相应的权限&&仅允许该用户从某个网段访问数据库
GRANT USAGE,SELECT,INSERT,UPDATE,DELETE,SHOW VIEW,CREATE TEMPORARY TABLES,EXECUTE ON `cad`.* TO webuser@'10.124.156.%' IDENTIFIED BY 'test123456';
FLUSH PRIVILEGES;
3.创建一个普通用户,仅赋予其查询的权利
GRANT USAGE,SELECT ON `cad`.* TO public@'10.124.%.%' IDENTIFIED BY 'public123456';
FLUSH PRIVILEGES;
4.查看权限:
SHOW GRANTS FOR 'webuser'@'10.124.156.%';
5.回收权限:
REVOKE DELETE ON cad.* FROM 'webuser'@'10.124.%.%';
6.删除用户
DROP USER 'webuser'@'10.124.%.%';
创建用户并赋予权限:
GRANT USAGE,SELECT,CREATE,INSERT,UPDATE,SHOW VIEW,EXECUTE ON *.* TO server@'10.124.%.%' IDENTIFIED BY '123456';
FLUSH PRIVILEGES;
回收权限并删除用户:
SHOW GRANTS FOR 'server'@'10.124.%.%';
REVOKE ALL ON *.* FROM 'server'@'10.124.%.%';
DELETE user FROM mysql.user WHERE user='server' and host='10.124.%.%';