I have a script which build my docker env.
I have this line:
docker exec -it guac-mysql /bin/bash
mysql -u root -p123456
CREATE DATABASE guacamole;
CREATE USER 'guacamole' IDENTIFIED BY 123456;
GRANT SELECT,INSERT,UPDATE,DELETE ON guacamole.* TO 'guacamole';
FLUSH PRIVILEGES;
quit
When I run, the script gets in the bash on the container, but the other lines dont run, I just have to run them one by one.
How can I exec and then inject the script?
Thanks :)
you can't run this as a single script. the docker exec command will create a shell into the container. it won't run the rest of it until you exit the container. then it will run the mysql etc...
you need to run docker exec -it guac-mysql /bin/bash
and then you can create a separate script to run the mysql command within the container itself
but your current script won't work. again, it will run mysql and then wait for that to exit before running the create database, etc.
see the docs on executing scripts from a file: https://dev.mysql.com/doc/refman/5.7/en/mysql-batch-commands.html
Related
I'm trying to run a mysqldump from inside the db service of my Docker Compose app.
Problem: Instead of dumping the file to /tmp/mydb.sql, it is printing the output to the screen.
Here is my command: docker compose exec db mysqldump -uroot mydb > /tmp/mydb.sql
There is no file in /tmp when this command is done running. I also confirmed that /tmp is writable.
How can I have the command write to /tmp/mydb.sql?
Using the shell redirection can be tricky because by default, it is interpreted by the shell where you ran the docker command, not the shell inside the container.
If you want to ensure the output is redirected by the mysqldump command inside the container, I suggest you use mysqldump with the --result-file=/tmp/mydb.sql option (see the link for documentation) instead of getting confused with shell redirection.
does any one has an idea how to execute MySQL command inside Kubernetes POD from GitLab Runner ?
My Problem:
I want to create two View Table for my Database that is setup and ready inside a GitLab Pipeline.
My current approach:
1 I read out the wordpress pod infos
MSPOD=$(kubectl get pods --namespace=default -o=jsonpath="{.items[*].metadata.name}" -l app=wordpress,tier=mysql)
2 I try to execute the create table view as single command as i can not sh into the POD via Runner.
kubectl exec $MSPOD -- mysql --database=wordpress --password='M*****?' -e "CREATE VIEW ...;"
But this does not work it actully tries to run the single items of the command in the Terminal.
It also does not work as a embedded execution
kubectl exec $MSPOD -- $(mysql --database=wordpress --password='M*****?' -e "CREATE VIEW ...;")
Causing the same error.
The init container with MySQL client should work for you. Your SQL code can be provided as a configmap.
I'm setting an script to configure Docker environments for developer local machines. A specifically part of the script consists to create a temporary container with a local volume to set the database i'll use in next step. this is the code used in the script:
docker run -d --name mysql_temp -v ~/dev/mysql:/var/lib/mysql/data -e MYSQL_ROOT_PASSWORD=test -p 3306:3306 centos/mysql-57-centos7:latest
the next step in the script is this line:
mysql -u root -h 127.0.0.1 --protocol=tcp --password=test -e "CREATE DATABASE db_test;"
then, the script returns me this error:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 2
Curiously, if i force the exit with exit command after the first command and i execute the second one by my hand, directly in the terminal, it works and creates me the database.
Any idea what is this happening?
thanks in advance,
In your script, when the first line executes, it creates the docker container. But it takes time to initialise the mysql server on the container. The script executes the second line without waiting for mysql to initialise. Hence it cannot connect.
I'm assuming you have written a shell script. Try the following script.
docker run -d --name mysql_temp -v ~/dev/mysql:/var/lib/mysql/data -e MYSQL_ROOT_PASSWORD=test -p 3306:3306 centos/mysql-57-centos7:latest
sleep 10
mysql -u root -h localhost --protocol=tcp -ptest -e "CREATE DATABASE db_test;"
Note the second line having sleep 10. Basically this will create a delay of 10 seconds before executing the next command. Try varying the sleep time to higher if it still doesn't work.
Also change 127.0.0.1 to localhost and --password=test to -ptest
Note: You said that if you exit the script and run the mysql command manually, it works. Thats because by the time you tried running manually, the container has initialised mysql. Try running the docker run command in one terminal window. Once the container starts, try running the mysql command immediately in another terminal window to see if it works as quickly as you expected.
I have a mysql docker container and it is up and running. I can get into it and see the mysql prompt. I do not want to mount external storage on it. Everytime I start this container, I want to execute a test.sql script from my file system to create DB and do few such actions. When I run the script which exists in my current working directory, it complaints. I know this is trivial but I am unable to catch the issue.
/microservices/mysqlcontainer]docker exec 3a21e5e3669d /bin/sh -c 'mysql -u root -ppwd <./test.sql'
/bin/sh: ./test.sql: No such file or directory
Since your script is outside the container, there's no need to catch the shell input redirection. You should be able to run the following:
docker exec -i 3a21e5e3669d mysql -u root -ppwd <./test.sql
I am using Docker to create a dockerfile with mysql as the base image:
FROM mysql
#set root pass
ENV MYSQL_ROOT_PASSWORD password
#update linux
RUN apt-get update
#create database
RUN mysql -u root -ppassword -e "CREATE DATABASE dbname"
#install vim
RUN apt-get install vim -y
The dockerfile fails on the step where I try to create a database, it doesn't finish building and i receive this error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
When I remove the #create database run command the dockerfile will build and I am able to run a container from that image. I know that it isn't a problem with the mysql server as I can enter the container and run the mysql command manually with success and the service status is running.
Using an environment variable i.e MYSQL_ROOT_PASSWORD within the file also allows me to create a database successfully but this will only work with a single database, I need to be able to use the mysql command to make queries, such as creating additional databases / assigning users etc.
This may be because I need to specify the host and port of the docker container but this still does not allow me to connect
RUN mysql -u root -ppassword -h 127.0.0.1 -P 3308 -e "CREATE DATABASE dbname"
Strangely, doing this also often crashes the container and puts it in a state where it will crash again on start-up every time that I try to restart it again.
I think the issue might be that in the service hasn't started within the container used to build your Dockerfile.
Try starting and configuring MySQL server within a single step. As a reference please check this file: https://github.com/dockerfile/mysql/blob/master/Dockerfile
Use below-given commands in your Dockerfile:
RUN service mysql restart && echo 'CREATE DATABASE db_name;' | mysql -uroot -
pYOUR_ROOT_PASSWORD
Had the very same problem: When starting the container and running a set of RUN instructions, or .sh or .sql scripts in /docker-entrypoint-initdb.d/ no connection to the database server could be established.
I found the solution by a comment of #wpalmer on the mysql-image:
The init scripts run by the entrypoint, internally, use the variable "${mysql[#]}" to call mysql (for example, when loading .sql files placed in the docker-entrypoint-initdb.d directory. Any .sh files which are processed by the entrypoint are included by "sourcing" them, meaning that variable is available for use by any .sh files which are run).
So what this means for you, instead of providing the plain mysql command with user, pass etc. as in
RUN mysql -u root -ppassword -e "CREATE DATABASE dbname"
use the placeholder instead:
RUN "${mysql[#]}" -e "CREATE DATABASE dbname"
You can try to build other image and run the create DB from there.
Example of docker-compose.yml
web:
build: web
links:
- "db:db.local"
entrypoint: entrypoint.sh
db:
build: db
environment:
MYSQL_ROOT_PASSWORD: password
command: mysqld
For entrypoint.sh you put something like this:
#!/bin/sh
#this is a hack to wait until the DB image is up and the port is open
until mysqladmin -u root -ppassword -e -h db.local ping; do
echo "$(date) - waiting for mysql"
sleep 3
done
if ! mysql -u root -ppassword -e -h db -e 'use dbname'; then
mysql -u root -ppassword -e "CREATE DATABASE dbname"
fi
exec "$#"
You can copy your queries as .sql file into "/docker-entrypoint-initdb.d" container directory. mysql will execute them after starting container
COPY ./init/db.sql /docker-entrypoint-initdb.d/
read official doc https://hub.docker.com/_/mysql?tab=description&page=1
Initializing a fresh instance