Ubuntu18 重置MySQL8 root密码

问题

购买云服务器后,选择了带MySQL环境的镜像,查看日志知道初始密码是空,但是使用’mysql -u root --skip-password’ 无法登陆数据库。
官方参考:2.10.4 Securing the Initial MySQL Account

# 查看MySQL安装日志
root@VM-0-16-ubuntu:/data# head /var/log/mysql/error.log|grep -E 'root' --color=auto
2020-02-29T14:35:51.914062Z 5 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

解决方案

操作环境:Ubuntu 18.04 + MySQL 8.0.19

方案1 跳过权限验证

命令摘要:

mysqld --user=mysql --skip-grant-tables
mysql
mysql> flush privileges;
mysql> alter user 'root'@'localhost' identified by '123456';

详细步骤:

  1. 跳过权限验证方式启动MySQL服务
root@VM-0-16-ubuntu:/data# mysqld --user=mysql --skip-grant-tables

启动成功后的日志信息(ready for connections):

root@VM-0-16-ubuntu:/data# tail -f /var/log/mysql/error.log
2020-05-25T11:13:00.156006Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.19'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  MySQL Community Server - GPL.
  1. 免密登录mysql并修改密码
# 新建一个命令行窗口,执行以下命令
root@VM-0-16-ubuntu:/etc/mysql# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.19 MySQL Community Server - GPL
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
#由于MySQL启动时没有开启权限验证,必须先执行此命令,才能修改权限
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.01 sec)
mysql> exit;
Bye
  1. 正常启动MySQL服务并使用密码登录
root@VM-0-16-ubuntu:/data# systemctl start mysql
root@VM-0-16-ubuntu:/etc/mysql# mysql -uroot -p
Enter password: 

方案2:定义初始化文件(未成功)

官方文档:B.4.3.2 How to Reset the Root Password
根据我自己的环境,做了以下操作:

  1. 查看并停止MySQL服务
root@VM-0-16-ubuntu:/data# systemctl status mysql
● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2020-05-25 17:25:58 CST; 32min ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 13051 ExecStartPre=/usr/share/mysql-8.0/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
 Main PID: 13101 (mysqld)
   Status: "Server is operational"
    Tasks: 40 (limit: 2124)
   CGroup: /system.slice/mysql.service
           └─13101 /usr/sbin/mysqld

May 25 17:25:57 VM-0-16-ubuntu systemd[1]: mysql.service: Service hold-off time over, scheduling restart.
May 25 17:25:57 VM-0-16-ubuntu systemd[1]: mysql.service: Scheduled restart job, restart counter is at 2.
May 25 17:25:57 VM-0-16-ubuntu systemd[1]: Stopped MySQL Community Server.
May 25 17:25:57 VM-0-16-ubuntu systemd[1]: Starting MySQL Community Server...
May 25 17:25:58 VM-0-16-ubuntu systemd[1]: Started MySQL Community Server.
root@VM-0-16-ubuntu:/data# systemctl stop mysql
  1. 新建密码初始化文件
root@VM-0-16-ubuntu:/data# vim mysql-init
# mysql-init文件中内容如下:ALTER USER 'root'@'localhost' IDENTIFIED BY 'root1#ROOT';
  1. 启动MySQL服务
root@VM-0-16-ubuntu:~# mysqld --user=mysql --init-file=/data/mysql-init

错误日志:

root@VM-0-16-ubuntu:/data# tail -f /var/log/mysql/error.log
2020-05-25T10:07:27.487143Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.19) starting as process 19048
2020-05-25T10:07:28.185432Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2020-05-25T10:07:28.206236Z 0 [ERROR] [MY-013129] [Server] A message intended for a client cannot be sent there as no client-session is attached. Therefore, we're sending the information to the error-log instead: MY-000029 - File '/data/mysql-init' not found (OS errno 13 - Permission denied)
2020-05-25T10:07:28.206642Z 0 [ERROR] [MY-010455] [Server] Failed to open the bootstrap file /data/mysql-init
2020-05-25T10:07:28.206711Z 0 [ERROR] [MY-010119] [Server] Aborting
2020-05-25T10:07:29.609279Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.19)  MySQL Community Server - GPL.

排查:
错误信息中说找不到/data/mysql-init文件(权限拒绝),官方文档说要确保mysql用户有对mysql-init的读权限,这一点通过chown命令已经确保了。虽然也做了其他尝试,但仍未解决该问题。

root@VM-0-16-ubuntu:~# chown mysql:mysql /data/mysql-init 
root@VM-0-16-ubuntu:~# ll /data/mysql-init 
-rw-r--r-- 1 mysql mysql 58 May 25 17:07 /data/mysql-init

你可能感兴趣的:(Ubuntu18 重置MySQL8 root密码)