ubuntu 19.10下MySQL 8.0.19安装后密码修改

本文介绍在Ubuntu19.10环境下,如何解决MySQL8.0.19安装后默认密码未知的问题,通过官方文档指导,获取初始账号及密码,完成密码修改。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ubuntu 19.10下MySQL 8.0.19安装后密码修改

本文背景

这个问题昨晚折腾了我一晚,原因是开始学习《Python3网络爬虫开发实战》这本书。在环境安装这一章一路畅通无阻,到了mysql之后就卡住了,因为安装过程没有提示输入密码。参考了CSDN各种的修改密码博客都没办法,方法都不对。有的跟我一样的版本号,给的语句在本机输入后显示格式错误。后面不得不查阅了官方文档。纪念一下第二次查官方。

mysql的安装

系统为ubuntu19.10,mysql 8.0.19

sudo apt-get update
sudo apt-get install -y mysql-server mysql-client

安装过程未提示输入用户名和密码

相关命令

首先给出服务启动,停止,重启的命令,后面要用:

sudo service mysql start
sudo service mysql stop
sudo service mysql restart

查阅文档

文档地址

第一步

A password may already be assigned to the initial account under these circumstances:

  • On Windows, installations performed using MySQL Installer give you the option of assigning a password.
  • Installation using the macOS installer generates an initial random password, which the installer displays to the user in a dialog box.
  • Installation using RPM packages generates an initial random password, which is written to the server error log.
  • Installations using Debian packages give you the option of assigning a password.
  • For data directory initialization performed manually using mysqld --initialize, mysqld generates an initial random password, marks it expired, and writes it to the server error log. See Section 2.10.1, “Initializing the Data Directory”.

从上面我们可以看出,使用apt安装的mysql是有初始密码的。

If you do not know the initial random password, look in the server error log.

接下来我们查阅系统分配的初始账号和密码

sudo gedit /etc/mysql/debian.cnf 

会显示:

# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = debian-sys-maint
password = JSHJAHJZBxsajxns
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = JSHJAHJZBxsajxns
socket   = /var/run/mysqld/mysqld.sock

user后面就是你的账户名debian-sys-maint

password后面就是你的密码JSHJAHJZBxsajxns

Connect to the server as root using the password:

shell> mysql -u root -p
Enter password: (enter the random root password here)

接下来我们用它登录

sudo -udebian-sys-maint -pJSHJAHJZBxsajxns

登录成功会显示:

mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.19-0ubuntu0.19.10.3 (Ubuntu)

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> 

这样就登录进去了。

第二步

我们继续看文档

Choose a new password to replace the random password:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root-password';

按人家说的做在mysql中输入语句,把你自己的密码替换掉root-password

ALTER USER 'root'@'localhost' IDENTIFIED BY 'root-password';

最后输入exit;退出即可

exit;

现在试试登录应该就可以了。

额外说明

如果登录出现下面报错:

ERROR 1698 (28000): Access denied for user 'mrcangye'@'localhost'

那么在登录时,语句前面加sudo就可以登录了

出现这个问题原因是root用户使用的是auth_socket,我们要把它改成caching_sha2_password

我们先进入mysql

sudo -udebian-sys-maint -pJSHJAHJZBxsajxns

登录成功后输入以下以下语句:

ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'root-password';

用你自己的密码替换掉root-password

最后exit;退出即可

exit;

再试试登录mysql应该就正常了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈沧夜

打个赏,让我买瓶可乐喝呗~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值