13
Linux上Mysql数据库 用户权限控制
source link: http://www.cnblogs.com/chenyanbin/p/14003024.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
导读
大家或许都听过程序员删库跑路,可想而知,如果对用户开放太多的数据库操作权限,操作不当,可能会造成意想不到的损失,通过本篇学习,可以熟练掌握mysql用户权限的控制。当然啦,数据被删掉,可以使用技术手段(binlog)恢复回去的,过几天更新~
Linux安装mysql
Mysql限制root用户ip地址登录
修改mysql库里边的user表: update mysql.user set host='localhost' where user='root'; 刷新权限: flush privileges;
修改密码
忘记密码
编辑my.cnf,并添加:skip-grant-tables
编辑my.cnf vim /usr/local/mysql/my.cnf ================== [client] default-character-set=utf8 [mysqld] character_set_server=utf8 datadir=/usr/local/mysql/data port = 3306 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES symbolic-links=0 max_connections=400 innodb_file_per_table=1 #表名大小写不明感,敏感为 lower_case_table_names=1 #跳过权限表,添加该命令 skip-grant-tables
重启mysql服务
service mysql restart
登录mysql,并修改密码
mysql -uroot -p 直接回车不用输入密码!!!!!!!! 然后使用下面3种修改密码之一即可
修改密码
方式一
连到数据库中修改密码
格式: set password for 用户名@ip =password('密码'); alter user '用户'@'ip' identified by '密码'; 刷新权限: flush privileges; 示例 set password for [email protected] = password('root');
方式二
格式: ./mysqladmin -u用户 -p旧密码 password 新密码
方式三
格式: select * from mysql.user where user='root' \G; update mysql.user set authentication_string=password('123456') where user='root'; 刷新权限: flush privileges;
创建用户并限制ip登录
创建用户
语法: create user '用户名'@'ip' identified by '密码'; 用户名:创建的账户 ip:指定该用户在哪个主机上可以登录,如果是本地用户可用localhost,如果想让该用户可以在任意远程主机登录,可以使用通配符:% 密码:该用户的登录密码,密码可以为空,如果为空,则该用户可以不需要密码登录服务器
查看权限: show grants for '用户名'@'ip';
查看用户权限: select * from mysql.user where user='用户' \G;
mysql> select * from mysql.user where user='ybchen' \G; *************************** 1. row *************************** Host: % User: ybchen Select_priv: N Insert_priv: N Update_priv: N Delete_priv: N Create_priv: N Drop_priv: N Reload_priv: N Shutdown_priv: N Process_priv: N File_priv: N Grant_priv: N References_priv: N Index_priv: N Alter_priv: N Show_db_priv: N Super_priv: N Create_tmp_table_priv: N Lock_tables_priv: N Execute_priv: N Repl_slave_priv: N Repl_client_priv: N Create_view_priv: N Show_view_priv: N Create_routine_priv: N Alter_routine_priv: N Create_user_priv: N Event_priv: N Trigger_priv: N Create_tablespace_priv: N ssl_type: ssl_cipher: x509_issuer: x509_subject: max_questions: 0 max_updates: 0 max_connections: 0 max_user_connections: 0 plugin: mysql_native_password authentication_string: *D2EDE838B7E0B05A2C599D3E1F5821D0ADC4F26E password_expired: N password_last_changed: 2020-11-19 23:26:12 password_lifetime: NULL account_locked: N 1 row in set (0.00 sec)
创建一个用户,并指定密码,限制网段登录
适用于只能在公司内网环境下开发程序,外网登录不上,限制:172.20网段登录
create user 'ybchen'@'172.20.%.%' identified by '123456';
删除用户
语法: drop user '用户'@'ip';
语法: delete from mysql.user where user='用户';
授予用户库表权限
授权
语法: grant 权限1,权限2... on 数据库 to '用户' grant 权限1,权限2... on 数据库 to '用户'@'ip' identified by '密码'
对cyb用户授予所有库所有表所有权限
all privileges:代表所有权限 *.*:代表所有库所有表 grant all privileges on *.* to 'cyb';
授予cyb2用户,db1库所有权限
语法: 创建用户 create user 'cyb2'@'%' identified by 'cyb2'; 授予cyb2用户,db1库的所有权限 grant all privileges on db1.* to 'cyb2'@'%' identified by 'cyb2'; 刷新权限 flush privileges;
授予cyb3用户,db1库student的查询权限(只有增删改查权限,没有新建表、删除表权限)
语法: 创建用户 create user 'cyb3'@'%' identified by 'cyb3'; 授予cyb3用户,db1库student,增删改查权限 grant insert,select,update,delete on db1.student to 'cyb3'@'%' identified by 'cyb3'; 刷新权限 flush privileges;
移除权限
语法: revoke 权限1,权限2... on 数据库对象 from '用户'@'ip'
revoke all privileges on *.* from 'cyb'@'%'; flush privileges;
注意
这种移除权限,但是用户还是可以正常登陆的,如果让用户登陆都登不上,需要将mysql.user的记录删除!
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK