mysql5.7用户管理:添加用户、授权、撤权、修改密码

前言

mysql5.7版本中用户管理与以前版本略有不同,在此记录,以备忘

登陆

[root@ver ~]# mysql -h 127.0.0.1 -P 3316 -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.17 Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

参数说明:
-h: 指定数据库IP地址;
-P: 指定端口,默认的3306时,可以忽略;
-u: 指定登陆用户名;
-p: 使用密码密码(小写,注意与指定端口的大写P区分);

注意:5.7版本不再支持 mysql -u root -ppassword 形式登陆

指定操作数据库

mysql> show databases;	# 查看所有数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| fhgk               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

mysql> use mysql	# 指定当前操作的数据库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql>

创建数据库

# utf8mb4与utf8的区别是其支持Emoji表情
create database sina default character set utf8mb4 collate utf8mb4_unicode_ci;

创建用户

# 创建用户
mysql> CREATE USER 'username'@'host' IDENTIFIED BY 'password';

# 删除用户
mysql> DROP USER 'username'@'host';

host参数说明:
% 匹配所有主机
localhost localhost不会被解析成IP地址,直接通过UNIXsocket连接
127.0.0.1 会通过TCP/IP协议连接,并且只能在本机访问;
::1 ::1就是兼容支持ipv6的,表示同ipv4的127.0.0.1

此时还没有授权,只能登陆,无法做其余操作

用户授权

# 用户授权
mysql> grant all privileges ON `databasename`.* TO 'username'@'127.0.0.1';

# 创建用户的同时授权
mysql> grant all privileges on databasename.* to 'username'@'host' identified by '1234';

# 授权刷新
mysql> flush privileges;

# 查看用户拥有权限
mysql> show grants for dev@'%';
+----------------------------------------------------------------------+
| Grants for dev@%                                                     |
+----------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'dev'@'%'                                      |
| GRANT SELECT, INSERT, UPDATE, DELETE, ALTER ON `fhgk`.* TO 'dev'@'%' |
+----------------------------------------------------------------------+
2 rows in set (0.00 sec)

# 撤消用户授权,撤消要求各参数与授权时使用的一致,可以相查看授权再撤消
mysql> revoke privileges ON databasename.* FROM 'username'@'host';

privileges参数说明:
all privileges: 所有权限;
select: 查询;
insert: 新增记录;
update: 更新记录;
delete: 删除记录;
create: 创建表;
drop: 删除表;
alter: 修改表结构;
index: 索引相关权限;
execute: 执行存储过程与call函数
references: 外键相关;
create temporary tables:创建临时表;
lock tables 锁表;
create view 创建视图;
show view 查看视图结构;
create routine
alter routine:
event:
trigger: 触发器相关;

*databasename.参数说明:
此处可以针对具体的某个库,如:【zjims.】;
也可以针对具体库中的某个对象,如:【zjims.t_user】;
还可以针对所有数据库,如:【
.*】;

修改密码

# 修改自己的密码
mysql> set password=password('newpassword');

# 修改别人密码——方法1
mysql> set password for 'username'@'host' = password('newpassword');

# 修改别人密码——方法2: 适用mysql5.7以前的版本,5.7以后的版本中mysql.user表没有了password字段
mysql> update mysq.user set password=password('newpassword') where user='user' and host='host';
# 修改别人密码——方法3:适用mysql5.7
mysql> update mysql.user set authentication_string=password('newpassword') where user='root';

# 修改别人密码——方法4
mysql> alter user 'test'@'%' identified by 'newpassword';

重置管理员密码

  1. 停止mysql服务:service mysqld stop 或 ./mysql.server stop;
  2. 以不检查权限方式启动mysql:./mysqld --skip-grant-tables --user=mysql &;
  3. 以空密码方式登陆:mysql -h 127.0.0.1 -P 3306 -u root;
  4. mysql5.7以前版本——修改root密码:update mysq.user set password=password(‘newpassword’) where user=‘root’;
  5. mysql5.7以后版本——修改root密码:update mysql.user set authentication_string=password(‘newpassword’) where user=‘root’;(只能用此种update方法修改)
  6. 刷新权限:flush privileges;
  7. 关闭mysql:shutdown;
  8. 以正常方式启动mysql: service mysqld start 或 ./mysql.server start;

参考资料

  1. http://www.cnblogs.com/fslnet/p/3143344.html
  2. http://www.cnblogs.com/xujishou/p/6306765.html
  3. http://www.cnblogs.com/4php/p/4113593.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值