mysql -p用法详解
一、介绍
mysql -p
是一个MySQL命令行工具的选项。在使用该选项后,命令行会提示你输入密码,以便连接到MySQL服务器。本文将详细介绍mysql -p
的使用方法,并给出五个示例代码及运行结果。
二、使用方法
使用mysql -p
的基本用法如下:
mysql -p
然后,命令行将提示你输入密码:
Enter password:
输入正确的密码后,按下回车键即可连接到MySQL服务器。
三、示例代码及运行结果
示例一:连接到本地MySQL服务器
mysql -p
Enter password: **********
运行结果:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 123456.
Server version: 8.0.23 MySQL Community Server - GPL.
...
连接成功后,你可以开始在MySQL命令行中执行SQL语句了。
示例二:连接到指定主机的MySQL服务器
mysql -h hostname -p
Enter password: **********
运行结果:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 123456.
Server version: 8.0.23 MySQL Community Server - GPL.
...
这里,-h
选项后面跟着要连接的主机名。
示例三:连接到指定端口的MySQL服务器
mysql -P port -p
Enter password: **********
运行结果:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 123456.
Server version: 8.0.23 MySQL Community Server - GPL.
...
这里,-P
选项后面跟着要连接的端口号。
示例四:连接到指定数据库的MySQL服务器
mysql -p -D dbname
Enter password: **********
运行结果:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 123456.
Server version: 8.0.23 MySQL Community Server - GPL.
...
Database changed
这里,-D
选项后面跟着要连接的数据库名。
示例五:连接到指定用户的MySQL服务器
mysql -u username -p
Enter password: **********
运行结果:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 123456.
Server version: 8.0.23 MySQL Community Server - GPL.
...
这里,-u
选项后面跟着要连接的用户名。
四、总结
本文介绍了mysql -p
的用法,并给出了五个示例代码及运行结果。通过mysql -p
,你可以轻松地连接到MySQL服务器并执行SQL语句。