mysql创建删除用户以及添加权限
2018-06-26 本文已影响2人
陆_志东
注:以下数据库用户以及权限操作,只有再刷新权限后才会生效.下面有刷新权限方法
查看所有数据库用户及对应端口和密码
select user,host,password from mysql.user;
mysql创建新的用户
creater user 用户名 identified by "密码";
例: creater user xiaodong identified by "mysql"
新创建的用户,没有任何权限
给用户分配权限
语法: grant 权限 on 数据库.数据表 to "用户" @ 主机名
例:给xiaodong分配所有的数据库数据表的所有权限,并且允许所有ip访问
grant all on *.* to "xiaodong"@"%" # 注意%要带引号
例: 给xiaodong设置test数据库的students表读权限,访问ip为本地
grant select on test.students to "xiaodong"@127.0.0.1
创建用户和分配权限放一起执行
grant select on test.students to "xiaodong" @192.168.1.107 identified by "mysql";
# 创建用户xiaodong 登陆密码为mysql, 并分配test数据库students数据表的读权限,允许访问的ip为192.168.1.107
收回数据库权限
revoke 权限 on 数据库.数据表 from "用户"@主机名
删除用户
drop user "用户名"@"%"
drop user "用户名"@localhost
修改指定用户的密码
update mysql.user set password=password("新密码") where User = "test" and Host = "localhost"
刷新数据库权限
方法一:使用命令 FLUSH PRIVILEGES
# flush privileges
方法二:重启mysql服务
windows:
net stop mysql
net start mysql
linux:
/etc/init.d/mysqld start
/etc/init.d/mysqld stop
/etc/init.d/mysqld restart