I have installed docker on my mac. I have MySQL container which is running on my local machine (MAC).
Docker ps command is giving me below output -
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b5c50b2d334a test_mysql2 "docker-entrypoint.s…" 2 hours ago Up 2 hours (healthy) 0.0.0.0:32783->3306/tcp test_mysql2_1
I know username and password to the mysql which would be setup up in the container.
I want to connect to mysql and run some queries But I am not able to figure out how to connect to it. Any help will be appreciated.
Do you want to Connect to MySQL through Docker if yes; Kindly follow this step by step procedure that I am using.
Step 1 : Pull MySql image from docker hub. The following command will pull the latest mysql image.
cli> docker pull mysql
Step 2: Run a container from this image. ‘-name’ gives a name to the container. ‘ -e’ specifies run time variables you need to set. Set the password for the MySQL root user using ‘MYSQL_ROOT_PASSWORD’. ‘-d’ tells the docker to run the container in background.
cli> docker run --name=testsql -e MYSQL_ROOT_PASSWORD=rukshani -d mysql
This will output a container id; which means that the container is running in the background properly.
Step 3: Then check the status of the container by issuing, ‘docker ps’ command
cli> docker ps
Now you should be able to see that MySQL is running on port 3306.
Step 4: To checkout the logs of the running container use the following command
cli > docker logs testsql
Step 5: Find the IP of the container using following. Check out the “IPAddress” from the output, this will tell you the IP address.
cli> docker inspect testsql
Now you should be able to connect to MySQL using tIPs ip address on port 3306.
Base on what I understand from your question, this is what you need. (I hope so)
(This is not my own documentation, I only like to document everything most especially those procedure that I cannot put in my head, so that if ever the same thing happen or I need same procedure in the future, I will not waste my time to research again, but instead I will open my notes and run the commands.)
As you can see in the output of docker ps, the port 32783 (local machine) is mapped to the port 3306 inside the docker container. If you are using a MySQL Client (e.g. MySQL Workbench) you should be able to connect using ip localhost and port 32783. If not, you should go with docker exec and then open a interactive mysql shell inside the container (As mulg0r commented).
Related
I have a docker container running MySQL and another docker container running phpmyadmin. Both containers are running on my Ubuntu server.
Normally I can log into MySQL without problems thru phpmyadmin. However, this has happened several times in the past, that phpmyadmin runs into some issue and says:
"Cannot log in to the MySQL server" and
"mysqli::real_connect(): (HY000/2002): No such file or directory".
The funny thing is this happens at seemingly random times before giving this error. One time it worked for 4 months before giving this error message, another time it was 1 month, another time it was 3 months. There didn't seem to be any periodic or specific amount of time before it gave me this error.
I also checked the mysql container and it's still up and running and when I log into it (mysql container), I can access my db and see all the data and tables in it.
When I start the phpmyadmin container, I use this command. There is no config.user.inc.php file in /etc/phpmyadmin and it works for a few months.
docker run --name myadmin -d --link mysql_db_server:db -p 8080:80 phpmyadmin
I found some stackoverflow questions that were similar to my issue but doing what is suggested doesn't work.
One person said to edit the config.user.inc.php file and change the host to 127.0.0.1. I used the config.sample.php as my template for config.user.inc.php. In my config.user.inc.php file, I added
$cfg['Servers'][$i]['host'] = '127.0.0.1';
I then mounted a volume on my local linux server to map to /etc/phpmyadmin on the container such that when we started the container, it would use the config file. I ran:
docker run --name myadmin -d --link mysql_db_server:db -p 8080:80 -v /local/dir/config.user.inc.php:/etc/phpmyadmin/config.user.inc.php phpmyadmin
However, this is worse than when I run the docker command without the volume because using the config.user.inc.php file makes me run into the error immediately. It's almost if the config.sample.inc.php file was misconfigured
The work around for me is to wait for phpmyadmin to give me the error, stop and kill the phpmyadmin container, then start a new one. However, if I can get it to work right off the bat and not run into this error, that would be most ideal.
Im having some trouble filling my DB when installing a kar on karaf (Im using liquibase).
Im using windows 10, but using linux containers in docker, karaf 4.2.1 and mysql 5.7
There are the steps I did:
1-Went to task manager and stoped the mysql service.
2-Created and run a mysql 5.7 container doing the following:
docker run --name mysql-container -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
Connected using 127.0.0.1 root/root in heidiSql and created the required DB's so when installing the kar liquibase will fill them with data.
3-Copied the kar to the repo folder in my karaf folder, added the folder to .tar, and then to gzip (.tar.gzip)
4-Created my karaf container using:
.\build.sh --from-local-dist --archive apache-karaf-with-kar.tar.gz
5-Did run karaf in the background:
docker run -d -p 127.0.0.1:8101:8101 --name=karaf-container karaf
Now I used putty and ssh to connect to karaf - localhost:8101. enter the user karaf and pass karaf.
All ok, I do the kar:install command, kar install but the bundles are in grace period.
I go to heidiSql and see that the DB's are not filled with data.
I go back to karaf connection, do a feature:refresh and i get erros like:
Error resolving artifact org.ops4j.pax.transx:pax-transx-features:xml:features:0.3.0: [Could not transfer artifact org.ops4j.pax.transx:pax-transx-features:xml:features:0.3.0 from/to central (http://repo1.maven.org/maven2/): Failed to transfer file: http://repo1.maven.org/maven2/org/ops4j/pax/transx/pax-transx-features/0.3.0/pax-transx-features-0.3.0-features.xml. Return code is: 501 , ReasonPhrase:HTTPS Required.]
I suposse the problem here is that somehow my connection to mysql doesn't work.
After some time the ssh connection is lost (I suposse this is normal if I dont do anything for some time there).
I tried to create a mysql-server container instead, but that way im not even able to connect using heidiSql.
Adicional question:
1-Where do I find the link to the kubernetes dashboard? I cant find it on docker.
2-I was having a first error when trying to docker run karaf:
\"karaf\": executable file not found in $PATH": unknown.
I resolved that because I realised that my karaf folder had less files than a downloaded binary one.
Basically the files are the files you see when you open the folder, the build.md, license, etc...
So I did copy paste to my folder of those files and I was able to run it. Im asking, because its still a mystery to me...
Thanks for your attention
edit: tried stoping the mysql-container and started mysql from the task manager and its the same, now im sure that for some reason its not connecting to the DB's. so I guess the problem is when I create a karaf container, maybe im missing some configurations. gonna keep trying
After 2 days I finally found the problem.
First, let me say that I did try too to create a network
docker network create mynet
and used the tag docker run --network=mynet in both mysql-container and karaf-container.
In my hook, I did try to replace localhost with mysql-container:3306 and even after that it didn't work.
Anyway, to resolve, I did what I said above, plus create a new super user:
docker exec -it mysql-container mysql -u root -p
CREATE USER 'user'#'localhost' IDENTIFIED BY 'pass';
GRANT ALL PRIVILEGES ON *.* TO 'user'#'%';
FLUSH PRIVILEGES;
And after that, all worked fine.
I will leave here a usefull link that was kinda hard to find but it has pretty good info:
https://codebeamer.com/cb/wiki/5854748
plus, for the 2 extra questions I asked:
1- Still didnt find the kube dashboard, didn't look much for it yet to be honest.
2- For what I have seen in the build.sh it will search a specific folder name 'apache-karaf', so I think this error here happens if you rename your folder and it doesnt match with the build.sh.
I am using Docker to run someone's app. One of the containers is the web app and the sibling container is mysql. I need to dump a table from the DB locally but I'm not sure how to connect to it. I ran docker inspect [container name] on the mysql container and I see the IP address listed but now what do i do with that?
Try:
docker exec {container} mysqldump {mysqldump option} > dump.sql
Hopefully mysqldump is in the container.
All my experience with Docker so far has led me to believe that containers are stateless.
If so, why is my container storing the password that I change it to after the first run if I spun it up without specifying a volume or bind mount? I am especially puzzled since none of the other edits I make to the dbms persist (like creating tables).
Additional Details:
Versions:
1. Docker - 18.09.0 build 4d60db4
2. Image - mysql/mysql-server:latest
Commands:
1. $ docker run --name=sql -d mysql/mysql-server:latest
2. $ docker logs sql 2>&1 | grep GENERATED to grab the generated password for first login
3. $ docker exec -it sql mysql -uroot -p
4. mysql> Enter Password: <generated password>
5. mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY 'stkoverflw';
6. mysql> exit
7. $ docker stop sql
8. $ docker start sql
9. $ docker exec -it sql mysql -uroot -p
10. mysql> Enter Password: <stkoverflw>
How does the password configuration persist across restarts of the container?
Containers are not stateless. Containers are easy to create and destroy, so they can be used to run a service which is stateless, but each container is itself stateful.
When the container is running, there is a volume containing its root filesystem. You don't have to tell Docker to create it. Docker has to create it because otherwise where do the container's files go?
When you say docker stop, the container stops running but it is not destroyed. When you say docker start, the same container resumes with the same root volume. That's where the changed password persists. The process running in the container was stopped and a new process was started (so state held in memory would be lost), but the filesystem is still there.
To get rid of a container (including the changed password), say docker rm. Then you can say docker run to start from scratch.
I'm trying to use MySQL docker container in my host system to make installation and configuration processes much easier and faster.
So, I've pulled an image from:
https://hub.docker.com/r/mysql/mysql-server/
Then started container based on this image..
Container started fine, but I was not able to connect to this DB from my host system (everything is ok if I try to connect from container). It failed with a message:
ERROR 1130 (HY000): Host '<here goes my IP>' is not allowed to connect to this MySQL server
So, as I understand, my root user has no enough permissions.
I've entered my container:
docker exec -it mysql bash
Connected to DB:
mysql -uroot -ppassword
Updated permissions for my root user:
use mysql;
UPDATE user SET Host="%" WHERE User='root';
It's updated fine.
Than I decided to save my updated image somehow... I've found this guide:
http://docs.oracle.com/cd/E52668_01/E75728/html/section_c5q_n2z_fp.html
After executing:
docker stop mysql
docker commit -m "Fixed permissions for root user" -a "Few words about author" `docker ps -l -q` myrepo/mysql:v1
docker rm mysql
docker run --name new-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=pass -d myrepo/mysql:v1
I've found that my root user hasn't permissions again.
What is wrong here?
How to public my updated image into my Dockerhub?
My original answer is for persisting the change in the MySQL data after it has been initialized. But since you want to do this in the image for every initialization automatically there is a different approach for this. You can use one of the following options:
There is an environment variable called MYSQL_ROOT_HOST for this image where you can set the host (https://github.com/mysql/mysql-docker/blob/mysql-server/5.7/docker-entrypoint.sh#L63-L69). You should be able to set this to % to allow all hosts to connect as root such as -e MYSQL_ROOT_HOST="%".
The image supports adding SQL files to /docker-entrypoint-initdb.d/ to be initialized on startup (https://github.com/mysql/mysql-docker/blob/mysql-server/5.7/docker-entrypoint.sh#L98-L105). You can create your SQL file that has UPDATE mysql.user SET Host="%" WHERE User='root'; in it and then ADD that file to /docker-entrypoint-initdb.d/ in your own image. Then, when starting a container based on that image it will initialize that SQL file.
That image specifies a default volume to hold the MySQL data at https://github.com/mysql/mysql-docker/blob/mysql-server/5.7/Dockerfile#L11. When you start the container, a volume is created for that container. When you update the permissions for the root user, it is saved in this volume (it is actually part of MySQL data for the mysql database). But once you remove the container, that volume is also lost.
There are usually two things you can do in this case to preserve the data between container restarts or even new containers:
Create a named volume and mount the data there. To do this you can run docker volume create mysqldata. Then, when starting the container mount the data with -v mysqldata:/var/lib/mysql. This volume will persist even after you stop or delete your MySQL container.
Bind mount the data to a host folder. Instead of creating a volume, you can just mount a folder such as -v /mnt/mysqldata:/var/lib/mysql. This will persist all your MySQL data on the host at /mnt/mysqldata.
Though, these are not the only ways to persist data, they are two built-in methods. There are also Docker volume plugins that allow you to use other storage mediums (examples might be https://github.com/rancher/convoy for NFS and https://github.com/NetApp/netappdvp for NetApp).
docker exec -it mysql bash
chown -R mysql:mysql /var/lib/mysql
if you change permission of volume in host, above code will correct permission denied for root.