本文共 4921 字,大约阅读时间需要 16 分钟。
目录
mysql -u-p[ ] [-D ] [-P ] [-h ] [--prompt=" "]
参数 | 值 | 描述 |
-u | username | 登录的用户名 |
-p | password | 登录的密码。值可省略,若不省略password,表示明文密码登录 |
-D | database | 可选参数。表示登录后进入指定的数据库 |
-P | port | 可选参数。指定MySQL端口号登录 |
-h | host | 可选参数。指定主机名登录(通常用于远程登录MySQL服务器) |
--prompt | prompt | 可选参数。指定登录提示符 |
推荐的方式是使用用户名登录。
mysql -u-p
例如,使用用户名为root的用户登录:
$ mysql -u root -pEnter password:
此时会有回显提示你继续输入密码。输入的密码不会显示在Console上。
⚠️如果密码输入错误,MySQL报错:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
使用明文密码登录可以避免二次输入密码。
mysql -u-p
例如,使用用户名为root,密码为root的账户登录:
mysql -u root -proot
💡参数-p和明文密码之间不能出现空白符。
⚠️当指定的明文密码错误时,MySQL报错 RROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
⚠️使用明文密码登录MySQL缺乏安全性,不推荐使用这种方法登录MySQL。
可以使用-h参数指定主机ip登录MySQL。这样我们可以远程登录其它服务器上的MySQL数据库。
mysql -u-p -h
例如远程登录另一台主机的MySQL数据库:
$ mysql -u root -p -h 192.168.0.100Enter password: Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 13Server version: 8.0.21 HomebrewCopyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>
如果出现连接被拒绝的情况,可以参考(适用于Mac MySQL 8.0+)。
使用-P参数指定端口号登录。默认情况下端口号为3306。
mysql -u-p -P
$ mysql -u root -p -P 3306Enter password:Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 22Server version: 8.0.21 HomebrewCopyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>
默认情况下,登录后MySQL的命令提示符是"mysql> ",我们可以自己定制命令提示符。使用--prompt参数来自定义命令提示符。例如我们将命令提示符定义为"$ ":
$ mysql -u root -p --prompt="$ "Enter password:Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 24Server version: 8.0.21 HomebrewCopyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.$
MySQL提供了一些预定义命令提示符,供使用者组合使用:
提示符 | 说明 |
\h | 显示当前登录的主机 |
这些MySQL预定义的命令提示符可以自由组合,选择性的使用:
$ mysql -u root -p --prompt="host[\h]: "Enter password:Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 25Server version: 8.0.21 HomebrewCopyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.host[localhost]:
💡提示:自定义命令提示符仅在当次连接有效。
默认情况下数据库连接后,不会进入任何的数据库中。使用-D参数进入指定的数据库。
mysql -u-p -D
例如在登录成功后直接进入到sys数据库中:
$ mysql -u root -p --prompt="host[\h]: " -D sysEnter password:Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -AWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 26Server version: 8.0.21 HomebrewCopyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.host[localhost]:
⚠️注意:当指定的数据库不存在时,MySQL报错 ERROR 1049 (42000): Unknown database '<database>'
MySQL提供了3种方式退出当次连接:
exitquit\q
MySQL提供了三种方式来查看帮助文档:
help\h?
以#开头的语句在MySQL中被当作注释。
mysql> #helpmysql> #exitmysql> #quitmysql> # I am comment
使用 "--"作为注释时要注意,字符串"--" 与注释内容中要有一个空格符,否则MySQL会认为是一个SQL语句。
mysql> -- I am comment
使用系统函数version查询MySQL版本。
select version();
例如:
mysql> select version();+-----------+| version() |+-----------+| 8.0.21 |+-----------+1 row in set (0.01 sec)
使用系统函数user查询当前登录用户。
select user();
例如:
mysql> select user();+----------------+| user() |+----------------+| root@localhost |+----------------+1 row in set (0.00 sec)
当前登录用户是root。
show character set;
校对集是数据比较的方式,只有当数据产生比较的时候,校对集才会生效。
校对集有三种格式。_ci表示对大小写不敏感,例如utf8_hungarian_ci;_cs表示对大小写敏感,例如utf8mb4_zh_0900_as_cs;_bin表示二进制比较,例如armscii8_bin。
show collation;
调用系统函数now来获取当前MySQL服务器的日期时间:
select now();
select current_time;
在MySQL终端中,使用\c可以执行命令的执行。
\c
例如下面的例子中将取消执行查询数据库列表的操作:
mysql> select schemas\c
转载地址:http://hcsoi.baihongyu.com/