博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MySQL in Action(持续更新中...)
阅读量:4189 次
发布时间:2019-05-26

本文共 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;

 

日期与时间

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

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

select now();

获取当前MySQL服务器的时间

select current_time;

 

取消命令执行

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

\c

使用示例

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

mysql> select schemas\c

 

转载地址:http://hcsoi.baihongyu.com/

你可能感兴趣的文章
Linux网络运维 -- 配置DHCP服务器
查看>>
Android开发问题记录
查看>>
Verilog编程网站学习——门电路、组合电路、时序电路
查看>>
android——学生信息显示和添加
查看>>
Android——ImageSwitcher轮流显示动画
查看>>
Android——利用手机端的文件存储和SQLite实现一个拍照图片管理系统
查看>>
图像调优1:清晰度相关参数MTF,SFR,MTF50,MTF50P 以及TVL的概念以及换算说明
查看>>
图像调优3: CCM参数的标定
查看>>
ctags在verilog代码浏览中的应用
查看>>
NeoVintageous 在sublime中的使用
查看>>
用ncverilog跑仿真时,如何去除对特定路径的timing检查
查看>>
在ncverilog仿真条件设置中+nospecify ,+notimingcheck 和 +delay_mode_zero之间有什么区别
查看>>
linux下nerdtree安装方法
查看>>
最长回文子串(Go,LeetCode)
查看>>
windows下TortoiseGit安装和使用的图文教程
查看>>
基于Jquery的(可视区域,向上滚动向下滚动两种)图片懒加载
查看>>
原生JS的(可视区域,向上滚动向下滚动两种)图片懒加载
查看>>
使用VMware搭建Hadoop集群虚拟网络配置
查看>>
解决vmware下拷贝主机后不识别eth0网卡
查看>>
Promise简单实践
查看>>