一、连接
要连接到服务器,通常需要提供 MySQL 用户和登录密码,如果是远程登录还需指定远程主机地址、端口等:
mysql -h host -u user -p
Enter password: ********
连接成功后,可以看到如下信息:
mysql -h localhost -u root -p
Enter password: *********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.33 MySQL Community Server - GPL
Copyright (c) 2000, 2023, 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 -u user -p
注意
若在连接登录时报错:
ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost:3306'
此时则需要查看你的MySQL服务是否启动。
二、断开连接
可以通过 \q
、exit
、quit
断开连接:
mysql> exit
Bye