MySQL 无法在 ubuntu 16 上使用 --skip-grant-tables 重置 root 密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41984956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 22:10:30  来源:igfitidea点击:

Can't reset root password with --skip-grant-tables on ubuntu 16

mysqlubuntuubuntu-16.04rootchange-password

提问by pedronalbert

I am trying to reset the root password following MysqlPasswordResetbut when I try to start the server with --skip-grant-tables the server doesn't start

我正在尝试按照MysqlPasswordReset重置根密码,但是当我尝试使用 --skip-grant-tables 启动服务器时,服务器没有启动

  • Ubuntu 16.04.1 LTS (GNU/Linux 4.4.0-59-generic x86_64)
  • mysql Ver 14.14 Distrib 5.7.17, for Linux (x86_64)
  • Ubuntu 16.04.1 LTS (GNU/Linux 4.4.0-59-generic x86_64)
  • mysql Ver 14.14 Distrib 5.7.17,适用于 Linux (x86_64)

Server is running

服务器正在运行

$ mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

Stop server

停止服务器

$ sudo /etc/init.d/mysql stop
[ ok ] Stopping mysql (via systemctl): mysql.service.

Trying to start with --skip-grant-tables

尝试从 --skip-grant-tables 开始

sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &
[1] 9856

Connect with no password

无需密码即可连接

$ mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
[1]+  Exit 1                  sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking

I also tried to start with mysql_safe (error.log is empty)

我也尝试从mysql_safe开始(error.log为空)

sudo mysqld_safe --skip-grant-tables
2017-02-01T16:33:31.382105Z mysqld_safe Logging to syslog.
2017-02-01T16:33:31.383942Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2017-02-01T16:33:31.386058Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2017-02-01T16:33:31.388009Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.

回答by pedronalbert

I found that the mysql.sock is deleted when the mysql service is stoped and mysqld_safe can't create it (I couldn't find the reason), so my solution was back up the sock folder and restore before start mysqld_safe

发现mysql服务停止时mysql.sock被删除,mysqld_safe无法创建(原因没找到),所以我的解决办法是在启动mysqld_safe之前备份sock文件夹并恢复

Start server

启动服务器

$ sudo service mysql start

Go to sock folder

进入袜子文件夹

$ cd /var/run

Back up the sock

备份袜子

$ sudo cp -rp ./mysqld ./mysqld.bak

Stop server

停止服务器

$ sudo service mysql stop

Restore the sock

恢复袜子

$ sudo mv ./mysqld.bak ./mysqld

Start mysqld_safe

启动mysqld_safe

$ sudo mysqld_safe --skip-grant-tables --skip-networking &

Init mysql shell

初始化mysql shell

mysql -u root

Change password

更改密码

FLUSH PRIVILEGES;

SET PASSWORD FOR root@'localhost' = PASSWORD('my_new_password');

回答by dvlcube

For Ubuntu 19 with MySQL 8.0.17-0ubuntu2, what ended up working for me was a combination of many answers:

对于带有 MySQL 8.0.17-0ubuntu2 的 Ubuntu 19,最终对我有用的是许多答案的组合:

  1. In the MySQL's configuration file (/etc/mysql/mysql.conf.d/mysqld.cnfon my machine), under [mysqld], add:

    skip-grant-tables = 1 plugin-load-add = auth_socket.so

  2. Restart the MySQL Service;

  3. Connect to MySQL: mysql -uroot;

  4. Run:

  1. 在 MySQL 的配置文件(/etc/mysql/mysql.conf.d/mysqld.cnf在我的机器上)的 下[mysqld],添加:

    skip-grant-tables = 1 plugin-load-add = auth_socket.so

  2. 重启MySQL服务;

  3. 连接到 MySQL: mysql -uroot;

  4. 跑:

UPDATE mysql.user SET authentication_string=null WHERE User='root';
FLUSH PRIVILEGES;

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'pass123';
UPDATE mysql.user SET authentication_string=null WHERE User='root';
FLUSH PRIVILEGES;

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'pass123';
  1. Stop MySQL and comment skip-grant-tablesin the configuration file;

  2. Start MySQL again and this should now work: mysql -u root -ppass123.

  1. 停止 MySQL 并skip-grant-tables在配置文件中注释;

  2. 启动MySQL再次,这应该现在的工作:mysql -u root -ppass123

回答by auhsor

pedronalbert'sanswer above worked for me but the last step is now deprecated and throws the following warning:

pedronalbert上面回答对我有用,但最后一步现在已弃用并引发以下警告:

Warning | 1287 | 'SET PASSWORD FOR = PASSWORD('')' is deprecated and will be removed in a future release. Please use SET PASSWORD FOR = '' instead

警告 | 第1287章 'SET PASSWORD FOR = PASSWORD('')' 已弃用,将在未来版本中删除。请使用 SET PASSWORD FOR = '' 代替

Use this command instead:

请改用此命令:

SET PASSWORD FOR root = '<plaintext_password>';

回答by gevge

I tried many ways including @pedronalbert 's but still not working.

我尝试了很多方法,包括 @pedronalbert 的,但仍然无法正常工作。

The way I solved it is adding "skip-grant-tables" in /etc/my.cnfthen start mysql service and connecting mysql with "mysql -u root" as https://www.codero.com/knowledge-base/content/33/296/en/how-to-reset-your-root-mysql-password.html

我解决它的方法是在/etc/my.cnf 中添加“ skip-grant-tables”,然后启动 mysql 服务并将 mysql 与“mysql -u root”连接为 https://www.codero.com/knowledge-base/ content/33/296/en/how-to-reset-your-root-mysql-password.html

It works in my VM CentOS 7.

它适用于我的 VM CentOS 7。