Hab gerade einen ssh Tunnel folgendermaßen eingerichtet:
ssh -L 3333:localhost:3306 -p 10022 root@remotehost
Dann wollte ich mich mit der MySQL Datenbank verbinden, hab aber immer nur die lokale Datenbank erreichen können. Ich hab schon vermutet, dass mysql den Portparameter (-P 3333 oder –port=3333) nicht auswertet, da es sich immer nur mit der lokalen DB verbindet. Nach längerer Zeit habe ich 127.0.0.1 ausprobiert und es hat funktioniert. Aber warum ist localhost nicht 127.0.0.1?
klaus@ubuntu:~$ mysql -u groupoffice-com -P 3333 --password=passwort -h 127.0.0.1
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 443
Server version: 5.5.22-0ubuntu1 (Ubuntu)
Copyright (c) 2000, 2011, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| groupofficecom |
| mysql |
| performance_schema |
| phpmyadmin |
+--------------------+
5 rows in set (0.03 sec)
mysql> quit
Bye
klaus@ubuntu:~$ mysql -u groupoffice-com -P 3333 --password=passwort -h localhost
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 234
Server version: 5.1.62-0ubuntu0.10.04.1 (Ubuntu)
Copyright (c) 2000, 2011, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| test2 |
+--------------------+
2 rows in set (0.00 sec)
mysql> quit
Kennt jemand dieses seltsame Phänomen?
Erklärung
Herr A.S. (Vielen Dank!) in den Kommentaren lieferte die Erklärung. Bei Angabe von localhost wird immer der Unix Socket verwendet, selbst wenn der Port mit angegeben ist. Will man eine TCP/IP Verbindung, muss man explizit das Protokoll angeben --protocol=TCP oder die lokale IP Adresse 127.0.0.1 verwenden.
Von Connecting to the MySQL Server (Kapitel ist nicht ins Deutsche übersetzt):
On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by using the --protocol=TCP option. For example:
shell> mysql --host=127.0.0.1
shell> mysql --protocol=TCP
The --protocol option enables you to establish a particular type of connection even when the other options would normally default to some other protocol.
Hinweis: Die PHP Funktion mysql-connect verhält sich genauso, ist aber nicht so ausführlich dokumentiert.