
MySQL 常用管理语句速查:以 utf8mb4 建库、创建/删除用户、修改密码、分配与查看权限,以及本地与远程登录的区别。
新建数据库
create database 数据库名 default character set utf8mb4 collate utf8mb4_unicode_ci; -- 以 utf8mb4 建立
create database if not exists 数据库名 default charset utf8 collate utf8_general_ci; -- 不存在则以 utf8 建立创建 / 删除用户
create user '用户名'@'localhost' identified by '密码';
drop user 用户名@localhost;
drop user 用户名@'%'; -- 允许任意主机登录的用户这样删除更改密码
-- 方法1,实时生效:
set password for 用户名 = password('1122');
-- 方法2,需刷新:
update mysql.user set password = password('1234') where user = '用户名';
flush privileges;权限分配
grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码";
grant all privileges on 数据库名.* to 用户名@localhost identified by '密码';
flush privileges; -- 刷新系统权限表
show grants for 用户名; -- 查看用户权限注意:localhost 表示该用户只能本地登录;想远程登录则把 localhost 改为 %,表示任意电脑都可登录,也可指定某台机器。
