mysql的登陆方式有两种,分别是socket和tcp/ip方式登陆。
Socket(只能用于本地连接)
当server和client在同一台服务器上的时候,我们可以直接用mysql命令登陆。
[root@localhost ~]# /usr/local/mysql55/bin/mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.60-log Source distribution
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> status
--------------
/usr/local/mysql55/bin/mysql Ver 14.14 Distrib 5.5.60, for Linux (x86_64) using readline 5.1
Connection id: 3
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.5.60-log Source distribution
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /tmp/mysql.sock
Uptime: 21 hours 40 min 17 sec
当什么参数都没有指定的时候,mysql默认使用socket方式登陆,如果/etc/my.cnf的[client]没有指定socket文件路径时,mysql默认会去寻找/tmp/mysql.sock,所以如果mysql服务启动的时候,生成的socket文件不是默认路径的话,登陆会报错(ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock')。
[root@localhost ~]# cat /etc/my.cnf.bak
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
mysql服务启动的时候,socket文件的生成目录可以在[mysqld]上指定,如果没有指定,默认是/tmp/mysql.sock。
TCP/IP(适合远程连接,本地也可以使用)
mysql登陆的时候,指定参数-h,会使用tcp/ip的方式连接,如果没有指定端口的话,默认是使用3306端口
[root@localhost ~]# /usr/local/mysql55/bin/mysql -h127.0.0.1 ----如果是两台服务器,在另外一台服务器需要连接该台服务器就要使用TCP/IP,socket就无法使用了
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.5.60-log Source distribution
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> status
--------------
/usr/local/mysql55/bin/mysql Ver 14.14 Distrib 5.5.60, for Linux (x86_64) using readline 5.1
Connection id: 4
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.5.60-log Source distribution
Protocol version: 10
Connection: 127.0.0.1 via TCP/IP
Socket和Tcp/ip
当mysql登陆时,同时指定-h和-S,mysql会默认使用tcp/ip的方式连接。
[root@localhost ~]# /usr/local/mysql55/bin/mysql -h127.0.0.1 -S /tmp/mysql.sock
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.60-log Source distribution
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> status
--------------
/usr/local/mysql55/bin/mysql Ver 14.14 Distrib 5.5.60, for Linux (x86_64) using readline 5.1
Connection id: 5
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.5.60-log Source distribution
Protocol version: 10
Connection: 127.0.0.1 via TCP/IP