MySQL的两种登录方式

本文介绍了MySQL的两种登录方式:TCP/IP和Socket。TCP/IP方式常见,而Socket适用于本地多实例通信,速度更快。当需要连接未配置的MySQL实例时,可使用Socket通过mysql.sock文件路径登录。

        相信很多小伙伴登录Mysql的时候,毫不犹豫使用账号、密码(即:TCP/IP)方式登录。今天和大家分享的是另一种登录方式:socket方式登录。

        首先,二者的区别:常规的账号密码登录也就是TCP/IP方式登录;而socket其实就是利用mysql.sock作为主机和客户机在同一台机器上使用unix domain socket作为通信载体进行通信的,其速度比TCP/IP要快很多。

        其次,socket使用场景:当一台主机上因业务需要或者其他的设计、规划需要安装了多个mysql时(Mysql安装路径和端口不同即可),由于环境变量中同一时只能配置一个mysql的指向或软连接,如果需要登录未配置的mysql就可以使用socket方式了。

        最后,两种登录方式的具体操作示例如下:

第一种登录方式(TCP/IP):

语法:mysql -h IP地址 -u 用户 -p 密码 -P 端口

本机操作-h和-P可以忽略,远程操作时-h不能忽略,否则无法访问

[root@iZm5egizeg7ei92a04dhb3Z mysql]# mysql -h127.0.0.1 -uroot -p -P3306
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 32057
Server version: 5.7.27-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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.sock登录):

语法:mysql -S mysql.sock路径

如果不确定mysql.sock文件路径,可以通过“find / -name mysql.sock"来查找,一般在mysql安装目录下

[root@iZm5egizeg7ei92a04dhb3Z mysql]# find / -name mysql.sock
/usr/local/mysql/mysql.sock
/opt/zbox/tmp/mysql/mysql.sock
[root@iZm5egizeg7ei92a04dhb3Z mysql]# mysql -S /opt/zbox/tmp/mysql/mysql.sock
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1063
Server version: 5.5.5-10.5.5-MariaDB Source distribution

Copyright (c) 2000, 2019, 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的两种登录方式的简单介绍。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值