mysql新建用户与 添加、删除远程访问权限(命令记录)

tech2026-09-02  1

目录

新建用户

添加远程访问权限

关闭远程访问


新建用户

查看用户命令 select user, host from mysql.user; 创建用户 CREATE USER 'username'@'host' IDENTIFIED BY 'password'; username:用户名;host:指定在哪个主机上可以登录,本机可用localhost,%通配所有远程主机;password:用户登录密码;

添加远程访问权限

mysql数据库设置用户远程连接权限 grant all privileges on *.* to 'root'@'%' identified by 'root4xml004a' with grant option; 创建数据库 CREATE DATABASE 库名; 授予用户在本地服务器对该数据库的全部权限 grant all privileges on dbname.* to zhrt@localhost identified by '123456'; 设置用户testuser,只拥有【查询】操作权限; grant select on *.* to testuser@localhost identified by "123456" WITH GRANT OPTION; 授予用户通过外网IP对于该数据库的全部权限 grant all privileges on dbname.* to 'zhrt'@'%' identified by '123456'; 刷新权限 FLUSH PRIVILEGES;

关闭远程访问

如已有root用户使用localhost可设置为127.0.0.1

update user set host = "localhost" where user = "root" and host= "%"; 或者 delete from user where user="root" and host="%" ; 退出mysql exit; 关闭/启动/重启 mysql service mysql stop service mysql start service mysql restart

查看当前用户: SELECT CURRENT_USER();

查看某个用户的权限:show grants for 'root'@'%'; 

撤销用户权限:

REVOKE privilege ON databasename.tablename FROM 'username'@'host'; privileges:用户的操作权限,如SELECT,INSERT,UPDATE等,如果要授予所的权限则使用ALL databasename:数据库名 tablename:表名,如果要授予该用户对所有数据库和表的相应操作权限则可用*表示,如*.*

设置完mysql需要重启服务

最新回复(0)