recover MySQL root password with mysqld_safe

本文介绍了一种通过五步操作来重置MySQL root密码的方法:停止MySQL服务、以安全模式启动MySQL、更改root密码、停止安全模式下的MySQL以及重新启动MySQL服务。此外,还提供了一种使用debian-sys-maint用户身份登录并重置密码的替代方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://newexception.com/recover-mysql-root-password


In order to recover MySQL root password you need to do this five easy steps.



1. Stop MySQL server
2. Start MySQL in safe mode
3. Change MySQL root password
4. Stop MySQL safe mode
5. Start MySQL server
Stop MySQL server
To stop MySQL server under Ubuntu / Debian use one of the following commands:


#service mysql stop
If that doesn't work, you can still kill the mysqld process


#killall mysql
or


#kill -9 PID
where PID is the process id of the MySQL daemon


Start MySQL in safe mode
#mysqld_safe --skip-grant-tables
or


#mysql_safe --skip-grant-tables &
The first command will start MySQL in safe mode and you will need to keep the terminal open. The second command will put MySQL in background.


Change MySQL root password
Login to MySQL as root


#mysql -u root -p
mysql> USE mysql;
mysql> UPDATE user SET password = PASSWORD("new_password") WHERE User = 'root';
What we have done here is we told MySQL that we are going to use the MySQL database, because all settings regarding MySQL are stored in this database.


Stop MySQL safe mode
Go back to the terminal where you started MySQL in safe mode and press CTRL + C. If you used the second command, you will need to kill the process.


#killall mysqld_safe
Note: Before proceeding, be sure that MySQL is not running.
Start MySQL server
#sudo service mysql start
You are done. Now you can login to MySQL with your new password.


Recover MySQL root password with debian-sys-maint user
Another way to recover the root password is by login with the debian-sys-maint user, which will work only in Ubuntu / Debian. You can find this info in the debian.cnf file which is located under /etc/mysql.


# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = debian-sys-maint
password = PASSWORD
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = PASSWORD
socket   = /var/run/mysqld/mysqld.sock
basedir  = /usr
Simply login with debian-sys-maint user and enter the password which is specified in the debian.cnf file


#mysql -u debian-sys-maint -p
Thanks to Regis Caspar for pointing this out.
<think>好的,用户遇到了MySQL启动失败的问题,需要分析mysqld_safe的日志,特别是localhost.localdomain.err文件中的内容。我得先回想一下相关的知识,并结合提供的引用内容来给出解决方案。 首先,用户提到的错误日志路径是/var/lib/mysql/localhost.localdomain.err,这可能是因为MySQL在启动过程中遇到了问题,导致mysqld_safe无法正常启动。根据引用[3],启动失败时应该通过查看这个.err文件来获取详细信息。所以第一步要确认用户是否已经检查了这个文件的具体内容。 接下来,mysqld_safe负责启动mysqld进程,如果出现无响应的情况,可能的原因有很多,比如配置错误、权限问题、端口冲突或者资源不足。引用[2]提到mysql_safe无响应,可能的原因包括配置文件错误或者资源限制。需要用户检查my.cnf中的配置,尤其是[mysqld]、[server]和[mysqld_safe]部分,是否有错误的参数设置,比如内存分配过大。同时,引用[4]指出mysqld_safe会读取这些部分的配置,所以任何配置错误都可能导致启动失败。 另外,权限问题也是一个常见原因。MySQL的数据目录和文件需要有正确的所有权,通常应该是mysql用户和组。用户需要检查/var/lib/mysql目录及其子文件的权限是否正确。如果权限不对,使用chown和chmod命令进行调整。 端口冲突也可能导致启动失败,比如3306端口被其他程序占用。用户可以通过netstat或lsof命令检查端口使用情况,确保没有其他进程占用该端口。 资源不足的情况,比如内存或磁盘空间不足,也可能导致MySQL无法启动。需要检查系统的可用资源,尤其是磁盘空间是否足够,以及是否有足够的内存分配给MySQL。 如果错误日志中有具体的错误信息,比如无法创建pid文件,或者表损坏,需要针对性地解决。例如,pid文件路径是否正确,是否有写入权限;如果是InnoDB表损坏,可能需要使用innodb_force_recovery参数进行恢复。 最后,建议用户备份错误日志,尝试在安全模式下启动,或者逐步注释掉配置选项,排除问题。如果所有方法都无效,可能需要考虑重新初始化数据目录或重新安装MySQL,但这是最后的手段,需要谨慎操作。 总结来说,需要用户提供具体的错误日志内容,才能更精准地定位问题所在。然后根据不同的错误信息,逐一排查配置、权限、资源等方面的问题。</think>### MySQL启动失败日志分析步骤 #### 1. **定位错误日志文件** 根据引用[2]和[3],`mysqld_safe`启动失败的日志记录在以下路径: ```bash /usr/local/mysql/data/localhost.localdomain.err # 或 /var/lib/mysql/localhost.localdomain.err ``` 使用命令查看具体错误: ```bash more /var/lib/mysql/localhost.localdomain.err # 引用[3] ``` #### 2. **常见错误类型与解决方案** ##### **2.1 配置文件问题** - **问题表现**:日志中可能包含`unknown variable`或`option error`。 - **解决方法**: - 检查`my.cnf`或`my.ini`配置文件的语法,尤其是`[mysqld]`、`[server]`和`[mysqld_safe]`部分[^4]。 - 示例:若配置了无效参数如`innodb_buffer_pool_size=8G`但内存不足,需调整为合理值。 ##### **2.2 权限问题** - **问题表现**:日志提示`Permission denied`或`cannot create file`。 - **解决方法**: ```bash chown -R mysql:mysql /var/lib/mysql # 确保数据目录所有权正确 chmod 755 /var/lib/mysql # 调整目录权限 ``` ##### **2.3 端口冲突或资源不足** - **问题表现**:`Can't start server: Bind on TCP/IP port: Address already in use`或`Out of memory`。 - **解决方法**: ```bash netstat -tuln | grep 3306 # 检查端口占用 free -h # 查看内存使用情况 ``` ##### **2.4 数据文件损坏** - **问题表现**:日志中出现`InnoDB: Database page corruption`或`Table is marked as crashed`。 - **解决方法**: - 尝试修复表: ```bash myisamchk --force --safe-recover /path/to/table.MYI ``` - 若InnoDB损坏,在配置文件中添加: ```bash innodb_force_recovery = 1 # 从1到6逐步尝试 ``` #### 3. **安全启动与调试** - **强制清理残留进程**: ```bash pkill mysqld # 终止残留进程 rm -f /var/lib/mysql/mysql.sock # 删除残留套接字文件 rm -f /var/run/mysqld/mysqld.pid # 删除残留PID文件,引用[3] ``` - **手动启动调试**: ```bash mysqld_safe --skip-grant-tables --skip-networking & # 安全模式启动 ``` #### 4. **备份与恢复** - **备份错误日志**: ```bash cp /var/lib/mysql/localhost.localdomain.err /tmp/mysql_error_backup.log ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值