环境:
Windows 10
MySQL版本:5.7.17
创建用户
Grant语句
语法:
grant priv_type on database.table to 'user' @ 'host' [identified BY [password] 'password'];
priv_type:表示新用户的权限.
database.table:表示新用户可操作数据库及数据表.
user:新用户名.
host:新用户可登陆的主机.
password:关键字,若密码为普通字符串则不用指定.
identified by:关键字,用于设置用户密码.
password:新用户的密码.
实例:
grant select,insert,update on id.* to 'xiahan'@'%' identified by 'net123!';
说明:创建一个用户名为xiahan的用户,设置密码为net123!,该用户可以在任意主机登陆进行id数据库的查询,插入,更新操作。
Create语句
语法:
create user 'user1'@'host' [identified by [password] 'password'];
实例:
create user 'xiahan'@'localhost' identified by 'net123!';
说明:创建一个名为xiahan的用户仅限于本地登录,密码为net123!.
Insert语句
语法:
insert into mysql.user(host,user,authentication_string) vlues('host','user',password('password'));
注:
1.在新版本的mysql中,user表中password字段更名为authentication_string,若为老版本mysql则将authentication_string修改为password即可.
2.因为只设置这三个字段的值,故其它字段取值皆为默认值.如果除这3个字段以外的某个字段没有设置默认值的同时又非空,那么这个语句将不能执行,需要将没有默认值的字段设置值.通常ssl_cipher,x509_issuer,x509_subject这3个字段没有默认值,因此这3个字段必须设置初始值.
实例:
insert into mysql.user(host,user,authentication_string,ssl_cipher,x509_issuer,x509_subject) values ("localhsot",'xiahan',password('net123!'),'','','');
刷新权限
flush privileges;
没事儿刷新一下
删除用户
Drop user语句
drop user 'user'@'host';
实例:
drop user 'xiahan'@'host';
说明:删除xiahan用户.
Delete语句
语法:
delete from mysql.user where host='host' and user='user';
实例:
delete from mysql.user where host='localhost' and user='xiahan';
root用户修改自己的密码
在终端:
格式:
C:\Users\Administrator>mysqladmin -uroot -pold_pass password "new_pass"
实例:
C:\Users\Administrator>mysqladmin -uroot -pnet123!! password "xiahan"
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
说明:将旧密码net123!!更改为新密码xiahan,其中password为关键字.
Update语句更新user表
语法:
update mysql.user set authentication_string=password("new_pass");
实例:
update mysql.user set authentication_string=password("net123!");
说明:
1.旧版本需要将authentication_string字段改为password.
2.使用完需要刷新权限.
root用户修改普通用户密码
Set语句
语法:
set password for 'user'@'host'=password('new_pass');
实例:
set password for 'xiahan'@'localhost'=password('net123!');
Update语句更新user表
语法:
update mysql.user set authentication_string=password("new_pass");
实例:
update mysql.user set authentication_string=password("net123!");
Grant语句
语法:
grant priv_type on database.table to user [identified by [password] 'new_pass'];
实例:
grant select,update,insert on *.* to 'xiahan'@'host' identified by 'net123!';
普通用户修改密码
Set语句
语法:
set password=password('new_pass');
实例:
set password=password('net123!');
说明:需退出重新登陆
root用户忘记密码解决方法(Windows,Linux通用方法)
Windows下
1.找到其mysql配置路径,默认路径为C:\AppServ\MySQL\my.ini.
2.在其[mysqld]下添加:–skip-grant-tables.
[mysqld]
--skip-grant-tables
3.重启服务.
4.mysql -u root -p登陆,不需要输密码直接回车.
5.使用上面root用户修改自己的密码中update语句
.
Linux下
1.找到配置文件my.cnf.默认路径为/etc/my.cnf.
2.同Windows一样.
注:
1.Linux下使用命令/etc/init.d/mysqld restart
或service mysqld restart
重启MySQL服务.
2.仅能使用update语句
更新密码.