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
--portor -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use--hostor -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=TCPoption. For example:shell> mysql
--host=127.0.0.1
shell> mysql--protocol=TCP
The--protocoloption 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.
/etc/hosts enthält aber schon so was wie
===============
127.0.0.1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
===============
oder?
klaus@ubuntu:~$ cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 ubuntu.fritz.box ubuntu
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
klaus@ubuntu:~$
Sonst würde die Auflösung von localhost nicht funktionieren, oder?
Könnte das mit der /etc/hosts zu tun haben? Nur eine Schnellschuss Vermutung…
Unter Linux wird bei “localhost” automatisch versucht, über einen Unix-Socket die Verbindung aufzubauen. Wenn man das nicht will, muss man explizit das Protokoll angebben (“–protocol=TCP”). Siehe auch http://dev.mysql.com/doc/refman/5.0/en/connecting.html
Danke, genau das ist es! Da muss man erst mal drauf kommen.
Was ist hier los? Poste so einen Unfug bitte in irgendein Forum und drück nicht das Planetenniveau nach unten. Danke
Ich poste das, was mich gerade interessiert. Wenn ihnen das nicht passt, lesen sie meinen Blog einfach nicht!
Das ist der Planet von Ubuntuusers. Gewöhn dich dran, arrogant wie die sau, mich wollten sie garnicht nehmen.
Das ist einfach schade, und dann solche Kommentare wie dieser hier.
Der FREUNDLICHE Hinweis, dass der Artikel eher in ein Forum gehört, bzw. nicht in den Planet hätte ja auch genügt.
Ganz oft kommt es nicht darauf an, was man sagt, sondern wie man es sagt.
Also Namenloser(????), einfach freundlich bleiben. Und an den Admin: Ich kann schon verstehen, dass das nicht ganz passt, also mach doch einfach ne Kategorie für den Planet und sortiere sowas hier dann nicht ein.
Hi Markus,
im Moment des Schreibens fand ich den Artikel für meinen Blog passend. Mittlerweile, besonders wenn man die Lösung kennt, wäre es auch im Ubuntuusers Forum gut aufgehoben gewesen. Eventuell hilft es jemanden, der über das gleiche Problem stolpert.
Gruß Klaus