登录命令

isixe 2023-01-15 15:32:25
Categories: Tags:

语法

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 服务器。

 

 

 

登录到服务器

示例

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.

 

 

 

登录到数据库

示例

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.

 

 

 

 

 

登录到数据库并执行SQL语句

示例

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     |          |
+-------+---------------+------+------+-----------+-------+

 

 

 

来自 <http://c.biancheng.net/view/7480.html>