mysql -h hostname|hostlP -p port -u username -p DatabaseName -e "SQL语句"
对上述参数说明如下: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”指定了登录成功后要使用的数据库。
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 表的基本结构。
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> QUIT; Bye
本文链接:http://task.lmcjl.com/news/18035.html