mysql用户权限管理

-- 查询数据库用户
select user

-- 使用ip链接数据库(%匹配所有)
mysql -h192.168.1.% -ulisi -pxxxx;

-- 修改host域,使IP可以链接上
update user set host='192.168.1.%' where user='root';
flush privileges;

-- 修改用户密码,password()对密码加密
update user set password=password('11111') where xxxx;
flush privileges;

-- 查看用户以及权限
select * from user where user='lisi' \G;

-- 查看库表的级别和host链接状态
select * from db \G;
select * from table \G;

-- 新增用户并赋予全局权限
-- 常用的权限all,create,drop,insert,delete,update,select
-- 比如grant all on *.* to lisi@'192.168.1.%' identfied by'1111';
grant [权限1,权限2,权限3.。] on *.* to user@'host' identfied by'password';

-- 回收全局权限
-- 比如revoke all on *.* from lisi@'192.168.1.%';
revoke all on *.* from user@'host' identfied by'password';

-- 针对库做授权
-- 比如grant all on test.* to lisi@'192.168.1.%';
grant all on tbName.* to lisi@'192.168.1.%';
revoke all on tbName.* to lisi@'192.168.1.%';

-- 针对表做授权
-- 比如grant insert,update,select on test.goods to lisi@'192.168.1.%';
grant insert,update,select on dbName.tbName to lisi@'192.168.1.%';
revoke all on dbName.tbName to lisi@'192.168.1.%';

-- MYSQL添加远程用户或允许远程访问三种方法
-- 添加远程用户admin密码为password
GRANT ALL PRIVILEGES ON *.* TO admin@localhost IDENTIFIED BY password WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO admin@\"%\" IDENTIFIED BY password   WITH GRANT OPTION;

-- 用root用户登陆,然后:
grant all privileges on *.* to userName @"%" identified by "密码";
flush privileges;   * 刷新刚才的内容*

-- 格式:grant 权限 on 数据库教程名.表名 to 用户@登录主机 identified by "用户密码";@ 后面是访问mysql的客户端ip地址(或是 主机名)
-- % 代表任意的客户端,如果填写 localhost 为本地访问(那么  用户就不能远程访问该mysql数据库了,在服务器上localhost是指服务器主机)。
-- 同时也可以为现有的用户设置是否具有远程访问权限。如下:
use mysql;
update db set host = '%' where user = 'userName'; (如果写成 host=localhost 那此用户就不具有远程访问权限)
flush privileges;
grant all privileges on *.* to 'userName'@'%' identified by 'password' with grant option;

-- 使用grant语句添加:
-- 首先在数据库本机上用root用户登录mysql(我是用远程控制linux服务器,相当于在服务器本机登录mysql了),然后输入:
grant all privileges on *.* to admin@localhost identified by 'password' with grant option;

-- 添加一个用户admin并授权可从任何其它主机(localhost)发起的访问(通配符%)。使用这一条语句即可。
grant all privileges on *.* to admin@"%" identified by 'password' with grant option;

-- 使用insert语句:
-- 用户信息可在mysql数据库中的users表中查看,这里不在介绍了就。数清y的个数哦。
-- 好了,使用admin帐号连接试试看,我是屡试屡成功哦,呵呵!
insert into user values('%','admin',password('password'), 'y','y','y','y','y','y','y','y','y','y','y','y','y','y');

-- 添加远程用户admin密码为password
grant all privileges on *.* to admin@localhost identified by 'password' with grant option;
grant all privileges on *.* to admin@"%" identified by 'password' with grant option;

由于项目开发的要求数据库的设计不得不用远程模式。但是数据库的远程设置并没那么简单,该项目的数据库是mysql5.0。刚开始以为只要装了数据库服务器就可以进行远程链接了,但是mysql的设置是为了用户的安全,系统默认的设置是不允许远程用户连接,只能本地的用户连接。只要我们设置下系统的管理员用户的host这一项的值就可以给远程的用户访问了。数据库权限管理极其重要,特别是主从数据库配置以及读写分离涉及到赋予表级权限;

posted @ 2017-12-07 14:05  TangYJun  阅读(7834)  评论(0编辑  收藏  举报