一、如何拒绝用户从某个精确ip访问数据库
假如在mysql.user表中存在用户'mydba'@'192.168.85.%',现在想拒绝此用户从某个精确ip访问数据库

# 创建精确ip用户,分配不同的密码
mysql> create user 'mydba'@'192.168.85.1' identified by 'otherpwd';
# 精确匹配,这个ip进来的密码为otherpwd,由于密码不匹配拒绝登录 mydba@192.168.85.132,3306 [(none)]> select user,host,authentication_string from mysql.user where user='mydba';
+-------+--------------+-------------------------------------------+
| user | host | authentication_string |
+-------+--------------+-------------------------------------------+
| mydba | 192.168.85.% | *A7E26519238B6EA2F943D5FAC3CD7812AD8F87E5 |
| mydba | 192.168.85.1 | *33D5FAF1A32909300D21AB5A38FA4F215D9FCB26 |
+-------+--------------+-------------------------------------------+
2 rows in set (0.00 sec) mydba@192.168.85.132,3306 [(none)]> select password('mysql5719'),password('otherpwd');
+-------------------------------------------+-------------------------------------------+
| password('mysql5719') | password('otherpwd') |
+-------------------------------------------+-------------------------------------------+
| *A7E26519238B6EA2F943D5FAC3CD7812AD8F87E5 | *33D5FAF1A32909300D21AB5A38FA4F215D9FCB26 |
+-------------------------------------------+-------------------------------------------+
1 row in set, 2 warnings (0.00 sec)

此时用户'mydba'@'192.168.85.1',即用户名mydba从192.168.85.1机器上访问,必须使用otherpwd才能登录
二、使用mysqladmin修改用户密码,当存在同名user(不同host)时,修改的是哪个用户的密码

mydba@192.168.85.132,3306 [(none)]> show master status;
+------------------+----------+--------------+------------------+----------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+----------------------------------------------+
| mysql-bin.000116 | 3175 | | | 8ab82362-9c37-11e7-a858-000c29c1025c:1-69371 |
+------------------+----------+--------------+------------------+----------------------------------------------+
1 row in set (0.00 sec) mydba@192.168.85.132,3306 [(none)]> exit
Bye
[root@ZST1 ~]# mysqladmin -h192.168.85.132 -P3306 -umydba -p password newpwd
Enter password:
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.
[root@ZST1 ~]#

此时修改的是哪个用户密码?'mydba'@'192.168.85.1'还是'mydba'@'192.168.85.%'?还是两者都修改了?
先说结论:看你从哪个客户端发起命令,仅修改与其匹配的current_user()对应的密码。mysqladmin中需要提供user+password以登录到MySQL中,在登录验证过程中MySQL已经对其进行精确匹配
2.1、current_user()为mydba@192.168.85.%,对应修改'mydba'@'192.168.85.%'的密码

# 前面的mysqladmin在ZST1(192.168.85.132)上执行,对应的current_user()为mydba@192.168.85.%
mydba@192.168.85.132,3306 [(none)]> select user,host,authentication_string from mysql.user where user='mydba';
+-------+--------------+-------------------------------------------+
| user | host | authentication_string |
+-------+--------------+-------------------------------------------+
| mydba | 192.168.85.% | *1FA85AA204CC12B39B20E8F1E839D11B3F9E6AA4 |
| mydba | 192.168.85.1 | *33D5FAF1A32909300D21AB5A38FA4F215D9FCB26 |
+-------+--------------+-------------------------------------------+
2 rows in set (0.01 sec)

2.2、current_user()为mydba@192.168.85.1,对应修改'mydba'@'192.168.85.1'的密码

# 从192.168.85.1上执行mysqladmin,要使用otherpwd密码才能执行
C:\Users\Administrator>mysqladmin -h192.168.85. -P3306 -umydba -p password newpwd1
Enter password: newpwd
mysqladmin: connect to server at '192.168.85.132' failed
error: 'Access denied for user 'mydba'@'192.168.85.1' (using password: YES)' C:\Users\Administrator>mysqladmin -h192.168.85. -P3306 -umydba -p password newpwd1
Enter password: otherpwd
Warning: Using a password on the command line interface can be insecure.
Warning: Server version is 5.7 or greater. The password will be sent to server in plain text. Upgrade the mysqladmin to a version that matches the server''s version. C:\Users\Administrator>

我们查看修改后的密码,以及binlog

# 查看修改后的密码,分别对应是不从客户端修改
mydba@192.168.85.132, [(none)]> select user,host,authentication_string from mysql.user where user='mydba';
+-------+--------------+-------------------------------------------+
| user | host | authentication_string |
+-------+--------------+-------------------------------------------+
| mydba | 192.168..% | *1FA85AA204CC12B39B20E8F1E839D11B3F9E6AA4 |
| mydba | 192.168.85.1 | *12AB8416B918C0EC1528FD04A686AE12D97A4A5D |
+-------+--------------+-------------------------------------------+
rows in set (0.01 sec) mydba@192.168.85.132, [(none)]> select password('newpwd'),password('newpwd1');
+-------------------------------------------+-------------------------------------------+
| password('newpwd') | password('newpwd1') |
+-------------------------------------------+-------------------------------------------+
| *1FA85AA204CC12B39B20E8F1E839D11B3F9E6AA4 | *12AB8416B918C0EC1528FD04A686AE12D97A4A5D |
+-------------------------------------------+-------------------------------------------+
row in set, warnings (0.00 sec) # 查看binlog
[root@ZST1 logs]# mysqlbinlog -vv --base64-output=decode-rows mysql-bin. --start-position=
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at
# :: server id end_log_pos CRC32 0xf0c57f78 GTID last_committed= sequence_number= rbr_only=no
SET @@SESSION.GTID_NEXT= '8ab82362-9c37-11e7-a858-000c29c1025c:69372'/*!*/;
# at
# :: server id end_log_pos CRC32 0x8a596597 Query thread_id= exec_time= error_code=
SET TIMESTAMP=/*!*/;
SET @@session.pseudo_thread_id=/*!*/;
SET @@session.foreign_key_checks=, @@session.sql_auto_is_null=, @@session.unique_checks=, @@session.autocommit=/*!*/;
SET @@session.sql_mode=/*!*/;
SET @@session.auto_increment_increment=, @@session.auto_increment_offset=/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=,@@session.collation_connection=,@@session.collation_server=/*!*/;
SET @@session.lc_time_names=/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
======================== 在ZST1上执行mysqladmin修改用户密码 ========================
ALTER USER 'mydba'@'192.168.85.%' IDENTIFIED WITH 'mysql_native_password' AS '*1FA85AA204CC12B39B20E8F1E839D11B3F9E6AA4'
/*!*/;
# at
# :: server id end_log_pos CRC32 0xfdf414d2 GTID last_committed= sequence_number= rbr_only=no
SET @@SESSION.GTID_NEXT= '8ab82362-9c37-11e7-a858-000c29c1025c:69373'/*!*/;
# at
# :: server id end_log_pos CRC32 0xe72ec7fb Query thread_id= exec_time= error_code=
SET TIMESTAMP=/*!*/;
======================== 在宿主机上执行mysqladmin修改用户密码 ========================
ALTER USER 'mydba'@'192.168.85.1' IDENTIFIED WITH 'mysql_native_password' AS '*12AB8416B918C0EC1528FD04A686AE12D97A4A5D'
/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
[root@ZST1 logs]#

两个ALTER USER对应两次mysqladmin修改用户密码
三、drop user 'mydba',没有指明host,它删除是哪个用户

mydba@192.168.85.132,3306 [(none)]> drop user 'mydba';
ERROR 1396 (HY000): Operation DROP USER failed for 'mydba'@'%'
mydba@192.168.85.132,3306 [(none)]>

根据错误信息,它删除的是用户'mydba'@'%'


18:17 2018/01/08 补充
四、如何备份用户权限信息
4.1、show create user,得到一致的用户名、密码

mydba@192.168.85.132,3306 [(none)]> show create user 'repl'@'192.168.85.%';
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| CREATE USER for repl@192.168.85.% |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| CREATE USER 'repl'@'192.168.85.%' IDENTIFIED WITH 'mysql_native_password' AS '*A424E797037BF97C19A2E88CF7891C5C2038C039' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

迁移到其他实例可以使用相同的用户名、密码
4.2、show grants for,可以得到用户的权限,只需将返回结果保存起来即可。如果user个数较多,建议拼接语句

# 拼接show grants语句
mydba@192.168.85.132,3306 [(none)]> select concat('show grants for ''',user,'''@''',host,''';') from mysql.user;
+------------------------------------------------------+
| concat('show grants for ''',user,'''@''',host,''';') |
+------------------------------------------------------+
| show grants for 'mydba'@'192.168.85.%'; |
| show grants for 'repl'@'192.168.85.%'; |
| show grants for 'mysql.session'@'localhost'; |
| show grants for 'mysql.sys'@'localhost'; |
| show grants for 'root'@'localhost'; |
+------------------------------------------------------+
5 rows in set (0.01 sec)
# 执行show grants语句得到权限语句
mydba@192.168.85.132,3306 [(none)]> show grants for 'repl'@'192.168.85.%';
+---------------------------------------------------------+
| Grants for repl@192.168.85.% |
+---------------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.85.%' |
+---------------------------------------------------------+
1 row in set (0.00 sec)

grant语句执行的前提:先创建对用user,或者grant xxx identified by 'pwd'
4.3、mysqlpump备份用户信息

# 使用mysqlpump直接备份用户信息
[root@ZST1 ~]# mysqlpump -h127.0.0. -P3306 -uroot -p --exclude-databases=% --users -A >/data/backup/user_pump_1323306_`date +%Y%m%d`.sql [root@ZST1 ~]# more /data/backup/user_pump_1323306_`date +%Y%m%d`.sql
...
CREATE USER 'repl'@'192.168.85.%' IDENTIFIED WITH 'mysql_native_password' AS '*A424E797037BF97C19A2E88CF7891C5C2038C039' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.85.%';

mysqlpump可以生成create、grant用户语句,mysqldump、mydumper是生成对应的insert语句

mysql.user细节三问的更多相关文章

  1. MySQL 系列(三)你不知道的 视图、触发器、存储过程、函数、事务、索引、语句

    第一篇:MySQL 系列(一) 生产标准线上环境安装配置案例及棘手问题解决 第二篇:MySQL 系列(二) 你不知道的数据库操作 第三篇:MySQL 系列(三)你不知道的 视图.触发器.存储过程.函数 ...

  2. qlserver、Mysql、Oracle三种数据库的优缺点总结

    这篇文章主要介绍了sqlserver.Mysql.Oracle三种数据库的优缺点总结,需要的朋友可以参考下   一.sqlserver优点:易用性.适合分布式组织的可伸缩性.用于决策支持的数据仓库功能 ...

  3. MySQL基础(三)——约束

    MySQL基础(三)--约束 约束是在表上强制执行的数据校验规则,主要用于维护表中数据的完整性以及当数据之间有以来关系时,保护相关的数据不会被删除. 根据约束对列的限制,可以划分为:单列约束(只约束一 ...

  4. mysql优化(三)–explain分析sql语句执行效率

    mysql优化(三)–explain分析sql语句执行效率 mushu 发布于 11个月前 (06-04) 分类:Mysql 阅读(651) 评论(0) Explain命令在解决数据库性能上是第一推荐 ...

  5. 【GDI+编程】--从三问开始

    一. GDI+三问 1.1 GDI+是什么? GDI+是GDI(Graphics Device Interface)的后继者,是一种图形设备的接口,它构成了Win XP操作系统的子系统的API. 1. ...

  6. MySQL系列(三)---索引

    MySQL系列(三)---索引 前言:如果有疏忽或理解不当的地方,请指正. 目录 MySQL系列(一):基础知识大总结 MySQL系列(二):MySQL事务 MySQL系列(三):索引 什么是索引 如 ...

  7. MySQL安装的三种方式

    .markdown-preview:not([data-use-github-style]) { padding: 2em; font-size: 1.2em; color: rgb(171, 178 ...

  8. RESTful三问

    我觉得学习一个技术,其实就是要弄明白三件事情:是什么(what),为什么(why),怎么用(how).正是所谓的三W方法. 所以打算总结一个"三问"系列.为了自己学习,也分享给别人 ...

  9. MySQL使用细节

    ************************************************************************ MySQL使用细节,包括部分常用函数以及注意如何提高数 ...

随机推荐

  1. Daily Scrum NO.3

    工作概况 符美潇(PM) 昨日完成的工作 1.Daily Scrum.日常会议及日常工作的分配和查收. 2.整合各DEV所写的代码,在TFS上进行Beta阶段第一次代码签入. 今日工作 1.Daily ...

  2. 《Linux内核分析》第三周学习小结 构造一个简单的Linux系统OS

    郝智宇 无转载 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 第三周 构造一个简单的Linux系统Me ...

  3. 第一Sprint阶段对各组提出的意见

    组号  组名 意见 1 理财猫 1.界面深色的部分,字体应调成亮色,可以适当美化字体. 2.希望能够自动记录花销,而不是只能手动“记一笔”. 3.功能略少,比如可以添加“收入详情”.“支出详情”. 2 ...

  4. idea 使用教程

    最智能的IDE IDEA相对于eclipse来说最大的优点就是它比eclipse聪明.聪明到什么程度呢?我们先来看几个简单的例子. 智能提示重构代码 如果你写的代码过于复杂,或者有更好的方式来替代你写 ...

  5. THE LAST ONE!! 2017《面向对象程序设计》课程作业八

    THE LAST ONE!! 2017<面向对象程序设计>课程作业八 031602230 卢恺翔 GitHub传送门 题目描述 1.时间匆匆,本学期的博客作业就要结束了,是否有点不舍,是否 ...

  6. @Autowire和@Resource注解的区别

    1.@Autowire是Spring开发的,而@Resource是jdk开发的 2.@Autowire是按照type来注解的,而@Resource是按照名称来的,如果名称找不到,那么就按照type,, ...

  7. Win 2008 r2 远程桌面多用户登陆,一用户多登陆配置

    Windows 2008 R2远程桌面,设置最大连接数,一个登录后另一个就被踢掉等问题 Windows 2008 R2配置如图: 1.打开远程桌面回话主机配置 2.右键RDP-Tcp,属性,可设置最大 ...

  8. 【转载】Understanding When to use RabbitMQ or Apache Kafka

    https://content.pivotal.io/rabbitmq/understanding-when-to-use-rabbitmq-or-apache-kafka RabbitMQ: Erl ...

  9. 【菜鸟】RESTful 架构详解

    RESTful 架构详解 分类 编程技术 1. 什么是REST REST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状态转移. 它首次 ...

  10. 【刷题】BZOJ 1924 [Sdoi2010]所驼门王的宝藏

    Description Input 第一行给出三个正整数 N, R, C. 以下 N 行,每行给出一扇传送门的信息,包含三个正整数xi, yi, Ti,表示该传送门设在位于第 xi行第yi列的藏宝宫室 ...