首页 > 数据库 >创建用户及密码问题汇总mysql

创建用户及密码问题汇总mysql

时间:2023-09-11 21:36:12浏览次数:34  
标签:SET OLD -- 汇总 密码 user mysql password

一、创建新用户能看到却连不上

场景:
通过mysqldump导出,迁移到新实例导入用户,能在用户表里面看到已有用户,但是连不上

[root@master1 ~]# mysqldump -uroot -pxxxxxxx --set-gtid-purged=off --single-transaction 
mysql user --where='user="test_user"' -t > user.sql
[root@master1 ~]# cat user.sql 
-- MySQL dump 10.13  Distrib 8.0.32, for Linux (x86_64)
--
-- Host: localhost    Database: mysql
-- ------------------------------------------------------
-- Server version	8.0.32

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!50503 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Dumping data for table `user`
--
-- WHERE:  user="test_user"

LOCK TABLES `user` WRITE;
/*!40000 ALTER TABLE `user` DISABLE KEYS */;
INSERT INTO `user` VALUES ('%','test_user','N','N','N','N','N','N','N','N','N','N','N','N','N','N','N','N','N','N','N','N','N','N','N','N','N','N','N','N','N','','','','',0,0,0,0,'mysql_native_password','*BCF4F28E525ED7EE4664FFFF4DAE13EC14A6ABE1','N','2023-04-24 02:42:01',NULL,'N','N','N',NULL,NULL,NULL,NULL);
/*!40000 ALTER TABLE `user` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */

新实例
[root@master1 ~]# mysql -uroot -pxxxxxx mysql <user.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@master1 ~]# mysql -uroot -pxxxxxx mysql -e'select user,host from mysql.user where user="test_user"'
mysql: [Warning] Using a password on the command line interface can be insecure.
+-----------+------+
| user      | host |
+-----------+------+
| test_user | %    |
+-----------+------+
[root@master1 ~]# mysql -utest_user -pTest@123
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'test_user'@'localhost' (using password: YES)

解决:
mysqldump导出的用户,是用insert 方式插入到mysql.user表,虽然能查询看到,但是内存权限表还是旧的,需要重新加载内存权限表。--flush privileges;

[root@master1 ~]# mysql -uroot -pxxxxxx mysql -e'flush privileges;'
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@master1 ~]# mysql -utest_user -pTest@123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 31
Server version: 8.0.32 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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>

二、不知道原密码情况下迁移用户登录失败

场景:
迁移数据库用户的时候,会根据authentication_string字段查到的加密信息创建用户,然后登录失败

原实例
mysql> select user,host,plugin,authentication_string from mysql.user where user='test_user';
+------------------+---------------+-----------------------+------------------------------------------------------------------------+
| user             | host          | plugin                | authentication_string                                                  |
+------------------+---------------+-----------------------+------------------------------------------------------------------------+
| test_user        | %             | mysql_native_password | *BCF4F28E525ED7EE4664FFFF4DAE13EC14A6ABE1                              |
+------------------+---------------+-----------------------+------------------------------------------------------------------------+
8 rows in set (0.00 sec)

新实例
mysql>create user test_user@'%' identified by '*BCF4F28E525ED7EE4664FFFF4DAE13EC14A6ABE1';
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> \q
Bye
[root@master1 ~]# mysql -utest_user -pTest@123
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'test_user'@'localhost' (using password: YES)

解决:
通过authentication_string查询到的加密串,需要用with plugin as 的方式创建用户,才会保持原密码不变

WITH mysql_native_password  as

mysql> drop user test_user@'%';
Query OK, 0 rows affected (0.00 sec) 
mysql> create user test_user@'%' identified WITH mysql_native_password  as '*BCF4F28E525ED7EE4664FFFF4DAE13EC14A6ABE1';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> \q
Bye
[root@master1 ~]# mysql -utest_user -pTest@123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.32 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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>

三、创建用户报错

场景:
通过authentication_string查询到的加密串,用as来创建用户,报错

mysql> create user test_user@'%' identified WITH mysql_native_password  as '$A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED';
ERROR 1827 (HY000): The password hash doesn't have the expected format.

解决:
通过authentication_string查询到的加密串来创建用户,需要注意plugin的不同,当plugin是mysql_native_password时authentication_string加密串长度是41,当plugin是caching_sha2_password的authentication_string加密串长度是70。--WITH caching_sha2_password  as

mysql> select user,host,plugin,authentication_string,length(authentication_string) length from mysql.user;
+------------------+---------------+-----------------------+------------------------------------------------------------------------+--------+
| user             | host          | plugin                | authentication_string                                                  | length |
+------------------+---------------+-----------------------+------------------------------------------------------------------------+--------+
| root             | %             | mysql_native_password | *098E13F0BBC737BD4608363FE8A0A13A348F3934                              |     41 |
| donor_user       | 192.168.137.% | mysql_native_password | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19                              |     41 |
| repl             | 192.168.137.% | mysql_native_password | *A424E797037BF97C19A2E88CF7891C5C2038C039                              |     41 |
| mysql.infoschema | localhost     | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |     70 |
| mysql.session    | localhost     | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |     70 |
| mysql.sys        | localhost     | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |     70 |
| root             | localhost     | mysql_native_password | *613997A8B0E1757211BDC25E3E8D0F5A1C0428B2                              |     41 |
+------------------+---------------+-----------------------+------------------------------------------------------------------------+--------+
mysql> create user test_user@'%' identified WITH caching_sha2_password  as '$A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED';
Query OK, 0 rows affected, 1 warning (0.00 sec)

标签:SET,OLD,--,汇总,密码,user,mysql,password
From: https://blog.51cto.com/u_13482808/7438449

相关文章

  • MySQL学习01
    一、数据库简介1、为什么需要数据库1、磁盘->高级缓存->寄存器->CPU数据存储在内存中,但是内存大小有限、不可能存储所有数据,并且掉电后数据丢失2、为了让程序在关机重启后数据依然可以使用,必须把数据保存在磁盘文件中3、随着程序功能越来越复杂、数据量越来越多、数据关系也......
  • MySQL数据误删flashback
    使用binlog2sql(Python版)解析binlog文件来创建回滚的sql准备工作需要提前安装Python环境gitclonehttps://github.com/danfengcao/binlog2sql.gitcdbinlog2sqlpip3install-rrequirements.txtMySQL必须开启binlog,且binlog_format=ROW,且binlog_row_image=FULLshowv......
  • MYSQL笔记
    一、创建列表1、 创建库CREATEDATABASEwjd_table2、 删库,dropdatabasetable_name;3、 选库usetable_name;4、 类型分别有:1) char或character(负责数据,需设定长度);2) int或inteser(数字为整数或负数);dec(提供数值空间);3) datatime或timestamp(负责记录时间和日期);4) blob(大量文......
  • OGG-Postgres同步到MySQL
    (一)数据库信息名称源端数据库目标端数据库数据库类型Postgresql12.4MySQL5.7IP地址20.2.127.2320.2.127.24端口54323306数据库testpdbtest用户ogguserroot密码ogguserP2021Root@1234 postg......
  • 基于PHP vue2+element+ laravel8+ mysql开发的不良事件管理系统源码
    技术架构:PHP vue2+element+laravel8+mysql5.7+vscode不良事件上报系统通过“事前的人员知识培训管理和制度落地促进”、“事中的事件上报和跟进处理”、以及“事后的原因分析和工作持续优化”,结合预存上百套已正在使用的模板,帮助医院从对护理事件、药品事件、医疗器械事件、......
  • 【笔记】Mysql常用操作
     linux上安装客户端 下载地址:https://dev.mysql.com/downloads/mysql/ 操作系统版本选择redhat,  在下载列表中选择ClientUtitiles  安装rpm-ivhmysql-community-client-5.7.36-1.el7.x86_64.rpm--nodeps--force ......
  • 虚拟机忘记密码后,centos重置密码方法
    一、使用步骤1、重启虚拟机重启虚拟机后,按“E”键,进入单用户模式; 2、改“ro”为“rw”,写上目录rd.break,进入进击救援模式;在下图箭头标识的地方ro改为rwrd.break 3、改完之后,按ctrl+x重启shell 4、使用命令chroot/sysroot切换到真正的系统目录chroot/sysroot......
  • mysql 体系结构
     mysql数据库的体系结构如下图:1.连接器:主要负责客户端与数据库的连接a.连接后没有任何动作,那么连接就处于空闲状态,showfullprocesslist进行查看b.客户端连接数据库后会在一定时间范围内断开连接,具体时间可以进行查看showvariableslike'%timeout%'interactive_timeout:服务器......
  • MySQL篇:第一章_软件安装和基本操作
    本篇安装软件NavicatePremium16破解版和phpstudy_prophpstudy_pro安装教程phpstudy官网:https://www.xp.cn/download.html下载等待下载完成后,双击可执行文件开始安装,选择自定义安装;等待安装完成即可拥有MySQL环境NavicatePremium16破解版安装教程官网地址:https://ww......
  • 【验】Postfix+Dovecot+MySQL搭建邮件服务器
    网上有很多使用Postfix搭建邮件服务器的文章,但目前貌似没有看到较为完整的一篇。本例将尝试在Ubuntu系统中使用Postfix+Dovecot+MySQL搭建邮件服务器。说到邮件服务器,网上有许多不同解决方案。Window操作系统下常见的邮件服务器有hMailServer、MailEnable、EVOMailServer、ApacheJ......