mysql two connection methods: socket and TCP/IP

Refer to mysql connection method: socket and tcp/ip

On Unix systems, there are two ways to log in to MySQL, namely socket and tcp/ip login.

When the server and client are on the same server, we can log in directly with the mysql command.

[root@mysql ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.46-log MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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.

root@[(none)]>

When no parameters are specified, mysql uses socket login by default. If the [client] of my.cnf does not specify the socket file path, mysql will search for /tmp/mysql.sock by default, so if the mysql service starts , if the generated socket file is not the default path, the login will report an error.

[root@mysql ~]# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

When the mysql service is started, the socket file generation directory can be specified on [mysqld]. If not specified, the default is /tmp/mysql.sock ;

when mysql is logged in, specify the parameter -h, and the connection will be made by tcp/ip , if no port is specified, the default is to use port 3306

[root@mysql ~]# mysql -h192.1.1.20
ERROR 2003 (HY000): Can't connect to MySQL server on '192.1.1.20' (111)

[root@mysql ~]# mysql -h192.1.1.20 -P3307
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.46-log MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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.

root@[(none)]>


When mysql is logged in, specify -h and -S at the same time, mysql will use tcp/ip to connect by default.

[root@mysql ~]# mysql -h192.1.1.20 -P3307  -S /ddd/sdfs.scok
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.46-log MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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.

root@[(none)]>

You can log in by specifying a socket file.

 

You can view the concise version: MySQL connection method

More details can be viewed: http://blog.itpub.net/15480802/viewspace-1755100/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324393921&siteId=291194637