MySQL in Action(持续更新中...)

本文详细介绍了MySQL的登录方法,包括指定用户名、明文密码、主机、端口和自定义命令提示符。还涵盖了退出连接、查看帮助文档、注释使用、查询版本号、用户、字符集、校对集、日期时间以及取消命令执行等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

登录

命令格式

使用示例

指定用户名登录

使用明文密码登录

指定主机登录

指定端口号登录

自定义命令提示符

登录后进入指定的数据库

退出

查看帮助文档

注释

使用#注释

使用 -- 注释

查询版本号

查询当前登录用户

查询服务器支持的字符集

校对集

校对集格式

查看数据库支持的校对集

日期与时间

获取当前MySQL服务器的日期时间

获取当前MySQL服务器的时间

取消命令执行

使用示例


 

登录

命令格式

mysql -u<username> -p[<password>] [-D<database>] [-P<port>] [-h<host>] [--prompt="<prompt>"]
参数描述
-uusername登录的用户名
-ppassword登录的密码。值可省略,若不省略password,表示明文密码登录
-Ddatabase可选参数。表示登录后进入指定的数据库
-Pport可选参数。指定MySQL端口号登录
-hhost可选参数。指定主机名登录(通常用于远程登录MySQL服务器)
--promptprompt可选参数。指定登录提示符

使用示例

指定用户名登录

推荐的方式是使用用户名登录。

mysql -u <username> -p

例如,使用用户名为root的用户登录:

$ mysql -u root -p
Enter password:

此时会有回显提示你继续输入密码。输入的密码不会显示在Console上。

⚠️如果密码输入错误,MySQL报错:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

 

使用明文密码登录

使用明文密码登录可以避免二次输入密码。

mysql -u <username> -p<password>

例如,使用用户名为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 <username> -p -h <host>

例如远程登录另一台主机的MySQL数据库:

$ mysql -u root -p -h 192.168.0.100
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.21 Homebrew

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.

mysql>

如果出现连接被拒绝的情况,可以参考(适用于Mac MySQL 8.0+)Mac Homebrew安装的MySQL无法远程登录问题解决

 

指定端口号登录

使用-P参数指定端口号登录。默认情况下端口号为3306。

mysql -u <username> -p -P <port>
$ mysql -u root -p -P 3306
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 8.0.21 Homebrew

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.

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 24
Server version: 8.0.21 Homebrew

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.

$

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 25
Server version: 8.0.21 Homebrew

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.

host[localhost]:

💡提示:自定义命令提示符仅在当次连接有效。

 

登录后进入指定的数据库

默认情况下数据库连接后,不会进入任何的数据库中。使用-D参数进入指定的数据库。

mysql -u <username> -p -D <database>

例如在登录成功后直接进入到sys数据库中:

$ mysql -u root -p --prompt="host[\h]: " -D sys
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 26
Server version: 8.0.21 Homebrew

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.

host[localhost]:

⚠️注意:当指定的数据库不存在时,MySQL报错 ERROR 1049 (42000): Unknown database '<database>'

 

退出

MySQL提供了3种方式退出当次连接:

exit
quit
\q

 

查看帮助文档

MySQL提供了三种方式来查看帮助文档:

help
\h
?

 

注释

使用#注释

以#开头的语句在MySQL中被当作注释。

mysql> #help
mysql> #exit
mysql> #quit
mysql> # 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;

 

日期与时间

获取当前MySQL服务器的日期时间

调用系统函数now来获取当前MySQL服务器的日期时间:

select now();

获取当前MySQL服务器的时间

select current_time;

 

取消命令执行

在MySQL终端中,使用\c可以执行命令的执行。

\c

使用示例

例如下面的例子中将取消执行查询数据库列表的操作:

mysql> select schemas\c

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值