MySQL连接两种方式

Mysql有两种连接方式,这里主要说明下linux类平台:
 
 1,TCP/IP
  此种外部方式连接,必须指定用户名,密码,端口号(如果默认端口号时可以不要)

[root@centos7 ~]# mysql -h192.168.97.189 -uroot -p -P3307    
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.15 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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> status
--------------
mysql  Ver 14.14 Distrib 5.7.15, for Linux (x86_64) using  EditLine wrapper

Connection id:          6
Current database:
Current user:           loge@centos7
SSL:                    Cipher in use is DHE-RSA-AES256-SHA
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         5.7.15 MySQL Community Server (GPL)
Protocol version:       10
Connection:             192.168.97.189 via TCP/IP
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    utf8
Conn.  characterset:    utf8
TCP port:               3307
Uptime:                 3 min 50 sec

Threads: 2  Questions: 13  Slow queries: 0  Opens: 112  Flush tables: 1  Open tables: 105  Queries per second avg: 0.056
--------------

mysql>


 2 ,socket
对mysql.sock来说,其作用是程序与mysqlserver处于同一台机器,只对发起本地连接时可用。
你无须定义连接host的具体IP地址,只要为空或localhost就可以。在此种情况下,指定连接的端口不起作用,比如配置文件是3307,可以不指定端口也能连接。并且再次连接中是用localhost登录的,比如root@localhost,从此可以看出localhost跟127.0.0.1是不同的,前者是走socket,后者走的是TCP/IP!需要注意的是在linux下才有socket。另外在mysql.user中的host默认是localhost,也就是安装完登录时候是需要用socket方式登录哈!
因为你在my.ini中或my.cnf中改变端口后,mysql.sock是随每一次 mysql server启动生成的。已经根据你在更改完my.cnf后重启mysql时重新生成了一次,信息已跟着变更。
那么对于外部连接,必须是要变更port才能连接的。

 

[root@centos7 ~]# mysql -uroot -p -S /tmp/mysql.sock
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.15 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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> status
--------------
mysql  Ver 14.14 Distrib 5.7.15, for Linux (x86_64) using  EditLine wrapper

Connection id:          3
Current database:
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         5.7.15 MySQL Community Server (GPL)
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:            /tmp/mysql.sock
Uptime:                 47 sec

Threads: 1  Questions: 7  Slow queries: 0  Opens: 105  Flush tables: 1  Open tables: 98  Queries per second avg: 0.148
--------------

mysql>

 
另外,linux下安装mysql连接的时候经常回提示说找不到mysql.sock文件,解决办法很简单:
如果是新安装的mysql,提示找不到文件,就用find搜索下,指定正确的位置或者使用命令 mysql --verbose --help | grep socket 看看是否找到
如果mysql.sock文件误删的话,就需要重启mysql服务,如果重启成功的话会在datadir或者指定目录下面生成mysql.sock,然后再配置my.cnf。
windows下还支持管道连接、SHARED MEMORY方式,但不支持socket方式。

如果是在本机访问数据库最好是使用socket,因为不占用网卡资源,效率高

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

朝闻道-夕死可矣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值