Can't connect to MySQL using Acumos_mariadb_service docker container - mysql

I have installed the maria_db service as a docker component of Acumos. Even if the docker container is running, I am not able to execute the following command:
mysql -h localhost -P 3306 --user=root --password=98dceddd-a364-4f76-abe0-b0dc7283fc7f -e 'SHOW DATABASES;'
because I get the error as error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
However, if I login into the docker container using:
docker exec -it acumos_mariadb_db_service bash
and run the same command, it works.
How can I login to the MySQL server from outside the container without getting any error?

Is docker exposing port 3306 on localhost?
docker run --name some-mariadb -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mariadb:tag -p 3306:3306
Docker containers don't expose ports to the host by default; you need to set them up yourself. In the example above, you're mapping the container's port 3306 to your local machine's 3306.
Details for how to set up container networking are here:
https://docs.docker.com/config/containers/container-networking/

I finally fixed the issue. Problem was the mariadb server runs as a docker image exposing port 3306 and the mariadb client is installed on the same local machine also using port 3306. Change the port mapping like 0.0.0.0:3307->3306/tcp solved the issue.

Related

Unable to connect to mysql 8 using mysql cmd client while running with docker-compose

I'm running a mysql 8 server on a custom port using docker and try to connect to it with command line client using the below command
`mysql -uroot -p -P 3305 --protocol=TCP -h localhost`
Error Response
ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (61)
Docker Compose File
version: '3'
services:
mysqldb:
image: mysql:8
ports:
- 3305:3306
environment:
- MYSQL_ROOT_PASSWORD=MyRootPass
- MYSQL_USER=myuser
- MYSQL_PASSWORD=myuserpass
- MYSQL_DATABASE=mydb
volumes:
- ../lcdatastore/mysql/data:/var/lib/mysql
But i'm able to connect to the mysql if the mysql is run using docker run command
docker run -e MYSQL_ROOT_PASSWORD=MyRootPass -e MYSQL_USER=myuser -e MYSQL_PASSWORD=myuserpass -e MYSQL_DATABASE=mydb -p 3305:3306 mysql:8
Thanks for any hint
Update
I'm checking this on macOS Catalina (Version 10.15.2)
I see two possible issues here. The first one is not the case for you in particular, I'm just leaving this here for anyone landing here in the future with that problem:
From the mysql docs:
If the host is not specified or is localhost, a connection to the local host occurs:
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: the client connects using a Unix socket file. The --socket option or the MYSQL_UNIX_PORT environment variable may be used to specify the socket name.
I.e. when using localhost as the host mysql tries to connect via a unix socket and not via network. The former won't work on your macOS host since only the latter will pass the connection to the container.
You can force the connection via network by using 127.0.0.1 as the host or by passing the --protocol=TCP parameter (which you did in your question):
mysql -uroot -p -P 3305 -h 127.0.0.1
mysql -uroot -p -P 3305 --protocol=TCP -h localhost
The second issue may be that the port is not correctly forwarded from the macOS host to the docker host:
Since docker uses Linux namespaces for its containers it does not actually work on macOS natively. What it does instead is to transparently start a Linux VM in the background - which is the actual docker host - and forwards all docker commands to that VM. So the containers are not actually running on macOS but inside a Linux VM.
So when a container exposes a port to the "host" this refers to Linux VM and not the macOS host. From the perspective of the mysql run on macOS localhostrefers to the macOS host and not the docker host (i.e. the Linux VM).
Normally docker will set up respective port forwardings from the macOS host to the Linux VM automatically to make this work as you expect it. But this seems to be broken in your case. To further debug this, first try to connect to mysql on the Linux VM directly:
# start a new container attached to the host network (i.e. the network of the Linux VM)
# "127.0.0.1" will force a network connection
# and "3305" therfor refers to the localhost on the docker host
docker run --network=host -ti mysql:8 mysql -u root -p -P 3305 -h 127.0.0.1
If this succeeds the docker networking is basically working correctly (inside the Linux VM) and there is an issue with forwarding ports from macOS to the VM.
Now:
check if port 3305 on macOS is accepting connections, e.g. with netcat, with and without the mysqldb service running
check which process is listening on 3305, e.g. with netstat or see Who is listening on a given TCP port on Mac OS X?

Can't connect to mysql docker image with MySql workbench

I run the mysql docker image with this command:
docker run --name mysql-docker -p 3306:3306 -e MYSQL_ROOT_PASSWORD=mysql_root -d mysql:8.0.12
The container is running. When I try to connect to it with MySQL Workbench from my local machine I get
Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/lib/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory
The documentation on the dockerhub page don't say anything about connecting from localhost, so I'm using 127.0.0.1 as the Hostname in MySQL Workbench.
How do I connect to MySQL running in a docker container from my local machine?

Connect SQL developer with MYSQL server running on Docker container

Could you please guide me - How can I connect my local SQL developer to MY SQL Server running on Docker container.
Check for the "bind"in my.cnf in docker running container, if its says 127.0.0.1 it will not connect from outside , you probably needs to change to 0.0.0.0 and restart the mysql server
I have used this command to run container -> docker run -p 3306:3306 --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:latest
It solved that issue for me.

Cant connect to Mysql docker container from host

Im running docker on windows and I start up a docker container with MySql like this
docker run -p 3306:3306 --name test -e MYSQL_ROOT_PASSWORD=secret-pw -d mysql/mysql-server:5.5
Then on my host I start up Mysql workbench and try to connect but it does not work.
docker inspect test reveal IP address on 172.17.0.2 but when I ping this I get no reply
Got this working on a linux host and I am pretty sure I have done the exact same steps
What am I doing wrong ?
Help Doc: https://docs.docker.com/samples/library/mysql/
Image link: https://store.docker.com/images/mysql
Command: docker run --name mysql_container_name --expose=3306 -p 3306 -v /my/own/datadir:/path/to/data/dir -e MYSQL_ROOT_PASSWORD=root_pwd -d mysql:tag --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
mysql_container_name: docker container name
expose: contaner exposeing port
p: host buinding port
/path/to/data/dir: share path between container and host
root_pwd: mysql root password
tag: repository tag
utf8mb4: mysql server character set and collation type
utf8mb4_unicode_ci: mysql server character set and collation type
Example: docker run --expose=3306 -p 3306 --name mysql -v /my/own/datadir:/opt/mysql -e MYSQL_ROOT_PASSWORD=0112358139 -d mysql:latest --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
Following steps to connect remotely:
docker exec -it mydb bash --> this will connect to mySql container.
echo "bind-address = 0.0.0.0" >> /etc/mysql/my.cnf --> this will update the my.cnf file.
service mysql restart --> restart the mySql service.
exit --> the mySql container.
docker inspect mysql | grep IPAddress --> grep the IP address of the contaner.
mysql -h 172.17.0.2 -u root –p --> remotely connect to the mySql.
Your host 3306 port should be forwarding to the container, so try connecting on localhost:3306. When I tried to replicate, got the "Host 172.17.0.1 not allowed to connect to this MySQL server" which means it got through at least.
More on the latter: https://github.com/fideloper/docker-mysql/issues/10

Unable to connect to dockerized mysql db remotely

On my AWS ec2 server I have docker 1.9.1 installed.
In an image test_image based from ubuntu:trusty official docker image, I have tried to setup the LEMP(Linux, Nginx, MySQL, PHP) architecture.
Following is the docker command i have used to start my container:
docker run --name test_1 -d -p 80:80 -p 3306:3306 test_image /bin/sh -c "while true; do echo daemonized docker container; sleep 5000; done"
I have exposed port 80 and 3306 to the host's network interface and have also allowed AWS's security group to allow inbound connections to these ports. Connection type in security group is: MYSQL/Aurora and protocol is: TCP (I know its not very secure, its only for initial implementation. Production setup will be different)
I followed this DigitalOcean tutorial: https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-14-04
After installing Nginx and starting it I am able to test it in the browser via ec2's pubic ip i.e. http://xxx.xxx.xxx.xxx shows the default nginx welcome page.
While installing MySQL, I followed the following commands in the docker container:
apt-get install mysql-server
mysql_install_db
/etc/init.d/mysql start
mysql_secure_installation
I have given a password to my root user and during mysql_secure_installation i had allowed remote access to root user.
mysql -u root -p command from inside the container connects me to the mysql db but not from outside the container.
Also from my local machine:
I tried with mysql-client:
mysql -h xxx.xxx.xxx.xxx -u root -p
I got the following error: ERROR 2003 (HY000): Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (111)
and also through mysql workbench but I still can't connect to the mysql db.
What am I doing wrong?
In your host mysql's my.cnf set the bind address to 0.0.0.0 so that mysql listens on all network interfaces
bind-address = 0.0.0.0
The default config is:
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address = 127.0.0.1