如果哪天你忘记了线上MySQL数据库的root密码,怎么办?
大家往往会想到skip-grant-tables参数,具体步骤如下:
1. 关闭MySQL数据库,因为root密码忘记了,mysqladmin无法使用,此时,只能通过kill pid关闭程序。
在这里,科普一下kill 和kill -9的区别
默认参数下,kill 发送SIGTERM信号给进程,告诉进程,你需要被关闭,请自行停止运行并退出。
kill -9 发送SIGKILL信号给进程,告诉进程,你被终结了,请立刻退出。与SIGTERM相比,这个信号不能被捕获或忽略,同时接收这个信号的进程在收到这个信号时不能执行任何清理
所以,万不得已,不要通过kill -9杀掉进程,这可能导致MySQL数据库的物理结构损坏,无法重新启动。
2. 在my.cnf文件[mysqld]部分添加skip-grant-tables参数
3. 登录数据库,修改root账户的密码
以下是修改root密码的三种方式:
1> mysql> set password for 'root'@'localhost'=password('123'); 无需刷新权限表
2> mysql> update mysql.user set password=password("456") where user="root" and host="localhost";
mysql> flush privileges;
3> # mysqladmin -u root password "123"
4. 关闭数据库,注释掉skip-grant-tables参数,重新启动数据库。
上面这种方式虽然不错,但是有个问题,你必须重启数据库,对于线上环境,这可能是不被允许的。
下面来谈谈另一种方法,有点“黑暗科技”的味道
这个方法利用的是mysql.user表还是MyISAM引擎的特性。
1. 将该实例的mysql.user表copy到另一个实例的目录下,譬如,test数据库的目录下

2. 登录另一个实例数据库,修改上述三个文件的权限,并修改root密码
mysql> select user,host,password from test.user;
+------+-----------+-------------------------------------------+
| user | host | password |
+------+-----------+-------------------------------------------+
| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+------+-----------+-------------------------------------------+
1 row in set (0.00 sec)
mysql> update test.user set password=password("hello") where user="root" and host="localhost";
Query OK, 1 row affected (0.15 sec)
Rows matched: 1 Changed: 1 Warnings: 0
3. 将上述三个文件copy回源数据库

4. 获取mysqld的pid,通过kill -HUP `pidof mysqld`方式让mysqld进程重新加载配置文件

[root@keepalived01 ~]# mysql -phello
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@keepalived01 ~]# kill -HUP 4283
[root@keepalived01 ~]# mysql -phello
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 2528
Server version: 5.6.26 MySQL Community Server (GPL)
Copyright (c) 2000, 2015, 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>
通过上述输出可以看出,kill -HUP之前,直接用密码hello登录被拒绝,kill -HUP之后,就可以直接登录了。
当然,以上方法仅供参考,在生产上慎用,毕竟安全压倒一切,天晓得哪里会出现问题。
以上就是本文的全部内容,希望可以帮助大家解决root密码忘记的困扰,谢谢大家的阅读。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于架构原理的相关内容,MySQL Server架构自顶向下大致可以分网络连接层、服务层、存储引擎层和系统文件层,下面一起来看一下,希望对大家有帮助。

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

mysql的msi与zip版本的区别:1、zip包含的安装程序是一种主动安装,而msi包含的是被installer所用的安装文件以提交请求的方式安装;2、zip是一种数据压缩和文档存储的文件格式,msi是微软格式的安装包。

方法:1、利用right函数,语法为“update 表名 set 指定字段 = right(指定字段, length(指定字段)-1)...”;2、利用substring函数,语法为“select substring(指定字段,2)..”。

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。

在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
