mysql docker container - cannot connect with windows - mysql

I am very sorry guys, I found several topics on stackoverflow but none of them solved my issue. I am a docker noob, but all I want to do is connect to my docker mysql database in a docker container created via docker-desktop on windows.
docker run -p 3306:3306 --name blaaa -e MYSQL_ROOT_PASSWORD=password -d mysql
I set up everything with the suggested port mapping, but I cannot connect to the docker container from the host (windows 10) via mysql-workbench or other programs. I read that there are issues and you often cannot connect to the docker host via localhost, but I cannot even figure out what the freakin ip of docker0 or other adapters is (ipconfig does not show anything). docker inspect <id> shows a lot of information, but besides mapping to 0.0.0.0:3306 and other ips that don't work, I cannot really figure out what to do here. the container itself is running fine and I can access the database from inside the container without any issues.
thanks for your help!
/SOLVED
I am sorry for the confusion; I think it was due to the Windows clients (I tried several) that things didn't work out. I finally got it working with HeidiSQL. Don't ask me how or why HeidiSQL finally works; but mysql-workbench generally showed strange behavior on my system, it crashed several times out of the blue. Thanks for your help.

Hey when you don't specify the database name, the container will stop as soon as it created, so specify the database name as environment variable
this is the docker command :
docker run -p <host_machine_port>:3306 --name <container_name> -e MYSQL_ROOT_PASSWORD=<root_password> -e MYSQL_DATABASE=<db_name> -d mysql
in your case :docker run -p 3306:3306 --name blaaa -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE= testDB -d MySQL
And next verify if the container running or not: docker ps
if it shows the container name you specified when you created it, it's running.
next connect to your container : mysql --host=127.0.0.1 --port=3306 -u root -p password

this works
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d -p 3307:3306 mysql
reason explained here -p is an argument
https://github.com/docker-library/mysql/issues/504

Related

How can I to connect to a Docker running mysql?

The instructions here: https://hub.docker.com/_/mysql indicate to run
$ docker run -it --network some-network --rm mysql mysql -hsome-mysql-container -uexample-user -p
But i have no idea what some-network is? So i run this instead and get and 'unknown MySQL host' error even though some-mysql-container is definitely the name of my container.
$ docker run -it--rm mysql mysql -hsome-mysql-container -uexample-user -p
What am I doing wrong here?
'some-network' refers to a docker-network. You need to create it first. I named it 'mysql-network' to make its purpose a bit more clear:
docker network create mysql-network
Then, start database container:
docker run --network mysql-network --name mysql-db -e MYSQL_ROOT_PASSWORD=mysecretpassword -d mysql
Then, start a the client container to connect to the first one:
docker run -it --network mysql-network --rm mysql mysql -hmysql-db -uroot -p
By adding both containers to the same network, they are able to communicate with each other.

using docker images for mySQL and redmine, how do I resolve "Unknown MySQL server host"?

I am using the docker images supplied at https://hub.docker.com/_/redmine
I have chosen to use MySQL as my database backend. So I have 2 docker containers: MySQL and Redmine, as downloaded from dockerhub.
Following the instructions on the docker/redmine link above, I ran through the commands and found that the redmine docker would not start. Inspecting the docker logs, I see:
rake aborted!
Mysql2::Error::ConnectionError: Unknown MySQL server host redmine (-5)
I thought the 2 dockers were having difficulty talking to each other, so I setup a new docker network for both containers to use:
docker network create --driver bridge redmine-net
Adapting the instructions, on the docker/redmine link above, I run
docker run -d name our-mysql --network redmine-net -e MYSQL_USER=redmine -e MYSQL_PASSWORD=todays-password -e MYSQL_DATABASE=redmine -e MYSQL_RANDOM_ROOT_PASSWORD=1 -p 3306:3306 mysql:5.7
docker run -d name our-redmine --network redmine-net -e REDMINE_DB_MYSQL=redmine -e REDMINE_DB_USERNAME=redmine -e REDMINE_DB_PASSWORD=todays-password redmine:latest
However, the redmine contain still falls over instantly, with the same error.
EDIT Using the *.yml file as provided in the dockerhub redmine instructions works pretty faultlessly.
So the question is: what is the docker-compose method doing that docker run isn't handling?
Thank you.
The REDMINE_DB_MYSQL arg of the redmine container do reference to the mysql container, so, if you define the database service like our-mysql, then set REDMINE_DB_MYSQL=our-mysql

Docker, unable to connect WordPress to MySQL

I'm trying to link two containers together, I was able to connect a PhpMyAdmin container with a MySQL container, but nothing seem to work when I'm using a WordPress container.
I tried different things, actually I'm using this command to run a MySQL container:
sudo docker run --name sql -e MYSQL_ROOT_PASSWORD=pass mysql
and this one to set up the WordPress container:
sudo docker run --name wpress -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_PASSWORD=pass -e WORDPRESS_DB_HOST=172.17.0.2 -p 8085:80 --link sql:mysql wordpress
MySQL container work fine, but I have this output from wpress:
MySQL Connection Error: (2054) The server requested authentication method unknown to the client
Warning: mysqli::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] in Standard input code on line 22
What I am doing wrong?
Edit:
I was able to connect the wpress container to sql container a couple of time during some test by adding a port to WORDPRESS_DB_HOST, which will give:
sudo docker run --name wpress -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_PASSWORD=pass -e WORDPRESS_DB_HOST=172.17.0.2:3306 -p 8085:80 wordpress
(I also remove the --link option, it worked without it).
So it work 2-3 times, but it doesn't work anymore.
It seem to be a version error. Use an image with a different version of mysql, mysql:5.7 for example, and it should work.
I had same problem/error.
This is what I had to do for mysql and wordpress:
docker run --name wordpressdb -e MYSQL_ROOT_PASSWORD=password -d mysql
docker exec -it wordpressdb bash
#inside run:
mysql -uroot -ppassword
#paste
ALTER USER 'root'#'%' IDENTIFIED WITH mysql_native_password BY 'password';
exit
exit
docker run --name wordpress -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_PASSWORD=password -p 8080:80 --link wordpressdb:mysql -d wordpress

Docker mysql container not creating user with password when specified with database

I have a usecase where i need to run mysql on a container and link it to another container. I also have my db files data in my host location which is mounted as a volume on to the database container... The condition is to run the container not as root but as a different user with all privileges.
The db is there is in the mounted volume.
I ran the following command:
docker run -d -v ~/testdata:/var/lib/mysql -e MYSQL_DATABASE=Testdata_DB -e MYSQL_USER=testdata -e MYSQL_PASSWORD=mypasswordhere -p 3306:3306 --name=testdata_db mysql
The above command will start the container but i am not able to see the user with the password when i bash into the running container. Only the mysql is running
docker exec -it testdata_db bash
Kindly let me know where i am going wrong. I followed the documentation under the docker official repo link.
I solved it by creating a init.sql with the required sql commands to create user , tables which it loaded from my host to the container under docker-entrypoint-initdb.d/ and set the env varibles as required. This made mysql instance load as a fresh instance and loaded all the required tables and data.
The final command is:
docker run --name testdata_db -p 3306:3306 -e "MYSQL_ROOT_PASSWORD= " -e "MYSQL_USER=test" -e "MYSQL_PASSWORD=mypass" -e "MYSQL_DATABASE=mysql" -v ~/mysql/db/:/var/lib/mysql/ -v ~/mysql/init/:/docker-entrypoint-initdb.d/ -d mysql

Avoid hard coding the mysql container ip in my apache container script

I have a mysql container which runs fine. I can start it and see it up and running in the docker ps list.
I then try to run another learnitouch container in which an engine-db-seed.sh shell script tries to connect to the mysql container server.
The learnitouch container Dockerfile contains:
ENTRYPOINT ["/bin/bash", "/usr/bin/learnintouch/engine-db-seed.sh"]
The engine-db-seed.sh file contains:
/usr/bin/mysql/install/bin/mysql --protocol=tcp -h 172.17.0.2 -u root -proot -v < /usr/bin/learnintouch/db_engine-db.sql
The db_engine-db.sql is being seeded all right in the mysql database.
But I had to hard code the mysql container IP as you can see in the -h option. I got the 172.17.0.2 IP address from a docker inspect on the mysql container. Not the most automated way...
How can I do without such hard coding ?
Running the mysql container:
docker run -d -p 3306:3306 -v /home/stephane/dev/php/learnintouch/docker/mysql/data:/usr/bin/mysql/install/data --name mysql stephane/mysql:5.6.30
Running the learnintouch container:
docker run -p 127.0.0.1:80:80 --link mysql:mysql --name learnintouch stephane/learnintouch
I'm using Docker version 1.12.1, build 23cf638
Just use the service name and make sure that both services are running on the same network (bridge0 by default).
So if you create your mysql service like this
docker run -d --name foo mysql-image
your engine-db-seed.sh could then be
/usr/bin/mysql/install/bin/mysql --protocol=tcp -h foo -u root -proot -v < /usr/bin/learnintouch/db_engine-db.sql
mysql will make a dns request for foo which will be resolved by Docker to the ip of your foo service.