I'm trying to connect to a MySQL host that only expose an IPv6 address from Wordpress running in Docker. I try to add the IP to the host like this:
$ docker run --name some-wordpress -e WORDPRESS_DB_HOST=<my_ipv6_addr> \
-e WORDPRESS_DB_USER=... -e WORDPRESS_DB_PASSWORD=... -d wordpress
Where <my_ipv6_addr is the IPv6 address to the MySQL host. But wordpress fails to connect with the following error message:
Warning: mysqli::mysqli(): (HY000/2002): Invalid argument in - on line 10
Is there a way to connect Wordpress to MySQL in Docker using IPv6?
Make sure that your docker daemon is running with the --ipv6 flag
By default, the Docker server configures the container network for IPv4 only.
You can enable IPv4/IPv6 dualstack support by running the Docker daemon with the --ipv6 flag. Docker will set up the bridge docker0 with the IPv6 link-local address fe80::1.
Related
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?
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.
I have two VM's running, one (A) has the MYSQL database inside a docker network.
The other (B) VM has my front-end application docker container.
I run my MYSQL docker container (A) as follow:
docker run --name db --net=netname -v /path/to/mysql/:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=password -d mysql:5.6.32
docker exec -i db mysql -pyourpassword -e 'CREATE DATABASE mydb'
After editing the MYSQL my.cnf file to bind-address = 0.0.0.0
I've tried to connect from VM (B) using the ip address given to me by the MYSQL docker container with ip addr show eth0 command.
which is:
eth0#if46: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:12:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.18.0.2/16 scope global eth0
valid_lft forever preferred_lft forever
host: 172.18.0.2 port: 3306
The response i receive:
OperationalError: (2003, "Can't connect to MySQL server on '172.18.0.2' ([Errno 110] Connection timed out)")
I've also tried VM (A) ip address but the response becomes connection refused
I'm sure i'm missing something -or many things- i'm open for enlightenment. How would i go about successfully connecting to (A) my MYSQL container from server (B) ?
Thank you in advance.
You might as well just map the mysql port to the host.
docker run -p 3306:3306 --name db --net=netname -v /path/to/mysql/:/var/lib/mysql
-e MYSQL_ROOT_PASSWORD=password -d mysql:5.6.32
then you can access mysql using the host ip or name.
If you are trying to reach containers directly by ip address.. in most cases that means you are doing it wrong. Assume the assigned ip is different every time you start it the container.
If you want to connect two containers, they have to be in the same network. In this way you can access containers through their network-alias. For example run the following commands:
docker network create my-network
docker run --name db --net=my-network --network-alias=db -v /path/to/mysql/:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=password -d mysql:5.6.32
docker run -it --name ping --net=my-network --network-alias=ping debian:strech /bin/bash
The inside the ping containers you can run ping -c2 db
If you want to have several containers working together, you can take a look to docker-compose
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
I am still a beginner with docker, trying to use docker to help in my development prototyping. My environment is Mac using boot2docker, version as below
Client version: 1.3.1
Client API version: 1.15
Go version (client): go1.3.3
Git commit (client): 4e9bbfa
OS/Arch (client): darwin/amd64
Server version: 1.3.2
Server API version: 1.15
Go version (server): go1.3.3
Git commit (server): 39fa2fa
I ran the command as below:
docker run --name mymysql -e MYSQL_ROOT_PASSWORD=mypw -e MYSQL_DATABASE=bullshit -d mysql -p 3306:3306
docker start mymysql
I can see the process running as below:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
22d3f780c270 mysql:5 "/entrypoint.sh -p 3 2 minutes ago Up 2 seconds 3306/tcp mymysql
However I still could not connect to the mysql instance running in the docker. I tried connect to the ip retrieved by :
$ boot2docker ip
The VM's Host only interface IP address is: 192.168.59.103
Please give me a pointer on how to solve this issue, I went through the tutorial but I am not sure what went wrong.
The command you used should give an error. The syntax for docker run is as follow:
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
You have to submit the options to docker run before specifying the image used (mysql in your case), and if it's the case, the command and possible argument(s) to that command.
Not specifying a command will run the default command in the image.
Before running again the container you should stop and remove the old one:
docker kill mymysql
docker rm mymysql
And, following your example you should run:
docker run --name mymysql -e MYSQL_ROOT_PASSWORD=mypw -e MYSQL_DATABASE=bullshit -p 3306:3306 -d mysql
As you set manually a port mapping from container's port 3306 to the same port of your Boot2docker VM, you should can access to MySQL using the IP of the Boot2docker instance, typically 192.168.59.103, and connecting to port 3306.