MySQL 支持使用TCP/IP 协议以及Socket方式登录。前者支持大部分的方式,后者仅支持Linux或者Unix环境。
1、 TCP/IP 连接
TCP/IP 连接是在所有平台下都支持的连接方式。也是网络中均支持的方式。这种方式在客户端和服务端通过网络的方式进行通讯比如下面的场景就是使用TCP/IP 登录的示例。
root@5942ac265a6e:/var/lib/mysql/test# mysql -uroot -pEnter password:Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 179Server version: 8.0.18 MySQL Community Server - GPLCopyright (c) 2000, 2019, 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 首先会检查用户的权限,用来判断是否允许用户登录或者操作等等。从下表可以看出 root 用户允许在所有的设备上登录。
mysql> use mysql;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> select host,user from user;+-----------+------------------+| host | user |+-----------+------------------+| % | root || localhost | mysql.infoschema || localhost | mysql.session || localhost | mysql.sys || localhost | root |+-----------+------------------+5 rows in set (0.00 sec)
2、Socket 连接
在Linux环境或者Unix环境,MySQL 提供了Unix 套接字进行登录的方式,它仅能被用于在MySQL 客户端连接一台MySQL 服务器。
在MySQL 系统变量中查询Socket的位置,通过Socket文件位置来进行登录操作。
mysql> show variables like 'socket';+---------------+-----------------------------+| Variable_name | Value |+---------------+-----------------------------+| socket | /var/run/mysqld/mysqld.sock |+---------------+-----------------------------+1 row in set (0.02 sec)
获取到Socket的位置之后,可以使用命令 mysql -uroot -S /var/run/mysqld/mysqld.sock -p 的方式进行登录,登录的效果和TCP/IP 的方式类似。
root@5942ac265a6e:/var/lib/mysql/test# mysql -uroot -p -S /var/run/mysqld/mysqld.sockEnter password:Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 183Server version: 8.0.18 MySQL Community Server - GPLCopyright (c) 2000, 2019, 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>
3、GUI 客户端连接
GUI客户端连接的本质还是 TCP/IP 连接方式,这种连接给不是特别熟悉命令行操作的读者更便利的方式。通过GUI可以比较方便的进行 操作。
常见的MySQL 客户端有
3.1 Navicat
3.2 DataGrip
3.3 PhpMyAdmin
3.4 SQLyog
3.5 MySQL workbench