1、首先登陆到root用户: mysql -u root -p
2、创建用户: create user '用户名'@'host' identified with mysql_native_password by '密码';
(注:host:指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost,如果想让该用户可以从任意远程主机登陆,可以使用通配符%)
(密码规则:1、mysql8.0以上密码策略限制必须要大小写加数字特殊符号。2、在MySQL 8.04前,MySQL的密码认证插件是“mysql_native_password”,而现在使用的是“caching_sha2_password”。此处本人指定用mysql_native_password是为了用客户端工具navcat连接)
3、创建库: create database 库名;
4、授权用户拥有数据库的权限:grant select,insert,update,delete,create,drop,alter,references on 数据库名.表名 to '用户名'@'host';
(注:如果要授予该用户对所有数据库和表的相应操作权限则可用*表示,如*.*)
5、刷新:flush privileges;
附加:
删除用户及权限:drop user '用户名'@'host';(drop user 用户名@'%';drop user 用户名@ localhost;)
删除数据库:drop database 数据库名;
修改密码:alert user '用户名'@'host' identified by '密码'; flush privileges;
列出所有数据库:show databases;
切换数据库:use '数据库名';
列出所有表:show tables;
显示数据表结构:describe 表名;
删除数据库:drop database 数据库名;
删除表:drop table 表名