Ubuntu 22.04 LTS 安装 MySQL8.0.29 重置 root 密码
适用版本
mysql> status
--------------
mysql Ver 8.0.29-0ubuntu0.22.04.2 for Linux on x86_64 ((Ubuntu))
第一步:在终端使用 apt 安装 mysql-server(如果已经安装可以忽略)
dademiao@ddm-ubuntu:~$ sudo apt install mysql-server
第二步:在终端查看 mysql 服务开启的状态,应该是 active(如果已经运行可以忽略)
dademiao@ddm-ubuntu:~$ systemctl status mysql.service
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2022-07-21 14:15:59 CST; 21min ago
Process: 7275 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCC>
Main PID: 7283 (mysqld)
Status: "Server is operational"
Tasks: 38 (limit: 9316)
Memory: 363.9M
CPU: 4.294s
CGroup: /system.slice/mysql.service
└─7283 /usr/sbin/mysqld
7月 21 14:15:58 ddm-ubuntu systemd[1]: Starting MySQL Community Server...
7月 21 14:15:59 ddm-ubuntu systemd[1]: Started MySQL Community Server.
第三步:安装完毕后,默认 root 不需要用密码即可登录,如果登录不了,可以使用第四步和第五步来登录mysql。
dademiao@ddm-ubuntu:~$ sudo mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.29-0ubuntu0.22.04.2 (Ubuntu)
第四步:查看 mysql 提供的默认用户名和密码(如果可以使用第三步登录,就不用这一步)
dademiao@ddm-ubuntu:~$ sudo cat -n /etc/mysql/debian.cnf
[sudo] dademiao 的密码:
1 # Automatically generated for Debian scripts. DO NOT TOUCH!
2 [client]
3 host = localhost
4 user = debian-sys-maint
5 password = Prw**********X6b
6 socket = /var/run/mysqld/mysqld.sock
7 [mysql_upgrade]
8 host = localhost
9 user = debian-sys-maint
10 password = Prw**********X6b
11 socket = /var/run/mysqld/mysqld.sock
默认用户名:debian-sys-maint(仅是范例,需使用自己运行相关命令后,得到的实际用户名)
默认密码:Prw**********X6b(仅是范例,需使用自己运行相关命令后,得到的实际密码)
第五步:使用第三步得到的 默认用户名 和 密码 登录 mysql(如果可以使用第三步登录,就不用这一步)
dademiao@ddm-ubuntu:/etc/mysql$ mysql -u debian-sys-maint -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.29-0ubuntu0.22.04.2 (Ubuntu)
第六步:选择 mysql 数据库
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
第七步:把 root 用户的授权插件修改为:mysql_native_password,并检查
mysql> update user set plugin="mysql_native_password" where user='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select user, plugin from user;
+------------------+-----------------------+
| user | plugin |
+------------------+-----------------------+
| debian-sys-maint | caching_sha2_password |
| mysql.infoschema | caching_sha2_password |
| mysql.session | caching_sha2_password |
| mysql.sys | caching_sha2_password |
| root | mysql_native_password |
+------------------+-----------------------+
5 rows in set (0.00 sec)
如果使用:select user, plugin from user; 查询到 root 用户的授权插件为 mysql_native_password,则无需执行第七步。
第八步:重置 root 密码,此处密码设置为 Dademiao2014# (请根据自己的需要修改)
mysql> alter user 'root'@'localhost' identified with mysql_native_password by 'Dademiao2014#';
Query OK, 0 rows affected (0.01 sec)
第九步:刷新授权并退出 mysql
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> exit
Bye
第十步:用新设定的 root 密码尝试登录 mysql
dademiao@ddm-ubuntu:/etc/mysql$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.29-0ubuntu0.22.04.2 (Ubuntu)
©重庆睿一网络科技有限公司