MySQL data directory stays default with -v flag in docker image - mysql

I am trying to start MySQL using docker image, I wanted to have a look at the binlog files, however I couldn't find them in /var/lib/mysql. From a few stackoverflow and Google reads, potential reason could be that mysql doesn't have permissions to write in /var/lib/mysql.
So I tried providing a different path using -v flag while starting the docker using the command docker run -it --rm --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=debezium -e MYSQL_USER=mysqluser -e MYSQL_PASSWORD=mysqlpw -v /home/username/mysql debezium/example-mysql:1.1
However, even after this, datadir variable in client still remains /var/lib/mysql. Can someone help me in this?
Using docker run -it --rm --name mysqlterm --link mysql --rm mysql:5.7 sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD" to start the MySQL client.

You are running client on different container and expect yo find logs there?
if you want see log files, you should run docker exec mysql bash, sometimes bash is not available, then use sh.

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.

run a docker mysql container and open it in one command

I want to run and open a mysql Cli in docker just with one command . Something like this is not working:
docker run --rm -it -p 33060:3306 --name mydb -e MYSQL_ROOT_PASSWORD=secret mysql mysql -p
I know I can connect to mysql after running my container this way
docker -it docker exec -it mydb mysql -p
but i want to do it in one liner.
Thanks
(Updated)*****
Seems that you can do it in version 8 calling MySQLsh at the end of the command. But unable to do it for previous versions
docker run --name=mk-mysql -p3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -it mysql/mysql-server:8.0.20 mysqlsh
The database server and client are two separate programs. A container only runs one program, so you can't run both the server and the client in the same container, both as the main process. You could write a script that starts the container and then runs mysql to connect to it, but that's about the best you can do.
#!/bin/sh
docker run -d -p 33060:3306 --name mydb -e MYSQL_ROOT_PASSWORD=secret mysql
exec mysql --host=127.0.0.1 --port=33060 --connect-timeout=60 --wait --password
If you're trying to do this to create a database or do other first-time initialization, you can bind-mount an initialization script into /docker-entrypoint-initdb.d and it will run as part of the database setup (only the very first time the database is started).
# Create the storage for the database
# (delete and recreate to rerun the init script)
docker volume create mysql-data
docker run \
-v mysql-data:/var/lib/mysql \
-v $PWD/init.sql:/docker-entrypoint-initdb.d/init.sql \
... \
mysql
If you're just trying to experiment with SQL commands, a serverless database like SQLite might fit your needs better.
the -p parameter is for the ports to be published and should not be part of the -it interactive, that should be your error,
Have a read of the docker run command, in the docker documentation,
https://docs.docker.com/engine/reference/run/

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.

I can't seem to get my local shared MySQL data volume to persist on Docker for Windows

I'm using the latest Docker for Windows on Win 10 with following run command:
docker run -d -e APP=db -e MYSQL_ROOT_PASSWORD=password -v /C/Users/myuser/Documents/mysql_data:/var/lib/mysql --name=db -p 3306:3306 mysql:latest
When I start the container and check the ~/Documents/mysql_data directory, there's nothing there.
The data persists reboot, but when I stop, remove and re-run the container, the data is gone.
I am on a security-restricted Windows 10 laptop with 16GB of RAM, where sharing files works fine for my PHP container with a similar run command.
I expected to have MySQL's ibData files in my local mysql_data directory and the ability to persist container re-run. Is this a bug, or am I getting the expected behavior or doing something wrong?
Edit: When I docker exec -it bash db into the database container, there's this output when I df -h
//10.0.75.1/C 236G 109G 127G 47% /var/lib/mysql
you can show docker logs:docker logs db
try follow:
docker run -d -e APP=db -e MYSQL_ROOT_PASSWORD=password -v /C/Users/myuser/Documents/mysql_data:/var/lib/mysql:rw --name=db -p 3306:3306 mysql:latest