MySQL连接两种方式

Mysql有两种连接方式:
 
 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,后者走的是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下还支持管道连接方式。

 

你可能感兴趣的:(--,MySQL安装配置)