如果使用 MySQL 数据库忘记了root账号密码,可以通过调节配置文件,跳过密码的方式登数据库,

在数据库里面修改账号密码,一般默认的账号是 root

1、编辑 MySQL 配置文件 my.cnf

注意: 以实际 my.cnf 配置文件路径为准

vim /etc/my.cnf

[mysqld]

spik-grant-tables  #增加

2、重启 MySQL 服务

注意:以实际 MySQL 启动脚本路径为准

/etc/init.d/mysqld restart

若报错,注意观察my.cnf配置文件中的内容

3、登陆数据库

/usr/bin/mysql 输入如下命令:

注意:以实际 MySQL 执行文件路径为准

mysql> USE mysql;

mysql> UPDATE user SET Password = password ('新密码') WHERE User = 'root' ;

mysql> flush privileges ;

mysql> quit

4、删除或者注释第一步骤中添加的 spik-grant-tables

5、重启 MySQL 服务

/etc/init.d/mysqld restart

6、使用新密码测试

特殊情况:

我这是使用源码安装的mysql,默认配置文件my.cnf(/usr/local/mysql/my.cnf)中的内容都没有配置,都是在启动文件mysqld(/etc/rc.d/init.d/mysqld)中进行配置的

datadir=/data/mysql

basedir=/usr/local/mysql

若按照上述方法进操作,在my.cnf中添加"spik-grant-tables",则重启mysql时报错

正确的解决方法:

根据启动文件mysqld中的datadirbasedir参数相应的启用my.cnf中的选项,然后再添加"spik-grant-tables",mysql重启就不会报错,也能正常按照下面的步骤进行

未修改前的my.cnf

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# spik-grant-tables
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port =  .....
# server_id = .....
# socket = .....

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

修改后的my.cnf

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# spik-grant-tables
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
basedir = /usr/local/mysql
datadir = /data/mysql
port = 3306
# server_id = .....
# socket = .....
skip-grant-tables

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

然后再重启mysql,进行下面的步骤,重置密码后,再把my.cnf中的修改的那四项全部注释掉,再次重启mysql即可。

linux下mysql忘记root密码解决方法的更多相关文章

  1. Linux 下mysql忘记root密码解决方法

    忘记root密码怎么办:1.关闭数据库2.使用-->mysqld_safe --skip-grant-tables &--<启动数据库3.使用空密码进入数据库(mysql命令后直接 ...

  2. CentOS下MySQL忘记root密码解决方法【转载】

    1.首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库. 因为在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护的 状态下,其他的用户也可以任意地登录 ...

  3. 【转】CentOS下MySQL忘记root密码解决方法

    原文转自:http://www.cnblogs.com/sbaicl/articles/3132010.html 1.首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库. 因为在 ...

  4. CentOS下MySQL忘记root密码解决方法【亲测】

    1.修改MySQL的登录设置: # vim /etc/my.cnf 在[mysqld]的段中加上一句:skip-grant-tables 例如: [mysqld] datadir=/var/lib/m ...

  5. linux下mysql忘记root密码怎么办

    Linux下MySQL忘记root密码怎么办? Linux下MySQL忘记root密码怎么办? 1. 修改MySQL配置文件 默认MySQL的配置文件为/etc/my.cnf,在[mysqld]下面添 ...

  6. MySQL 忘记root密码解决方法,基于Ubuntu 14.10

    忘记MySQL root密码解决方法,基于Ubuntu 14.10 忘了mysql密码,从网上找到的解决方案记录在这里. 编辑mysql的配置文件/etc/mysql/my.cnf,在[mysqld] ...

  7. Linux下mysql忘记root密码

    一台机器上的MYSQL服务器很久没用了,忘了root密码无法连接.一时情急,网上搜寻办法,解决,记录在此备用. 修改MySQL的登录设置:  //不同的版本的Linux配置文件的位置也不一样,以Lin ...

  8. linux下mysql忘记root密码的解决方案

    1.首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库. 因为在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护的 状态下,其他的用户也可以任意地登录 ...

  9. mysql忘记root密码解决方法

    1.需求 如何找回root密码 2.流程 a.首先停止数据库 b.在mysqld进程配置文件中添加skip-grant-tables, c.重启数据库 d.改root密码,不用密码直接登录mysql ...

随机推荐

  1. Second Day learning English

    Today I have set my Microsoft word program, use it send documents to the blog site.

  2. LeetCode: 221_Maximal Square | 二维0-1矩阵中计算包含1的最大正方形的面积 | Medium

    题目: Given a 2D binary matrix filled with 's and return its area. For example, given the following ma ...

  3. 自动SPF生成工具

    到openspf网站去自动生成一下,地址是http://old.openspf.org/wizard.html.详细解释见下图关于spf的详细语法请看http://www.openspf.org/SP ...

  4. HBase Snapshot功能介绍

    HBase在0.94之后提供了Snapshot功能,一个snapshot其实就是一组metadata信息的集合,它可以让管理员将表恢复到以前的一个状态.snapshot并不是一份拷贝,它只是一个文件名 ...

  5. JavaScript - javascript 中的 "||" 与 "&&" 的理解与灵活运

    你肯定见到过这样的代码:a = a||"xxx". 它其实就等价于下面三种形式的代码: a = a || "xxx"; 与: if (!a) { a = &qu ...

  6. ping: sendto: Network is unreachable

    在我的板子上ping路由上的IP的时候可以ping通,但是ping外网的IP的时候提示"ping: sendto: Network is unreachable" 后来使用rout ...

  7. mmo设计

    基于多人格斗系统设计. 总体 1.放技能/使用道具,行走,公告,聊天 共性: A.服务端代理推送 B.管道内推送 2.玩家信息(统一玩家信息查看,去除每个模块自己实现) 3.怪物掉落(统一掉落控制.领 ...

  8. PostgreSQL和Greenplum、Npgsql

    PostgreSQL和Greenplum.Npgsql 想着要不要写,两个原因“懒”和“空”.其实懒和空也是有联系的,不是因为懒的写,而是因为对PostgreSQL和Npgsql的知识了解匮乏,也就懒 ...

  9. AC_Dream 1224 Robbers(贪心)

    题意:n个抢劫犯分别抢到的金钱是k1, k2, k3,...,一共得到的金钱是m, 但是在分钱的时候是按照x1/y, x2/y, x3/y,....的比例进行分配的!这样的话 一些抢劫犯就会觉得不公平 ...

  10. Asp.Net(C#)自动执行计划任务的程序实例分析

    在业务复杂的应用程序中,有时候会要求一个或者多个任务在一定的时间或者一定的时间间隔内计划进行,比如定时备份或同步数据库,定时发送电子邮件等,我们称之为计划任务.实现计划任务的方法也有很多,可以采用SQ ...