登录MySQL服务器

时间:2024-04-14 19:56:41

简述

安装了数据库系统之后,可以配套安装一个图形化的操作应用,这种方式虽然操作起来直观易用,但是灵活度受到一定的限制。所以,尽管是数据库入门的新手,也应该学习使用命令行客户端操作数据库。

使用命令行操作数据库,首先需要登录MySQL服务器,进入mysql的操作命令行才能进行相关的操作。本篇博客以Windows操作系统安装的MySQL8.0数据库为例,讲解如何登录MySQL服务器。

前期准备

电脑已经安装了MySQL数据库,(远程登录除外)。MySQL8.0数据库安装过程参看命令行安装MySQL8.0.12数据库

操作步骤

打开Windows命令行(Win + R, 输入cmd)

Microsoft Windows [版本 10.0.17134.648]
(c) 2018 Microsoft Corporation。保留所有权利。

C:\Windows\system32>

进入mysql安装路径下的bin目录,如果配置过path环境变量,可以忽略这一步骤

Microsoft Windows [版本 10.0.17134.648]
(c) 2018 Microsoft Corporation。保留所有权利。

C:\Windows\system32>cd c:\database\mysql\bin

c:\DataBase\mysql\bin>

 输入登录命令,然后根据提示输入登录密码

mysql -hlocalhost -uroot -p

c:\DataBase\mysql\bin>mysql -hlocalhost -uroot -p
Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 67
Server version: 8.0.12 MySQL Community Server - GPL

Copyright (c) 2000, 2018, 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> ,表示已经登录成功。

有的用户在登录的时候,会遇到下面的提示:

 c:\DataBase\mysql\bin>mysql -hlocalhost -uroot -p
Enter password: ******
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

c:\DataBase\mysql\bin>

出现上面的提示,说明主机地址,用户名以及输入的密码不匹配,核对后重新登录即可。

登录命令中各个参数的意义

登录命令可以附带一些其他信息,完整的登录命令以及命令中参数所代表的意义如下图所示:

 登录MySQL服务器

 以mysql -hlocalhost -uroot  -p mysql -e"show tables"为例,运行结果如下所示:

c:\DataBase\mysql\bin>mysql -hlocalhost -uroot  -p mysql -e"show tables"
Enter password: ******
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| component                 |
| db                               |
| default_roles              |
| engine_cost               |
| func                            |
| general_log                |
| global_grants             |
| gtid_executed             |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| password_history          |
| plugin                    |
| procs_priv                |
| proxies_priv              |
| role_edges                |
| server_cost               |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+

c:\DataBase\mysql\bin>