重置 Docker 容器中 MySQL8 实例的 root 帐户密码,解决忘记密码的问题


如果我们是通过非 Docker 方式安装的 MySQL,可以直接通过mysqld --skip-grant-tables命令跳过权限表的限制,无密码启动 MySQL 服务,这样任何账号都无需密码就可以登录到 MySQL 服务,然后进行密码重置的操作就行了,非常简单。

如果我们是通过 Docker 方式安装的 MySQL,如何重置密码呢?本文将介绍如何重置 Docker 容器中 MySQL8 实例的 root 帐户密码以解决忘记密码的问题。

具体操作步骤如下:

  1. 登录到 MySQL8 的 Docker 容器中

    1
    docker exec -it <容器ID> bash
  2. 通过vivim命令修改 MySQL8 的 my.cnf 配置文件,在最后一行增加skip-grant-tables配置

    1
    vim /etc/mysql/my.cnf

    修改后的my.cnf配置文件内容如下:

    my.cnf
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    # Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
    #
    # This program is free software; you can redistribute it and/or modify
    # it under the terms of the GNU General Public License as published by
    # the Free Software Foundation; version 2 of the License.
    #
    # This program is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    # GNU General Public License for more details.
    #
    # You should have received a copy of the GNU General Public License
    # along with this program; if not, write to the Free Software
    # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

    #
    # The MySQL Server configuration file.
    #
    # For explanations see
    # http://dev.mysql.com/doc/mysql/en/server-system-variables.html

    [mysqld]
    pid-file = /var/run/mysqld/mysqld.pid
    socket = /var/run/mysqld/mysqld.sock
    datadir = /var/lib/mysql
    secure-file-priv= NULL
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0

    # Custom config should go here
    !includedir /etc/mysql/conf.d/
    skip-grant-tables

    如果执行vivim命令时提示bash: vi: command not foundbash: vim: command not found,需要先通过如下命令安装vivim

    1
    2
    apt-get update
    apt-get install vim

    如果安装vivim失败,出现Temporary failure resolving 'deb.debian.org提示,需要先退出 Docker 容器,在宿主机上创建/etc/docker/daemon.json文件,重启 Docker 服务后继续登录到 MySQL8 的 Docker 容器中安装vivim
    1)退出 MySQL 容器

    1
    exit

    2)创建并编辑 daemon.json 文件

    1
    vim /etc/docker/daemon.json
    daemon.json
    1
    2
    3
    {
    "dns": ["8.8.8.8", "8.8.4.4"]
    }

    3)重启 Docker 服务

    1
    service docker restart

    4)继续登录到 MySQL8 的 Docker 容器中

    1
    docker exec -it <容器ID> bash
  3. 修改my.cnf配置文件后,退出并重启 MySQL 容器
    1)退出 MySQL 容器

    1
    exit

    2)重启 MySQL 容器

    1
    docker restart <容器ID>
  4. 免密登录到 MySQL 容器中的 MySQL 服务

    1
    docker exec -it <容器ID> mysql -uroot 
  5. 修改 root 账号登录密码

    1
    2
    3
    4
    use mysql;
    flush privileges;
    alter user 'root'@'localhost' identified by '123456';
    flush privileges;

    注:alter user之前需要先flush privileges,否则会出现ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

  6. 退出 MySQL 登录,重新登录到 MySQL8 的 Docker 容器中,修改my.cnf配置文件,去掉skip-grant-tables配置后重启 MySQL 容器即可