如果使用 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. DataGridView中添加CheckBox列用于选择行

    DataGridView中添加CheckBox列用于选择行 1,编辑DataGridView,添加一列 CheckBox ,Name 赋值为 "select",如下图: 2,取消 ...

  2. 配置算法(第4版)的Java编译环境

    1. 下载 1.1 JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html选择“Windows x64 180.5 ...

  3. jquerymobile标签-属性手册

    Button data-role="button"   data-corners true | false data-icon home | delete | plus | arr ...

  4. redis的主从复制部署和使用

    reids一种key-value的缓存数据库目前非常流行的被使用在很多场景,比如在数据库读写遇到瓶颈时缓存且读写分离会大大提升这块的性能,下面我就说说redis的主从复制 首先需要启动多个redis实 ...

  5. 配置svn

    Linux搭建SVN Server 其中包括添加防火墙规则

  6. libevent源码深度剖析

    原文地址: http://blog.csdn.net/sparkliang/article/details/4957667 第一章 1,前言 Libevent是一个轻量级的开源高性能网络库,使用者众多 ...

  7. [原]运行编译好的Android模拟器

    Android源码编译好了之后,我们就可以运行它了. 1.配置环境变量: /data/data/Android$ export PATH=$PATH:$(pwd)/out/host/linux-x86 ...

  8. [CS231n-CNN] Training Neural Networks Part 1 : activation functions, weight initialization, gradient flow, batch normalization | babysitting the learning process, hyperparameter optimization

    课程主页:http://cs231n.stanford.edu/   Introduction to neural networks -Training Neural Network ________ ...

  9. sql: 查找约束

    主键约束 SELECT   tab.name AS [表名],   idx.name AS [主键名称],   col.name AS [主键列名] FROM   sys.indexes idx    ...

  10. HTML5[3]:中文换行

    保证中文每行第一个字,不会出现标点符号 p { white-space: pre-wrap; }