[转帖] mysql 用户 权限 密码等操作记录

时间:2023-03-09 09:10:52
[转帖] mysql 用户 权限 密码等操作记录

前言

From :https://blog.****.net/yu12377/article/details/78214336

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

登陆

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

Copyright (c) , , 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区分);

指定操作数据库

mysql> show databases;  # 查看所有数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| fhgk               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
 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>

创建用户

# 创建用户
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 privileges ON databasename.* TO 'username'@'host';

# 创建用户的同时授权
mysql> grant all privileges on databasename.* to ';

# 授权刷新
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'@'%' |
+----------------------------------------------------------------------+
 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以前的版本,.7以后的版本中mysql.user表没有了password字段
mysql> update mysq.user set password=password('newpassword') where user='user' and host='host';
# 修改别人密码——方法3:适用mysql5.
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