Docker, unable to connect WordPress to MySQL - 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

Related

mysql docker container - cannot connect with windows

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

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

Docker, Runned mysql container with port forwarding is stopped immediately as soon as it launched

I have got a problem with launching MySQL container.
I run MySQL container with below command:
$ sudo docker run -d --name stockdb -e MYSQL_ROOT_PASSWORD=yang1234 -e MYSQL_DATABASE=stkanalysis mysql:5.7 -p 3307:3306
and checked result using
$ sudo docker ps -a
This is the result.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
34e98ad90f73 mysql:5.7 “docker-entrypoint…” 2 seconds ago Exited (1) 1 second ago stockdb
When I launched same MySQL container without option -p, it worked well like this:
$ sudo docker run -d --name stockdb -e MYSQL_ROOT_PASSWORD=yang1234 -e MYSQL_DATABASE=stkanalysis mysql:5.7
But, whenever I put the port forwarding option -p, running container is failed(technically, it is exited as soon as runed)
I hope to run MySQL container with port forwarding to connect its DBMS from outside host.
I’m using Ubuntu 16.04 and Docker version is 17.09.0-ce.
I solved my problem.
The cause was the position of option -p located at the end of commend.
I moved option -p statement forward, and it works well now.
$ sudo docker run --name stockdb -p 3307:3306 -p 3308:22 -e MYSQL_ROOT_PASSWORD=yang1234 -e MYSQL_DATABASE=stkanalysis mysql:5.7
thank you.

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.

Using mysql image from docker

I am trying to use the available mysql tags from this doc.
I am using the following command to download the image
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
But when I run this image with
sudo docker run mysql:tag
it gives the following error
error: database is uninitialized and MYSQL_ROOT_PASSWORD not set
Did you forget to add -e MYSQL_ROOT_PASSWORD=... ?
Any idea on where else to set this password?