命令行登录 MySQL

你可以按照以下步骤在 Windows 或 Linux/CentOS 终端中登录 MySQL:


1. Windows 登录 MySQL

方法 1:使用 mysql -u root -p
mysql -u root -p
  • -u root 表示使用 root 用户登录
  • -p 表示提示输入密码

如果 MySQL 运行在远程服务器,可以加 -h 指定 IP:

mysql -h 192.168.1.100 -u root -p

方法 2:指定端口

如果 MySQL 运行在 非默认端口(3306),需要加 -P 选项:

mysql -u root -p -P 3307

2. Linux / CentOS 登录 MySQL

方法 1:登录 MySQL
mysql -u root -p

输入密码后即可进入 MySQL 命令行。


方法 2:如果 MySQL 服务未启动

先启动 MySQL:

systemctl start mysqld

或者:

service mysqld start

然后再用 mysql -u root -p 登录。


3. 登录 MySQL 失败怎么办?

情况 1:忘记了 root 密码

可以 重置 MySQL 密码

ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';
FLUSH PRIVILEGES;

情况 2:MySQL 服务未启动

检查 MySQL 是否运行:

systemctl status mysqld

如果没有运行,启动 MySQL:

systemctl start mysqld

情况 3:找不到 mysql 命令

可能 MySQL 没有加入 PATH,可以尝试:

"C:\Program Files\MySQL\MySQL Server 8.0\bin\mysql.exe" -u root -p

或者:

/usr/bin/mysql -u root -p

4. 退出 MySQL

在 MySQL 命令行中输入:

exit;

或者:

quit;

这样,你就可以顺利在命令行登录 MySQL 了!🚀


C:\Users\20120>mysql -u root -p
Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.31 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>
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐