Mysql Database cant connect using "loclhost" But Can Connect using "127.0.0.1" Centos 7 (Cloudlinux) - mysql

My OS: Centos 7 + CLOUDLINUX 7.7
When I Try To Login Mysql From Server
[root#server3 ~]# Mysql -u root -h localhost -P 3306 Working ok
[root#server3 ~]# Mysql -u root -h 127.0.0.1 -P 3306 Working Ok
But When I Use localhost on Hosted Domain "xyz.com" In there DB Host = "localhost"
It Not Connect, When I Change It to DB Host = "127.0.0.1" It Connect to Database
[root#server3 ~]# cat /etc/hosts
127.0.0.1 localhost
::1 localhost localhost6.localdomain6 localhost6
[root#server3 ~]# ping localhost
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.044 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.056 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.079 ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.064 ms
/** MySQL hostname */
define( 'DB_HOST', 'localhost' );
any Idea For Fix?

MySQL connects over TCP/IP when using IP address (127.0.0.1), and uses socket file when used localhost. In your situation, I suppose you have mysql.sock in some special location and it is not available from CageFS inside. While /var/lib/mysql/ is being used by default.
If so - please add that directory to /etc/cagefs/cagefs.mp file then run cagefsctl --remount-all.
And Problem Fix

Related

mysql could not connect user#remotehost (111)

Tried all - Server version: 5.7.29-0ubuntu0.18.04.1 (Ubuntu)
1) GRANT ALL PRIVILEGES ON mydb . * TO 'myuser'#'%' IDENTIFIED BY 'mypasswd' WITH GRANT OPTION;
2) FLUSH PRIVILEGES;
3) Restarted MySQL
4) edited config vim /etc/mysql/mysql.conf.d/mysqld.cnf [ commented #bind-address ]
5) Checked the Grant column in User accounts through PHPmyadmin and is set to YES
6) Firewall is disabled.
7) Failed == mysql -h eu-ce.compute.internal -u myuser -p
8) Done restarting MySQL whenever needed [ port is the default : 3306 ]
nmap hostname -p 3306
Starting Nmap 7.60 ( https://nmap.org ) at 2020-01-31 09:52 UTC
Nmap scan report for hostname (xxx.xx.xx.xxx)
Host is up (0.00028s latency).
PORT STATE SERVICE
3306/tcp closed mysql
Nmap done: 1 IP address (1 host up) scanned in 0.04 seconds
Tried a lot of options,
Is there anything else that I am missing here to connect a DB from a remote host.?
The problem is that the server is listening internally only.
Removing the line bind-address 127.0.0.1 from mysql cnf file.
Or try with to change port 3306 to different one in mysql cnf file.
and restart your mysql server.
I hope this will work for you.

MySQL - Can connect via localhost but not via 127.0.0.1

I have a MySQL server running on Debian (Server version: 5.5.60-0+deb8u1 (Debian)). Everything worked fine until rebooting the server. Now I can only connect to my MySQL server via localhost, whereas via 127.0.0.1 doesn't work:
$ mysql -h 127.0.0.1 -u root -p gives me ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0
I have port 3306 open in the firewall and the server seems to be listening on 3306:
$ netstat -plnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 20877/mysqld
Connecting to 3306 with telnet doesn't seem to work, neither (for both localhost and 127.0.0.1):
$ telnet 127.0.0.1 3306
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
In my my.cnf I have bind-address = 127.0.0.1, which shouldn't be the problem...
In /etc/hosts there are these lines
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
I am just not able to find the error, do you have any ideas?
It turned out that TCP-Wrappers were denying the connection to 127.0.0.1. Solved by adding mysqld: 127.0.0.1 to the /etc/hosts.allow-file.

From compose to swarm : Lost connection to MySQL server at 'reading authorization packet'

I have a docker-compose file that allows me to start and access a web application, this with the compose command: docker-compose -f docker-compose-dev.yml up -d on the original docker-compose-dev.yml file:
version: "3"
services:
learnintouch-startup:
image: localhost:5000/learnintouch-startup
container_name: learnintouch-startup
restart: always
volumes:
- "~/dev/docker/projects/learnintouch/volumes/engine:/usr/bin/learnintouch/engine"
- "~/dev/docker/projects/learnintouch/volumes/www.learnintouch/account/data:/usr/bin/learnintouch/www/learnintouch.com/account/data"
- "~/dev/docker/projects/learnintouch/volumes/www.thalasoft/account/data:/usr/bin/learnintouch/www/thalasoft.com/account/data"
- "~/dev/docker/projects/learnintouch/volumes/www.folkuniversitet/account/data:/usr/bin/learnintouch/www/folkuniversitet/account/data"
ports:
- "81:80"
links:
- mysql
- redis
- nodejs-learnintouch
nodejs-learnintouch:
image: localhost:5000/nodejs-learnintouch
container_name: nodejs-learnintouch
restart: always
volumes:
- "~/dev/docker/projects/learnintouch/volumes/engine:/usr/bin/learnintouch/engine"
ports:
- "9001:9001"
links:
- redis
mysql:
image: localhost:5000/mysql:5.6.30
container_name: mysql
restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
volumes:
- "~/dev/docker/projects/learnintouch/volumes/mysql/data:/usr/bin/mysql/install/data"
redis:
image: localhost:5000/redis:3.0.7
container_name: redis
restart: always
Now, I'd like to run it in swarm mode instead. I understand swarm mode is available since I'm running docker version 17.05.0-ce both for the engine and the client. The host is Ubuntu 16.04.
I could init the swarm all right with the docker swarm init command.
Then I try to start the web application:
$ docker stack deploy --compose-file docker-compose-swarm-dev.yml learnintouch
Creating network learnintouch_default
Creating service learnintouch_mysql
Creating service learnintouch_redis
Creating service learnintouch_learnintouch-startup
Creating service learnintouch_nodejs-learnintouch
The learnintouch-startup container has a MySQL client is trying to access the MySQL server in the mysql container.
But there is no such container name and therefore no such hostname any longer.
When, before, in non swarm mode, the container name was being used as the hostname, now, there is no such container name.
I tried with the following docker stack deploy --compose-file docker-compose-swarm-dev.yml learnintouch command on the docker-compose-swarm-dev.yml file:
version: "3"
services:
learnintouch-startup:
image: localhost:5000/learnintouch-startup
volumes:
- "~/dev/docker/projects/learnintouch/volumes/engine:/usr/bin/learnintouch/engine"
- "~/dev/docker/projects/learnintouch/volumes/www.learnintouch/account/data:/usr/bin/learnintouch/www/learnintouch.com/account/data"
- "~/dev/docker/projects/learnintouch/volumes/www.thalasoft/account/data:/usr/bin/learnintouch/www/thalasoft.com/account/data"
- "~/dev/docker/projects/learnintouch/volumes/www.folkuniversitet/account/data:/usr/bin/learnintouch/www/folkuniversitet/account/data"
ports:
- "81:80"
nodejs-learnintouch:
image: localhost:5000/nodejs-learnintouch
volumes:
- "~/dev/docker/projects/learnintouch/volumes/engine:/usr/bin/learnintouch/engine"
ports:
- "9001:9001"
mysql:
image: localhost:5000/mysql:5.6.30
environment:
- MYSQL_ROOT_PASSWORD=root
volumes:
- "~/dev/docker/projects/learnintouch/volumes/mysql/data:/usr/bin/mysql/install/data"
hostname: mysql
redis:
image: localhost:5000/redis:3.0.7
How can I specify the hostname of a swarm service ?
Note that if I start the application in non swarm mode with the docker-compose -f docker-compose-dev.yml up -d command then the application responds fine. It is only if I start it in swarm mode with the docker stack deploy --compose-file docker-compose-swarm-dev.yml learnintouch command that the mysql hostname is not found any longer: The data source for the database db_learnintouch could not be initialized for the user learnintouch on the host mysql:3306. Indeed, in that latter case, the container name is learnintouch_mysql.1.pu846rr8to5gwxwnxpdm4hdth instead of being mysql.
UPDATE: It appears the issue is not a hostname one but a swarm/mysql one.
In the mysql container I can log in to MySQL just fine:
$ docker exec -it learnintouch_mysql.1.m51o8deg8cslb8mzayp119m67 bash
root#mysql:/usr/bin/mysql-5.6.30# cd /usr/bin/mysql/install;
root#mysql:/usr/bin/mysql/install# bin/mysql --protocol=tcp -h mysql -P 3306 -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
But in the learnintouch-startup client container, I can only ping the mysql service but not log into it:
$ docker exec -it learnintouch_learnintouch-startup.1.kf13qktlvxs9rgjzaea2xxd68 bash
root#aa0f0539ba52:/usr/bin/learnintouch/www/folkuniversitet# cd /usr/bin/mysql/install
root#aa0f0539ba52:/usr/bin/mysql/install# bin/mysql --protocol=tcp -h mysql -P 3306 -u root -p
Enter password:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading authorization packet', system error: 0
root#aa0f0539ba52:/usr/bin/mysql/install# ping mysql
PING mysql (10.0.0.8) 56(84) bytes of data.
64 bytes from 10.0.0.8: icmp_seq=1 ttl=64 time=0.061 ms
64 bytes from 10.0.0.8: icmp_seq=2 ttl=64 time=0.084 ms
64 bytes from 10.0.0.8: icmp_seq=3 ttl=64 time=0.075 ms
^C
--- mysql ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1998ms
I can also telnet in fact:
root#861cc3861e67:/usr/bin/learnintouch/www/folkuniversitet# telnet mysql 3306
Trying 10.0.0.6...
Connected to mysql.
Escape character is '^]'.
N
5.6.30-logs5L\i|Sj��=0t}62:k,8s]mysql_native_password
!#08S01Got packets out of orderConnection closed by foreign host.
I'm using a custom built MySQL version mysql:5.6.30 with the my.cnf file:
[mysqld]
bind-address = 0.0.0.0 # Allow client binding from any IP address instead of just 127.0.0.1
port = 3306
sql_mode = NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION # This is strict mode: NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
socket = /usr/bin/mysql/install/tmp/mysql.sock
user = root
basedir = /usr/bin/mysql/install
datadir = /usr/bin/mysql/install/data
log-bin = /usr/bin/mysql/install/mysql.bin.log
log-error = /usr/bin/mysql/install/mysql.error.log
general-log-file = /usr/bin/mysql/install/mysql.log
slow-query-log-file = /usr/bin/mysql/install/mysql.slow.queries.log
innodb_file_per_table = 1
innodb_flush_log_at_trx_commit = 1
sync_binlog = 1
innodb_flush_method = O_DIRECT
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init-connect = 'SET NAMES utf8mb4'
character-set-client-handshake = FALSE
connect_timeout = 60
wait_timeout = 28800 # amount of seconds during inactivity that MySQL will wait before it will close a connection on a non-interactive connection
interactive_timeout = 28800 # same, but for interactive sessions
[client]
socket = /usr/bin/mysql/install/tmp/mysql.sock
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
protocol = tcp # Forces the client to use the TCP protocol, except for PHP which ignores it
There are no /etc/hosts.allow nor /etc/hosts.deny files in the mysql container.
There is no firewall around.
The Docker version is 17.05.0-ce on both engine and client and the docker-compose version is 1.16.1.
UPDATE: After deleting the swarm and starting it again, I could run the application in the browser and see it run just fine. And so, following this, I did another delete followed by a start, and again, the same error occured. So the error, sometimes, but rarely, does not occur.
UPDATE: It appears, running the telnet mysql 3306 command from within the client container, removes the issue and allows the MySQL client connection to proceed. This is consistent: I delete the swarm and start it anew, I open a bash in the client container, I try to log in the MySQL server container and it fails, I retry many times and it fails all the same, I run the ping mysql command and then try again to connect many times and it fails, and I then run the telnet mysql 3306 command and try then to connect and it succeeds.
root#2651380ce02e:/usr/bin/mysql/install# cd /usr/bin/mysql/install; bin/mysql --protocol=tcp -h mysql -P 3306 -u root -p
Enter password:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading authorization packet', system error: 0
root#2651380ce02e:/usr/bin/mysql/install#
root#2651380ce02e:/usr/bin/mysql/install# ping mysql
PING mysql (10.0.0.4) 56(84) bytes of data.
64 bytes from 10.0.0.4: icmp_seq=1 ttl=64 time=0.088 ms
64 bytes from 10.0.0.4: icmp_seq=2 ttl=64 time=0.109 ms
64 bytes from 10.0.0.4: icmp_seq=3 ttl=64 time=0.091 ms
^C
--- mysql ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.088/0.096/0.109/0.009 ms
root#2651380ce02e:/usr/bin/mysql/install# telnet mysql 3306
Trying 10.0.0.4...
Connected to mysql.
Escape character is '^]'.
N
5.6.30-logf[tT)mlX��Pi#EwHCT\SkBmysql_native_password
Connection closed by foreign host.
root#2651380ce02e:/usr/bin/mysql/install# cd /usr/bin/mysql/install; bin/mysql --protocol=tcp -h mysql -P 3306 -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Here is a complete demo run as evidence:
$ docker exec -it learnintouch_learnintouch-startup.1.ili3m1kre9q2eaiyzlucr2uot bash
root#ce21d5b78886:/usr/bin/learnintouch/www/folkuniversitet# cd /usr/bin/mysql/install; bin/mysql --protocol=tcp -h mysql -P 3306 -u root -pEnter password:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading authorization packet', system error: 0
root#ce21d5b78886:/usr/bin/mysql/install#
root#ce21d5b78886:/usr/bin/mysql/install#
root#ce21d5b78886:/usr/bin/mysql/install# ping mysql
PING mysql (10.0.0.4) 56(84) bytes of data.
64 bytes from 10.0.0.4: icmp_seq=1 ttl=64 time=0.040 ms
64 bytes from 10.0.0.4: icmp_seq=2 ttl=64 time=0.110 ms
64 bytes from 10.0.0.4: icmp_seq=3 ttl=64 time=0.093 ms
64 bytes from 10.0.0.4: icmp_seq=4 ttl=64 time=0.091 ms
^C
--- mysql ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 2999ms
rtt min/avg/max/mdev = 0.040/0.083/0.110/0.027 ms
root#ce21d5b78886:/usr/bin/mysql/install# bin/mysql --protocol=tcp -h mysql -P 3306 -u root -p
Enter password:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading authorization packet', system error: 0
root#ce21d5b78886:/usr/bin/mysql/install# bin/mysql --protocol=tcp -h mysql -P 3306 -u root -p
Enter password:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading authorization packet', system error: 0
root#ce21d5b78886:/usr/bin/mysql/install# ping mysql
PING mysql (10.0.0.4) 56(84) bytes of data.
64 bytes from 10.0.0.4: icmp_seq=1 ttl=64 time=0.091 ms
64 bytes from 10.0.0.4: icmp_seq=2 ttl=64 time=0.105 ms
64 bytes from 10.0.0.4: icmp_seq=3 ttl=64 time=0.082 ms
^C
--- mysql ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.082/0.092/0.105/0.014 ms
root#ce21d5b78886:/usr/bin/mysql/install# bin/mysql --protocol=tcp -h mysql -P 3306 -u root -p
Enter password:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading authorization packet', system error: 0
root#ce21d5b78886:/usr/bin/mysql/install# bin/mysql --protocol=tcp -h mysql -P 3306 -u root -p
Enter password:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading authorization packet', system error: 0
root#ce21d5b78886:/usr/bin/mysql/install# telnet mysql 3306
Trying 10.0.0.4...
Connected to mysql.
Escape character is '^]'.
N
5.6.30-logZo?*R#)M��(~cWg"q5R}?amysql_native_password
^CConnection closed by foreign host.
root#ce21d5b78886:/usr/bin/mysql/install# bin/mysql --protocol=tcp -h mysql -P 3306 -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.30-log Source distribution
Copyright (c) 2000, 2016, 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> quit;
Bye
root#ce21d5b78886:/usr/bin/mysql/install#
you can use the service name which you have specified in docker-compose file as the hostname in swarm, for example you have your mysql service name as mysql you can use that name as hostname,moreover some options like links are not supported in docker stack https://docs.docker.com/compose/compose-file/#not-supported-for-docker-stack-deploy
in case you want to use different hostname in different network,then you can use aliases
https://docs.docker.com/compose/compose-file/#networks
I suspected a bug in this mysql:5.6.30 and so I removed and replaced it with mariadb:10.1.24 and the issue was gone.

Connecting to MariaDB via TCP returns socket error

I'm trying to connect to a local installation of MariaDB 10xx via the mysql command line application but despite specifying a host and a port I get an error the client cannot connect via the unix socket.
This is my my.cnf:
⟩ sudo vi /opt/local/etc/mariadb-10.1/my.cnf
# Use default MacPorts settings
!include /opt/local/etc/mariadb-10.1/macports-default.cnf
[mysqld]
socket=/private/tmp/mysqld.sock
port=3306
log_error=/private/tmp/mysql_error.log
log_warnings=3
general-log
general-log-file=/private/tmp/mariadb_queries.log
log-output=file
This is the command line I use:
mysql -u root --host localhost --port 3306 --password
And this is the error:
⟩ mysql -u root --host localhost --port 3306 --password
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/opt/local/var/run/mariadb-10.1/mysqld.sock' (2 "No such file or directory")
I'm trying to check whether MariaDB is listening to TCP connections.
So I asked in the MariaDB IRC channel and apparently passing localhost to MariaDB causes it to use the unix socket. Replacing --host localhost with --host 127.0.0.1 did the trick.

MySQL connection only works through command line [duplicate]

This question already has an answer here:
mysql running but cannot connect to 127.0.0.1
(1 answer)
Closed 7 years ago.
From my mac, I installed mysql56+server through macports 4 months ago. Today after a laptop restart, I wasn't able to connect to MySQL anymore through SQL Pro. I can only connect through the Terminal. I have posted some relevant information below.
Command that successfully works for connecting to MySQL (after entering password):
/opt/local/etc/mysql56 mysql -u root -p
Command that fails to connect to MySQL:
/opt/local/etc/mysql56 mysql -u root -p -h '127.0.0.1'
Enter password:
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (61)
/opt/local/etc/mysql56 telnet 127.0.0.1 3306
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
telnet: Unable to connect to remote host
/etc/hosts contains:
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
The my.cnf file that I am using has everything commented out. I do not want to use a socketed connection, just standard 127.0.0.1 connection that I was using in SQL Pro.
In your my.cnf file, find bind-address and change it to (or comment out):
bind-address = 0.0.0.0
#bind-address = 0.0.0.0
After that restart mysql and try to connect:
mysql.server restart
mysql -u root -p -h '127.0.0.1'
This is a duplicate of both
Setup MySQL 5.6 with Macports and
mysql running but cannot connect to 127.0.0.1.
See also the output of port notes mysql56-server, which explains this and was printed during installation.