ubuntu20.04安装MySQL并修改登录密码全过程

直接pip3 install mysqlclient会报错,装上之后由于不知道初始密码所以sudo -u root -p 也登录不上,今天总结一下解决这些问题的全过程。

第一步:安装mysqlclient

先安装依赖,执行命令:

sudo apt-get install libmysqlclient-dev

然后安装mysqlclient:

pip3 install mysqlclient

这样mysqlclient就安装好了,但它设置了初始密码,我们不知道,所以登录不上去,如下报错:

$ mysql -u root -p
Enter password: 
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

第二步:修改密码

先进入root模式:

sudo -s

然后输入命令mysql

root@ubuntu:/home/cdd/Desktop/Django/typeidea-env/typeidea/typeidea# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 8.0.21-0ubuntu0.20.04.4 (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>

下面就是修改初始密码的步骤:
现在mysql命令下输入

mysql> use mysql

然后修改密码:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY '你的新密码';

到这里新密码就设置好了,下面我们登录下:

$ mysql -u root -p
Enter password:    # 这里输入你的新密码
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 28
Server version: 8.0.21-0ubuntu0.20.04.4 (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>

登录成功!

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值