MySQL by Windows PowerShell

MySQL by Windows PowerShell

December 6, 2022 ·XUE
XUE

Windows PowerShell 访问 MySQL

使用 Windows Server 2019 服务器安装 MySQL,下面是安装后通过 PowerShell 进入 MySQL 时遇到的问题和解决方法。

加入系统环境变量

Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

PS C:\Users\Administrator> cd D:\BtSoft\mysql\MySQL5.7\bin
PS D:\BtSoft\mysql\MySQL5.7\bin> mysql -u root -p
mysql : 无法将“mysql”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正
确,然后再试一次。
所在位置 行:1 字符: 1
+ mysql -u root -p
+ ~~~~~
    + CategoryInfo          : ObjectNotFound: (mysql:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Suggestion [3,General]: 找不到命令 mysql,但它确实存在于当前位置。默认情况下,Windows PowerShell 不会从当前位置加载命令 。如果信任此命令,请改为键入“.\mysql”。有关详细信息,请参阅 "get-help about_Command_Precedence"

百度检索,查找第一方法 Ⅰ:是否是 MySQL 因为没有安装在系统盘,没有加入环境变量导致。通过GUI界面加入系统环境变量后重试

PS D:\BtSoft\mysql\MySQL5.7\bin> mysql -u root -p
mysql : 无法将“mysql”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正
确,然后再试一次。
所在位置 行:1 字符: 1
+ mysql -u root -p
+ ~~~~~
    + CategoryInfo          : ObjectNotFound: (mysql:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Suggestion [3,General]: 找不到命令 mysql,但它确实存在于当前位置。默认情况下,Windows PowerShell 不会从当前位置加载命令 。如果信任此命令,请改为键入“.\mysql”。有关详细信息,请参阅 "get-help about_Command_Precedence"

加入 MySQL 的 bin 文件路径后,重试仍然报错。再次查看系统环境变量,发现 MySQL 路径消失

尝试再次安装 MySQL

查找第二方法 Ⅱ:尝试命令 .\mysqld.exe install

PS D:\BtSoft\mysql\MySQL5.7\bin> .\mysqld.exe install
The service already exists!
The current server installed: "D:\BtSoft\mysql\MySQL5.7\bin\mysqld.exe" --defaults-file=D:\BtSoft\mysql\MySQL5.7\my.ini MySQL

提示已经安装,无法解决

更改命令语法

再次查找方法 Ⅲ:在 Windows PowerShell 中,要运行位于当前目录下的脚本,请指定完整路径,或者键入句点 (.) 表示当前目录。(加句点时注意目标路径是否为当前目录)

默认情况下,Windows PowerShell 不会从当前位置加载命令 。如果信任此命令,请改为键入“.\mysql”。cmd命令行窗口不存在此问题。参考

综上,更改命令测试。

PS D:\BtSoft\mysql\MySQL5.7\bin> .\mysql -u root -p
Enter password: *****
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
PS D:\BtSoft\mysql\MySQL5.7\bin> create database wordpress;
create : 无法将“create”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径
正确,然后再试一次。
所在位置 行:1 字符: 1
+ create database wordpress;
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (create:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

成功。因此推测不是因为缺少环境变量设置导致,是由于ps的特性所导致,需要更改命令,加入句点。

获取默认密码

可以进入 MySQL,但是密码错误,无法获得管理员权限

百度搜索宝塔面板 MySQL 的默认密码。在数据库中成功找到 root 默认密码

PS D:\BtSoft\mysql\MySQL5.7\bin> .\mysql -u root -p
Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 30
Server version: 5.7.38-log MySQL Community Server (GPL)

Copyright (c) 2000, 2022, 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>

成功进入MySQL

第一次使用服务器,服务器为阿里云ECS,害怕不会用Linux,系统选择了Windows Server 2019

在ECS中安装宝塔面板,无法安装在C盘,只能安装在D盘

使用宝塔面板安装MySQL,安装后无法进入MySQL,看不懂错误提示,绕了好大一圈

最后编辑于20230713

Last updated on