Can I run two mysql commands when connecting to a docket container? - mysql

I have a brief script to connect to a docker container and login:
docker exec -it mysql mysql --password=password
The first mysql is the name of the container, the second is the first command I'd like to run.
This works fine, I end up with a mysql prompt inside the container, but then I have to run something like:
use mydatabase
I'd rather do this as a one-liner, just to save time. Is this possible?

To run multiple commands in one line on a container you can write something like this:
docker exec -it <container> <bash|sh> -c "<command1> && <command2> && <command3>"
In your case, for instance:
docker exec -it mysql sh -c "mysql --password=password && use mydatabase"
Using && delimiter instead of ; will execute the next command only if the previous one has completed successfully (0 exit code)

Related

pass password from .env on command to mysql inside docker

I basically know nothing about docker. And not that much more about bash neither. So:
There's a command in the README of a Laravel project i'm working on, that shows how to fill some data on local MySQL docker image, by sending a queries from a file located in the HOST.
docker exec -i {image} mysql -uroot -p{password} {database} < location/of/file.sql
What i want to do is "hide" the password from README, and make it read from .env file
So, i want to do something like this:
docker exec --env-file=.env -i {image} mysql -uroot -p$DB_PASSWORD {database} < location/of/file.sql
I've tested that docker ... printenv does show the variables from the file. But echoing one of then outputs a blank line: docker ... echo $DB_PASSWORD and running MySQL command using it, gets me "Access denied for user 'root'#'localhost'"
I've tried run the MySQL command "directly": docker ... mysql ... < file.sql and also "indirectly": docker bash -c "mysql ..." < file.sql.
You should prevent your shell from expanding the local variables (by single-quoting, or by escaping $)
This should be passed to containers shell and expanded there:
docker exec --env-file=.env -i {image} bash -c 'mysql -uroot -p$DB_PASSWORD {database}' < location/of/file.sql
It could possibly be two cases.
Check the key name in your env file and the docker run command
Check the path of the env file you are mapping to.

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/

Why do some docker exec commands not always work in a batch file?

I can run a docker exec command that calls mysql fine in PowerShell:
docker exec -it my_container_name mysql -e "source my-query.sql" -uroot -pMyRootPassword
The container is running, the sql file is there, and the query runs successfully.
But when I copy and paste the exact same command into a .bat batch file that contains some other docker commands, and run it (via PowerShell), I get an error message "Can't connect to local MySQL server through socket". If I put it into a separate batch file and run it on its own, it works fine.
There is a similar issue asked here - https://forums.docker.com/t/run-docker-exec-command-from-batch-file-in-windows-10/48163 - with no answer.
More Detail:
My complete batch file (simplified down) looks something like this:
set "container=my_container_name"
set "mysqlRootPassword=MyRootPassword"
docker stop %container%
docker rm %container%
docker run --name=%container% --env="MYSQL_ROOT_PASSWORD=%mysqlRootPassword%" --detach mariadb:10.5.6
docker exec -it %container% mysql -e "show databases;" -uroot -p%mysqlRootPassword%
When run this gives Error: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2). Everything above the exec runs OK.
Now if I run the exact same exec command in a separate batch file afterwards...
set "container=my_container_name"
set "mysqlRootPassword=MyRootPassword"
docker exec -it %container% mysql -e "show databases;" -uroot -p%mysqlRootPassword%
...Then it connects fine, the SQL command works fine, and shows the databases as expected.
Hm, it looks like it may be a timing issue. Might be trying to exec into the container before the container is fully spun up. Try adding a sleep statement before the exec.
PowerShellGuy is correct that it ended up being a timing issue. Apparently just because the docker container is up and running, it doesn't mean that the database is ready for connections. I solved the issue by adding a loop that waits and checks for a connection. There might be a better way to do this, but it solved my issue in the near term.
set "container=my_container_name"
set "mysqlRootPassword=MyRootPassword"
docker stop %container%
docker rm %container%
docker run --name=%container% --env="MYSQL_ROOT_PASSWORD=%mysqlRootPassword%" --detach mariadb:10.5.6
for %%i in (1 2 3 4 5 6 7) ^
do (
docker exec -it %container% mysql -e "show databases;" -uroot -p%mysqlRootPassword% && GOTO :connected || (
#echo "Trying again (%%i)..."
TIMEOUT 2
)
)
#echo "Timed out"
EXIT
:connected
docker exec -it %container% mysql -e "source my-query.sql" -uroot -p%mysqlRootPassword%

Is it possible to pass a determined $MYSQL_ROOT_PASS to MySQL docker from outside docker? If so how?

So I'm writing an install script and because I haven't been able to find a solid MySQL replacement on armfh (the db must be MySQL compatible), I'm using a community on that works, however it does not initiate the db as it should. it requires me to pass the following argument.
mysql -h"db" -u"root" -p"$MYSQL_ROOT_PASSWORD" "$MYSQL_DATABASE" < /docker-entrypoint-initdb.d/1_db.sql
From inside the docker. Problem is I want this to flow naturally as a smooth install script. I've tried using the following command to pass the document and get a password prompt:
docker exec -it db bash -c "mysql -h"db" -u"root" -p"$MYSQL_ROOT_PASSWORD" "$MYSQL_DATABASE" < /docker-entrypoint-initdb.d/1_db.sql"
If also tried:
docker exec -it db bash -c "mysql -h'db' -u'root' -p'$MYSQL_ROOT_PASSWORD' '$MYSQL_DATABASE' < /docker-entrypoint-initdb.d/1_db.sql
FwIW: I used the MYSQL_ROOT_PASSWORD=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 64 | head -n 1) to define the password. And if I manually enter the docker send command 1 (in quotations) the db initiates.
So to summarize my question: Is it possible to pass a command like the above to activate the 1_db.sql file from outside docker?
Any help would be amazing! Thanks in advance!
Is it possible to pass a command like the above to activate the
1_db.sql file from outside docker?
you can try something like
cat 1_db.sql | docker exec -i test bash -c 'mysql -uroot -p$MYSQL_ROOT_PASSWORD $MYSQL_DATABASE'
also, remember when you try exec bash -c "mysql -uroot -p$MYSQL_ROOT_PASSWORD"it will for MYSQL_ROOT_PASSWORD in the host, not inside container, use single quotes.
determined $MYSQL_ROOT_PASS to MySQL docker from outside docker? If so
how?
docker exec -i test bash -c 'echo mysql docker password is $MYSQL_ROOT_PASSWORD'

Run mysql script through docker?

Hi there,
I'm trying to create a docker container that will run a mysql script which generates a database and then table. My Dockerfile looks like this:
FROM mysql:latest
WORKDIR /
ADD . /
EXPOSE 3306
CMD mysql -u "root" -proot < "schema.sql"
I create the image through this:
docker build -t database .
And then I run it through this:
docker run -d -p 3306:3306 database
At this point the script should be run- however instead I just get this random line in the terminal:
0b2503b42482a4fa840351925845392e1abdf6022b23447187ff49ed4f0fa05b
Grateful for your help!
you are getting 0b2503b42482a4fa840351925845392e1abdf6022b23447187ff49ed4f0fa05b because of the option "-d" in docker run -d -p 3306:3306 database which indicates that the container will be running in the background. You can remove it if you dont want that.
For more info about this please check this link: Docker run command
The container should be running though.
Check the command Docker ps that will show you infos about running containers.
Hope this helps!
don't worry, this is 0b2503b42482a4fa840351925845392e1abdf6022b23447187ff49ed4f0fa05b ID from container.
You can use id to stop or check status or collect information of container. if you want check your command is work or not, you can run with this command docker run -it database bash or docker run -it database sh
Hope this helps.
Best Regards
as you are overriding CMD entry command in docker file, you would need to start mysqld service explicitly
CMD [mysqld, mysql -u "root" -proot < "schema.sql"]