On my centos host, running mysql command through docker exec is hung forever:
$ docker exec mysql /usr/bin/mysql -u root --password=passwd123 < mysql.backup.sql
The mysql DB is accessible using mysqlworkbench just fine.
Thanks for your help.
You can try following for backup
docker exec mysql /usr/bin/mysqldump -u root --password=passwd123 DATABASE > backup.sql
Instead of mysql you should use mysqldump ( Mysqldump is a command-line utility that is used to generate the logical backup of the MySQL database. )
If your purpose is restore, then
cat backup.sql | docker exec -i mysql /usr/bin/mysql -u root --password=passwd123 DATABASE
I was missing -i parameter in my original command.
This command works perfectly:
$ $ docker exec -i mysql /usr/bin/mysql -u root --password=passwd123 < mysql.backup.sql
Related
I want to import and export .sql into mysql or postgres in docker container
Similarly for postgres:
dump:
docker exec -it CONTAINER /usr/bin/pg_dump -U USER DATABASE > backup.sql
load:
docker exec -i CONTAINER /usr/bin/psql -U USER DATABASE < backup.sql
That is for a single database. If you want to do this for all databases and global objects, use pg_dumpall instead.
For a user with a password, you'll want to set up the password non-interactively (e.g., with PGPASSWORD).
Backupdocker
docker exec -it CONTAINER /usr/bin/mysqldump -u USER -pPASSWORD DATABASE > backup.sql
Restore backup.sql
docker exec -i CONTAINER /usr/bin/mysql -u USER -pPASSWORD DATABASE < backup.sql
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 have a scenario. I need to connect to a windows server from a unix machine using sshpass and execute a MySQL query remotely. Below is the code I want to run:
sshpass -p 'passwd' ssh -q -o StrictHostKeyChecking=no Administrator#IP mysql -uroot db_name -P 3306 -e "select date(lastupdate),circlecode,count(*) from some_table where date(lastupdate)='2017-05-17' group by 1;"
If I separate the two commands i.e. login and MySQL query it runs fine
sshpass -p 'passwd' ssh -q -o StrictHostKeyChecking=no Administrator#IP
mysql -uroot db_name -P 3306 -e "select date(lastupdate),circlecode,count(*) from some_table where date(lastupdate)='2017-05-17' group by 1;"
I have tried multiple solutions for similar cases I found on net but none works.
Please help. Thanks in advance.
I am creating an image in the docker to the mysql install mode, but it is giving error to start mysql.
My Dockerfile
The error occurs when processing and line 25 of Dockerfile:
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)
I found that the error occurs because mysql is not running. The docker print below to rotate the line 22:
bin boot dev etc home lib lib64 media mnt opt proc root run sbin scripts srv sys tmp usr var MySQL is stopped.
Stack trace complete
Any suggestion?
The docker daemon will execute RUN command one by one and commit the result, and seems your mysql service status is not committed to the image. To solve this problem, you may try these ways
Put all commands into one RUN command
RUN echo $(service mysql restart) && echo $(service mysql status) && sudo mysql -uroot -pmysql_pass -e "CREATE DATABASE wordpress;" && sudo mysql -uroot -pmysql_pass -e "CREATE USER 'wordpressuser'#'%';" && sudo mysql -uroot -pmysql_pass -e "SET PASSWORD FOR 'wordpressuser'#'%'= PASSWORD('${mysql_pass}');" && sudo mysql -uroot -pmysql_pass -e "GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpressuser'#'%'; FLUSH PRIVILEGES;"
initialize your mysql in entrypoint with a bash script.
init_mysql.sh
#!/bin/sh
sudo mysql -uroot -pmysql_pass -e "CREATE DATABASE wordpress;"
sudo mysql -uroot -pmysql_pass -e "CREATE USER 'wordpressuser'#'%';"
sudo mysql -uroot -pmysql_pass -e "SET PASSWORD FOR 'wordpressuser'#'%'= PASSWORD('${mysql_pass}');"
sudo mysql -uroot -pmysql_pass -e "GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpressuser'#'%'; FLUSH PRIVILEGES;"
and add following lines to dockerfile
ADD init_mysql.sh /
RUN \
cd / \
chmod 777 init_mysql.sh
CMD ./init_mysql.sh
You need to figure out why MySQL isn't starting. Log into the environment and manually try to start MySQL and see what the error is.
Have you considered basing your image on one of the official MySQL Docker images?
https://registry.hub.docker.com/_/mysql/