New on LowEndTalk? Please Register and read our Community Rules.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
why does PHP DB 127.0.0.1 works , but localhost doesn't work
Hi,
I have few sites running on a ubuntu 20 server on few different version of PHP
what bugs me is that , if I configure DB to 127.0.0.1
it works , however it breaks if I set to localhost
I have been googling around and confirm by phpinfo(); that mysqld.socket is set in correct position confirmed by netstat
commands
skip name resolve = OFF
skip networking = OFF
what else am I missing ?
Comments
localhost
may resolve to::1
.Is your MySQL listening on IPv6?
actually , yes , thanks for the hint , but I make it
bind-address = 0.0.0.0
or=127.0.0.1
doesn't work either , double checked withnetstat
yeah I think it's likely this too
none of these works with
localhost
You sure you are using TCP/IP when specifying localhost? I've already seen software that when you provide localhost it was trying to use socket rather than TCP/IP. Do you see it actually spitting errors about localhost:3306, not some /var/lib/mysql/blablabla
also check what does
localhost
actually resolves to https://www.php.net/manual/en/function.dns-get-record.php#refsect1-function.dns-get-record-examplesI think so
on my wp site , if I do
define( 'DB_HOST', 'localhost:/run/mysqld/mysqld.sock' );
it seems working , also with
127.0.0.1
, just notlocalhost
What the hell is this
I think it's tell wordpress to connect via that specific socket path
now it's interesting
Array ( [0] => Array ( [host] => localhost.mydomain.com [class] => IN [ttl] => 3789 [type] => HINFO [cpu] => RFC8482 [os] => ) )
somehow PHP takes
localhost
tolocalhost.mydomain.com
!?Most likely you don't have
localhost
specified in/etc/hosts
and it just search in domain provided.yeah , tried that , did not work , compared to other cpanel server, its PHP also gives similar result as
Array ( [0] => Array ( [host] => localhost.myotherdomain.com[class] => IN [ttl] => 3789 [type] => HINFO [cpu] => RFC8482 [os] => ) )
and I did try
/etc/hosts
with entry127.0.0.1 localhost.mydomain.com
as well as ther is an existing entriesand php gives
Array ( [0] => Array ( [host] => localhost.mydomain.com [class] => IN [ttl] => 0 [type] => A [ip] => 127.0.0.1 ) )
but still no joy
127.0.0.1
means TCP/IP connection to MySQL service.localhost
means you use UNIX socket with the connection.That's why
localhost:/run/mysqld/mysqld.sock
is working.I recommend to use
localhost
, which will be faster and more secure.If you wish to use TCP/IP, change to
127.0.0.1
instead.thanks for the tip, if you see the title, you will see I want to use localhost, but it doesn't work...
That's whole thread about...
Did you try
or
thanks for the hint , but both don't work...
Have you checked if your MySQL server is actually listening on the socket address?
yes, it is on socket
maybe check your mysql user permission see if it was allowed to login in from localhost?
for sure it does , even tried with
@%
Then socket file permission? Okay I have no idea now...
yes , if I explicitly use
localhost:/run/mysqld/mysqld.sock
in wordpress conf , it works , but for other non-wp CMS , this socket definition doesn't work andlocalhost
alone errors outthanks for trying , appreciated it
Let's experiment!
prepare: run multiple MySQL/MariaDB instances
1: mysql -h localhost -P (port other than first instance)
2: mysql -h 127.0.0.1 -P (port other than first instance)
SSH command line seems work for both localhost and 127.0.0.1
something more paculiar
it was working for
localhost
then I set up a master-slave replication and edit the my.cnf file , restart , start the replication , then it stopped working for
localhost
but only127.0.0.1
so today I grabbed another idling server, did same env , with exact same my.cnf , but it just still works on second server
what's the result of
ping localhost
?
binding to 0.0.0.0 doesn't automatically mean it also binds to ipv6.
on SSH 127.0.0.1
on PHP shell_exec
string(383) "PING localhost(localhost (::1)) 56 data bytes 64 bytes from localhost (::1): icmp_seq=1 ttl=64 time=0.021 ms 64 bytes from localhost (::1): icmp_seq=2 ttl=64 time=0.026 ms 64 bytes from localhost (::1): icmp_seq=3 ttl=64 time=0.027 ms --- localhost ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2035ms rtt min/avg/max/mdev = 0.021/0.024/0.027/0.002 ms "
i fell for this one a long time ago, mysqltuner.pl recommends to set
skip-name-resolve
ref: https://www.vionblog.com/skip-name-resolve-to-speed-up-mysql-and-avoid-problems/
so mysql users where host like 'localhost' cant resolve to 127.0.0.1
=> fix?
a) change mysql host to 127.0.0.1
b) update user config
c) remove skip-name-resolve
decide for youself
I never set skip name resolve to ON though...
sure enough, your MySQL is not binding to IPv6.
you need either to bind your mysql server to IPv6, or
edit
/etc/hosts
remove localhost and add
127.0.0.1 localhost