直接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>
登录成功!