前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MySQL登录和退出服务器

MySQL登录和退出服务器

原创
作者头像
用户1685462
修改2021-08-27 17:54:12
3.6K0
修改2021-08-27 17:54:12
举报
文章被收录于专栏:网站教程

启动 MySQL 服务后,可以使用以下命令来登录。

mysql -h hostname|hostlP -p port -u username -p DatabaseName -e "SQL语句"

对上述参数说明如下:

  • -h:指定连接 MySQL 服务器的地址。可以用两种方式表示,hostname 为主机名,hostIP 为主机 IP 地址。
  • -p:指定连接 MySQL 服务器的端口号,port 为连接的端口号。MySQL 的默认端口号是 3306,因此如果不指定该参数,默认使用 3306 连接 MySQL 服务器。
  • -u:指定连接 MySQL 服务器的用户名,username 为用户名。
  • -p:提示输入密码,即提示 Enter password。
  • DatabaseName:指定连接到 MySQL 服务器后,登录到哪一个数据库中。如果没有指定,默认为 mysql 数据库。
  • -e:指定需要执行的 SQL 语句,登录 MySQL 服务器后执行这个 SQL 语句,然后退出 MySQL 服务器。
例 1

下面使用 root 用户登录到 test 数据库中,命令和运行过程如下:

代码语言:javascript
复制
C:\Users\11645>mysql -h localhost -u root -p test
Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.29-log MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

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.

上述命令中,通过值 localhost 指定 MySQL 服务器的地址,参数 -u 指定了登录 MySQL 服务器的用户账户,参数 -p 表示会出现输入密码提示信息,最后值“test”指定了登录成功后要使用的数据库。 由结果可以看到,输入命令后,会出现“Enter password”提示信息,在这条信息之后输入密码,然后按 Enter 键。密码正确后,就成功登录到 MySQL 服务器了。

例 2

下面使用 root 用户登录到自己计算机的 mysql 数据库,同时查询 student 表的表结构,命令和运行过程如下:

代码语言:javascript
复制
C:\Users\11645>mysql -h localhost -u root -p test -e"DESC student"
Enter password: ****
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(4)      | NO   | PRI | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
| age   | int(4)      | YES  |     | NULL    |       |
| stuno | int(11)     | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+

结果显示,执行命令并输入正确密码后,窗口中就会显示出 student 表的基本结构。 用户也可以直接在 mysql 命令的 -p 后加上登录密码,登录密码与 -p 之间没有空格。

例 3

下面使用 root 用户登录到自己计算机的 MySQL 服务器中,密码直接加在 mysql 命令中。命令如下:

代码语言:javascript
复制
C:\Users\11645>mysql -h localhost -u root -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.29-log MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

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.

上述命令执行后,后面不会提示输入密码。因为 -p 后面有密码,MySQL 会直接使用这个密码。 退出 MySQL 服务器的方式很简单,只要在命令行输入 EXIT 或 QUIT 即可。“\q”是 QUIT 的缩写,也可以用来退出 MySQL 服务器。退出后就会显示 Bye。如下所示:

代码语言:javascript
复制
mysql> QUIT;
Bye

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 例 1
  • 例 2
  • 例 3
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档