WSL / Linux安装MySQL(以及注意事项)
1. 安装 MySQL
# 安装apt
sudo apt install# 通过apt 安装MySQL
sudo apt update
sudo apt install mysql-server
2. 验证安装 / 注意事项
# 验证安装(二选一)
mysql --version
sudo mysql -u root#实例:
jh@DESKTOP-885GOSQ:/mnt/e/YumeWorkFiles/Aaaa-t/Abbb-t$ sudo mysql -u root
[sudo] password for jh:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 8.0.42-0ubuntu0.24.04.2 (Ubuntu)Copyright (c) 2000, 2025, Oracle and/or its affiliates.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>
以 root 身份运行 mysql 客户端:sudo mysql -u root(此处的 root 是 MySQL 用户名) ;
权限流程:
jh→ 输入自己的密码 → sudo授予root权限(此处的 root权限 是 linux 超级用户root权限) →mysql客户端以root身份连接数据库(此处的 root 是 MySQL用户名)
sudo mysql -u root 命令后输入的密码(即 [sudo] password for jh 的密码) ,
是操作系统用户 jh的 sudo 权限密码,用于获取执行命令的系统管理员权限 与 MySQL 无关,仅控制 Linux 系统的权限提升。
命令中的 -u root 指定了 MySQL 数据库的用户名(默认 root)(此处的 root 是 MySQL 用户名) ;
未通过 -p参数输入密码,表明 MySQL 当前允许本地免密登录(Ubuntu 常见默认行为)
3. 查看与设置 MySQL 账户密码
# Ubuntu/Debian 查看临时账户密码
# 实例:
jh@DESKTOP-885GOSQ:/mnt/e/YumeWorkFiles/Aaaa-t/Abbb-t$ sudo cat /etc/mysql/debian.cnf
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host = localhost
user = debian-sys-maint
password = 8tNGwHsRMserKEvV
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = localhost
user = debian-sys-maint
password = 8tNGwHsRMserKEvV
socket = /var/run/mysqld/mysqld.sock
# 设置root用户新密码,后续使用root用户密码登录MySQL
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password'
# 此处是设置MySQL的root用户的密码,设置后 debian-sys-maint用户 仍在
# debian-sys-maint:Debian/Ubuntu 系统创建的专属维护账户,用于服务重启、日志轮转等自动化任务;
# debian-sys-maint 与 root 是平行账户,具有与root用户同等的权限级别。
注释:
user = debian-sys-maint
password = 8tNGwHsRMserKEvV
实例中的用户/密码,以及设置的root用户/密码,与 phpstudy/xampp/宝塔 等等,
这类面板中的数据库的 用户/密码 是同一个东西。
4. 操作建议(验证 MySQL 密码状态)
# 验证 MySQL 密码状态:
# 登录 MySQL 后执行
SELECT user, authentication_string FROM mysql.user WHERE user='root';
# 若 authentication_string 为空,表明当前为免密登录# 实例:
jh@DESKTOP-885GOSQ:/mnt/e/YumeWorkFiles/Aaaa-t/Abbb-t/wp-testdemo/phpMyAdmin$ sudo mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 8.0.42-0ubuntu0.24.04.2 (Ubuntu)Copyright (c) 2000, 2025, Oracle and/or its affiliates.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> SELECT user, authentication_string FROM mysql.user WHERE user='root';
+------+-----------------------+
| user | authentication_string |
+------+-----------------------+
| root | |
+------+-----------------------+
1 row in set (0.00 sec)mysql>
5. 总结
凭证类型 | 作用对象 | 设置方式 | 示例 |
---|---|---|---|
操作系统 sudo 密码 | Linux 用户 jh | sudo passwd jh | [sudo] password for jh |
MySQL 用户名密码 | 数据库服务 | ALTER USER 'root'@'localhost' | 面板中“数据库用户/密码” |