Docker for Windows
I have a machine set up with 2 containers. They are both running the standard mysql image. One is set up to be the server, and I am linking to it from the other and attempting to run mysql commands.
Ideally, I would like to be able to do this all through the command prompt so I can call it through Python.
I was able to run a few commands error-free and attempted to place my name into the Username table of myDB.
When I run the command:
docker exec sqlserverClient mysql -uroot -ppassword -e"use myDB" -e"select * from Usernames"
I get back the output:
Warning: Using a password on the command line interface can be insecure
And nothing else.
Where is my table? Even if the insert went wrong, shouldn't I at least see a blank table?
I expected to see something like this:
+------------+-------------+
| firstname | lastname |
+------------+-------------+
| First | Last |
+------------+-------------+
"docker logs sqlserverClient" doesn't have it. So where did it go?
Try with docker exec -it, to have tty and the output
But remember docker exec is for debug only.
The best practice is to have a docker container with:
an entrypoint set to sqlserverClient
a cmd that you pass in parameter when running a transient container.
That is:
docker run --rm -it sqlimage mysql -uroot -ppassword -e"use myDB" -e"select * from Usernames"
Once the command is executed, the container stopped and is rmeoved.
That means you can have an alias like:
alias drs='docker run --rm -it sqlimage'
And call:
drs mysql -uroot -ppassword -e"use myDB" -e"select * from Usernames"
Related
I want to create mysql dumps for a database which is running in docker container. However I do not want to get into the container and execute the command but do it from the host machine. Is there a way to do it. I tried few things but probably I am wrong with the commands.
docker exec -d mysql sh mysqldump -uroot -pSomePassword DBName > /dumps/MyNewDump.sql
docker exec -d mysql sh $(mysqldump -uroot -pSomePassword DBName > /dumps/MyNewDump.sql)
docker exec -d mysql mysqldump -uroot -pSomePassword DBName > /dumps/MyNewDump.sql
the dumps directory is already bind to the host machine.
These commands are seems not the right way to do it or probably not the right way to do it at all. These always ends up with an error:
bash: /dumps/MyNewDump.sql: No such file or directory
But if I just run mysqldump -uroot -pSomePassword DBName > /dumps/MyNewDump.sql inside the container it works fine.
This worked on my end(just replace the container ID):
docker exec 1d3595c0ce87 sh -c 'mysqldump -uroot -pSomePassword DBName > /dumps/MyNewDump.sql'
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump takes the usual MySQL CLI options to connect to a server running somewhere else. That means you can run it directly from the host, without needing docker exec (administrator) permissions.
mysqldump -h127.0.0.1 -uroot -pSomePassword DBName > MyNewDump.sql
In contrast to the docker exec forms, this creates the dump file on the host, but that's probably what you want.
I want to run a script on a mysql container, but when I am running it from the host it does not make the needed changes.
The command that I run on my docker host is
docker exec mysql mysql -u root -ppassword perks_camp < ./1.0.1.sql
it says mysql: [Warning] Using a password on the command line interface can be insecure. which is expected, and it seems that the script inside is ran, but when I log in to the container's db, the changes are not applied.
On the other hand, when I copy the file to the container and run the same command from inside the container mysql -u root -ppassword perks_camp < ./1.0.1.sql
, the changes are applied.
What is wrong here? What am I missing?
Backup
docker exec CONTAINER /usr/bin/mysqldump -u root --password=root DATABASE > backup.sql
Restore
cat backup.sql | docker exec -i CONTAINER /usr/bin/mysql -u root --password=root DATABASE
https://gist.github.com/spalladino/6d981f7b33f6e0afe6bb
I want to run a query agains mysql database stored in openshift.
As far I know there are two ways to do that:
1 - Make a port forward of the mysql pod and run the query pointing to that port from my local environment.
oc port-forward mysql-9-999aa 3306:3306
2 - run a rsh of the pod and execute mysql commands
oc rsh mysql-9-999aa
> mysql ...
However I want to ask if there is a way to run the query at once with exec command, something like:
oc exec mysql-9-999aa mysql --user=test --password=test myDatabase -e "SELECT * FROM test;"
I've tried that and it's not working. Anybody knows if it's possible to run a MySQL command with oc exec?
Thanks,
Try the following:
oc exec mysql-9-999aa -- bash -c "mysql --user=test --password=test myDatabase -e 'SELECT * FROM test;'"
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.
Im using this tutum-docker-mysql dockerfile, to get up and running with a docker image with mysql installed in it,the docker file creates a user with the name root and no password, what i need is to add something like this to the dockerfile:
RUN mysql -uroot -p"" && mysql create database test;
So when I build an image from the docker file, the database should already be there.
I was able to accomplish the goal of adding a database to the tutum-docker-mysql image by doing the following.
git clone https://github.com/tutumcloud/tutum-docker-mysql.git
cd tutum-docker-mysql
vi create_mysql_admin_user.sh
Inside of that .sh file I added a line, right below the two "mysql -uroot" lines that are already there. I simply added:
mysql -uroot -e "create database test;"
After that change to that .sh file, I simply built the docker image.
docker build -t tutum/mysql .
After the docker image was built, I could run it with something like:
docker run -d -p 3307:3306 tutum/mysql
Once the container is running, you need to know the password to use and the ip address of the container. To get the password you simply do
docker logs 2976a81f1a9b19787d9bde893c831b7e6586d7c8391ccd222ad29b02c282d896
But of course use the container id, that was returned from the "docker run" command above. Now that you have the password, you need the ip address. I get that by doing.
docker inspect 2976a81f1a9b19787d9bde893c831b7e6586d7c8391ccd222ad29b02c282d896
And looking at the "Gateway" address. With the password and the ip address I was then able to do this mysql command from OUTSIDE of the container.
mysql -uadmin -pJcM5FCphMOp4 -h172.17.42.1 -P3307
Where the ip address and the password are the values I got from the previous two docker commands. After that command has run, I can then issue a "show databases" command with the following results.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql>
I fought for a while trying it by modifying the Dockerfile. I think it might be possible, but after a while, I found the above solution a lot quicker and simpler.
You can pass ON_CREATE_DB env parameter on container startup.
Example from tutum README.md
docker run -d -p 3306:3306 -e ON_CREATE_DB="newdatabase" tutum/mysql