前言
From :https://blog.csdn.net/yu12377/article/details/78214336
mysql5.7版本中用户管理与以前版本略有不同,在此记录,以备忘
登陆
[[email protected] ~]# 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区分);
指定操作数据库
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>
创建用户
# 创建用户 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 ‘username‘@‘host‘ identified by ‘1234‘; # 授权刷新 mysql> flush privileges; # 查看用户拥有权限 mysql> show grants for [email protected]‘%‘; +----------------------------------------------------------------------+ | Grants for [email protected]% | +----------------------------------------------------------------------+ | 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‘;
重置管理员密码
- 停止mysql服务:service mysqld stop 或 ./mysql.server stop;
- 以不检查权限方式启动mysql:./mysqld –skip-grant-tables –user=mysql &;
- 以空密码方式登陆:mysql -h 127.0.0.1 -P 3306 -u root;
- mysql5.7以前版本——修改root密码:update mysq.user set password=password(‘newpassword’) where user=’root’;
- mysql5.7以后版本——修改root密码:update mysql.user set authentication_string=password(‘newpassword’) where user=’root’;(只能用此种update方法修改)
- 刷新权限:flush privileges;
- 关闭mysql:shutdown;
- 以正常方式启动mysql: service mysqld start 或 ./mysql.server start;
参考资料
- http://www.cnblogs.com/fslnet/p/3143344.html
- http://www.cnblogs.com/xujishou/p/6306765.html
- http://www.cnblogs.com/4php/p/4113593.html
原文地址:https://www.cnblogs.com/jinanxiaolaohu/p/9329525.html
时间: 2024-11-08 10:29:27