is it possible to use 3306 port (same port) in docker mysql running container as well as in windows 10 mysql - mysql

I deploy my war file in tomcat 7 successfully and start tomcat using following command
docker run -it --rm -p 7008:8080 -v //d/docker_tomcat/tomcat-users.xml:/usr/local/tomcat/conf/tomcat-users.xml:ro -v //d/docker_tomcat/webapps:/usr/local/tomcat/webapps:rw tomcat:7.0
when tomcat start it shows following error logs:-
AbandonedObjectPool is used (org.apache.commons.dbcp.AbandonedObjectPool#9030ca2)
LogAbandoned: true
RemoveAbandoned: true
RemoveAbandonedTimeout: 90
[localhost-startStop-1] ERROR org.hibernate.util.JDBCExceptionReporter - Cannot create PoolableConnectionFactory
i think that above error means that database studentdb is not accessible
here is my hibernate.properties file:-
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.username=root
hibernate.connection.password=root
hibernate.connection.url=jdbc:mysql://10.0.75.x<ip of docker>:3306/studentdb?autoreconnect=true&zeroDateTimeBehavior=convertToNull&jdbcCompliantTruncation=false
show_sql=false
hibernate.jdbc.use_streams_for_binary=false
hibernate.dbcp.testOnBorrow=true
hibernate.dbcp.validationQuery=SELECT 1 FROM DUAL
hibernate.dbcp.testOnReturn=false
hibernate.dbcp.maxWait=2000
hibernate.dbcp.testWhileIdle=true
hibernate.dbcp.minEvictableIdleTimeMillis=1800000
hibernate.dbcp.timeBetweenEvictionRunsMillis=300000
hibernate.dbcp.numTestsPerEvictionRun=5
hibernate.dbcp.removeAbandoned=true
hibernate.dbcp.removeAbandonedTimeout=90
hibernate.dbcp.logAbandoned=true
i think there may be error in hibernate.connection.url property of
hibernate.properties file.
and also doubt is it becuase my windows 10 uses port 3306 for mysql as well as docker also uses port 3306 for mysql. if is it problem then how can i change port of mysql container running in docker with some different port

Use following command to forward your local port to docker container port
docker run -p <LOCAL-PORT>:3306 <mysql-image-name>

Related

MySQL docker image takes too long to start up the dbms server and socket. ERROR 2002 (HY000): Can’t connect to local MySQL server through socket

Issue type:
initialization bug with mysql:8 docker official image.
Versions:
Docker version 20.10.14, build a224086;
Linux Ubuntu 21.10 host machine OS;
Docker mysql:8 and mysql:oracle official image tags, both with same bug.
Steps to reproduce:
run mysql:8 docker official image from docker hub passing the needed args at the CLI env variables. Such as:
sudo docker run --name app-mysql-container -v [some-absolute-path-in-your-host-machine]:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=[some-root-password] -e MYSQL_DATABASE=[some-database-name] -e MYSQL_USER=[some-user-name] -e MYSQL_PASSWORD=[some-user-password] -d mysql:8
use another instance of the terminal to follow the logs live of the container just created:
sudo docker --follow logs app-mysql-container
then use at the other instance of the terminal:
sudo docker exec -it app-mysql-container bash , in order to get inside the container bash shell, and type:
mysql -u root -p
[type password]
ERROR HAPPENS CONTINUOUSLY until server finishes getting up - about 8 minutes !!
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)
after the 8 minutes, when the mysqld server finally gets ready for connection, with a ready socket connection at 3306 port, then everything starts working fine... both for access from inside the container (MySQL CLI) as from outside linked containers with applications connecting to the mysql server.
Eventually the MySQL server starts almost instantly, rather than after 8 minutes... something is wrong with this docker image...
Printscreens below:

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 Windows 10 Docker mysql

Win 10
Composer version 1.4.1 2017-03-10 09:29:45
PHP 7
npm/Node
Docker CE
Apache 2.4
Powershell
git BASH shell
drush (installed via composer)
Noob Composer/Docker skills
I have a docker config yml specifying how mysql service can start:
version: "2"
services:
mysql:
image: mysql:5.6
ports:
- 3306:3306
volumes:
- /data/nbif_mysql:/var/lib/mysql
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
And when I call
#!/bin/bash
docker-compose up -d mysql
I see that the container is running:
PS C:\dev\appname> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f1a0ecab8af6 mysql:5.6 "docker-entrypoint..." 2 hours ago Up 5 seconds 0.0.0.0:3306->3306/tcp appname_mysql_1
But, notice that the reported IP is 0.0.0.0:3306->3306/tcp
So when I try to connect with the expected IP, it fails:
ERROR 2003 (HY000): Can't connect to MySQL server on '172.17.0.1' (10060)
How to I tell docker-compose to use that expected IP for docker?
Is this a setup issue, or some config tweak I need to do?
When you bind a port to your host, you have to use localhost instead of the container's IP address, because you're not assigning any local IP address.
Every container always runs in a isolated network (bridge), the container in your compose file will be able to find the others by their hostname, but inside of those containers, they are isolated from the local network so that's why you can't reach them.
In your compose file you only have a mysql container and you're binding that port in your host, so the only way to reach that container is by using localhost:3306
Remember, when you run a docker container it isn't like a server with an IP in your host network, it's more like a virtual machine with an isolated network configuration.
Take a look on the docker-compose docs in this specific topic:
https://docs.docker.com/compose/networking
UPDATE:
The link that finally answered the question was:
https://docs.docker.com/engine/userguide/networking/default_network/custom-docker0/

Running Spring Boot app inside Docker container, unable to connect MySQL

I'm trying to learn Docker and I created a container which is running MySQL server. It works fine and I can use MySQL from my Spring Boot application, when I run Spring Boot application locally (without Docker). But when I try to run Spring Boot application inside another Docker container, connection to MySQL fails and I get error: java.net.ConnectException: Connection refused
In my Spring Boot application.properties I have this configuration:
spring.datasource.url: jdbc:mysql://127.0.0.1/mydb
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driverClassName=com.mysql.jdbc.Driver
Any ideas what might be wrong?
When you want to use the MySQL container from your Spring Boot container a good idea would be to link to it like:
docker run ... --name spring-boot --link mysql ...
Assuming that mysql is the name of your MySQL container you could then use the following JDBC URL in your configuration:
spring.datasource.url: jdbc:mysql://mysql/mydb
The best way to handle this situation (which I used) would be to run your MySQL container in detached mode. Obviously, you can name your container as you like:
docker run --detach --name = my-MySQL --env = "MYSQL_ROOT_PASSWORD=your_password_here" mysql
Your container will be running in detached mode, now you can check the IP and the port on which its running using the inspect command :
docker inspect docker_mysql
And you can check the logs using:-
docker logs my-MySQL
Furthur you can use the IP you get after inspecting. My MySQL was running on 172.17.0.2 and 3306 port the default one:
spring.datasource.url=jdbc:mysql://172.17.0.2:3306/mydb
You can also connect to the MySQL server using any client. I generally use the mysql-client:
sudo mysql -uroot -pyour_password_here -h 172.17.0.2 -P 3306

unable to connect to dockerized mysql container locally

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.