Cannot find mysql docker data files on my volume local host - mysql

I created a simple mysql docker instance as
docker run -d
--name mysql3
-p 3306:3306
-v db:/var/lib/mysql
-e MYSQL_ROOT_PASSWORD=rsecret
-e MYSQL_DATABASE=mkt_data_db
mysql/mysql-server:8.0
After that I logged in and created tables
$docker exec -it mysql3 bash
#mysql -u root -p
use mkt_data_db;
create table price (ticker char(30), eod_date date, close float);
insert into price(ticker, eod_date, close) values ('xyz', curdate(), 100.1);
When I logout and check db directory, its empty
$ ls -al db/
total 0
drwxr-xr-x 2 accnt staff 64 21 Jun 23:38 .
drwxr-xr-x 6 accnt staff 192 21 Jun 23:38 ..
when I stop+rm+run docker again, I do see the the 'price' table and the data within it
Where is the database stored on my local host (mac)?
I would ideally like to move these database files (data) to another computer, provide new DB volume on docker run there and see the data on that machine.

This is an interesting problem, and kudos to you for posting the answer!
I'd be curious to know what the result of docker inspect mysql3 would show (look for the "Mounts" section in the large amount of output) when NOT specifying the full path (like in your original question).
An afterthought: I like to use pwd when creating a shared volume (assuming you are running the command in the desired directory):
docker run -d
--name mysql3
-p 3306:3306
-v $(pwd):/var/lib/mysql
-e MYSQL_ROOT_PASSWORD=rsecret
-e MYSQL_DATABASE=mkt_data_db
-e MYSQL_USER=mkt_data_usr
-e MYSQL_PASSWORD=usecret
mysql/mysql-server:8.0

The above problem was solved by providing absolute path with -v flag
i.e.
docker run -d
--name mysql3
-p 3306:3306
-v /Users/accnt/db:/var/lib/mysql
-e MYSQL_ROOT_PASSWORD=rsecret
-e MYSQL_DATABASE=mkt_data_db
-e MYSQL_USER=mkt_data_usr
-e MYSQL_PASSWORD=usecret
mysql/mysql-server:8.0
i.e. change -v db:/var/lib/mysql to -v /Users/accnt/db:/var/lib/mysql
After that $ls -l /Users/accnt/db showed my entire db contents (including mkt_data_db and price table inside

Related

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

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

How to attach a volume to a Bluemix container

I’m setting up a container on Bluemix using ice from the command line, but every time I try to attach a volume to a container it simply doesn’t work. The mounted folder isn't created in the root directory.
My command is:
ice create -p 80 -p 22 --name test --memory 1024 --volume notebooks:/notebooks registry.ng.bluemix.net/repository/app:latest
Docker gives you the option to create the volume yourself or allow Docker to create it for you. Either one of these will work:
docker run --name mysql_test -v /etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d jw_mysql:latest
OR
docker run --name mysql_test -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d jw_mysql:latest
For IBM Containers, the situation is different: you need to create a volume before you can use it. So only this will work:
cf ic volume create dbstorage
cf ic run -p 3306 --name cf_mysql_test -v dbstorage:/etc/mysql/config/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d registry.ng.bluemix.net/jw_image_reg/jw_mysql:latest
(Assuming you want to use port 3306.)

Docker MariaDB/Mysql dump

How can i mysqldump from running container on https://hub.docker.com/_/mariadb/ ?
I cant find any useful documentation or data?
Any method for backup and restore database.
This is my my continaer run command :
docker run --name myaapp-mariadb -v /databases/maria:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=password -d mariadb:10
If we assume you created the mariadb server container this way:
docker run --name some-mariadb -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mariadb:latest
Then you access it from another client container:
docker run -it --link some-mariadb:mysql \
--rm mariadb:latest \
sh -c 'exec mysqldump -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD" database_name' > database_name_dump.sql
There's lots more helpful usage tip in the mysql official image page.
Accepted answer stands accepted & correct in all its sense. Adding, this for the case where we have mapped the database to an external volume.
So, for example if the container was created using below command
docker run --name mysqldb -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -v /dir_path_on_your_machine/mysql-data:/var/lib/mysql -d mariadb:latest
then, we can execute the below command from cmd line or terminal
docker exec mysqldb mysqldump --user=root --password=password dbname > /dir_path_on_your_machine/mysql-data/dump/db.sql
However, the dump created using above commands will not dump stored procedures, functions and events. We would need extra params with the in order to do that
--triggers Dump triggers for each dumped table.
--routines Dump stored routines (functions and procedures).
--events Dump events.
So, we can modify our command to include the above params for desired result.
Sample update command
docker exec mysqldb mysqldump --routines --triggers --user=root --password=password dbname > /dir_path_on_your_machine/mysql-data/dump/db1.sql
In case, you encounter any import related error ,, check if this helps.