Mysql command from host not taking change - mysql

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

Related

How to import and export .sql into mysql or postgres in docker container

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

mysql command inside mysql container hung forever

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

How to execute mysqldump command from the host machine to a mysql docker container

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.

Docker - MySQL commands within Dockerfile using RUN (ERROR 2002)

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

Run MySQL query on remote machine through ssh in command line

I am trying to run MySQL query on remote machine with this command:
ssh user#192.168.2.26 "mysql -uroot -proot -e \"use test";""
I am not able to use that database.
Please suggest a working command.
Try this:
mysql -h host -u root -proot -e "show databases;";
Try this:
ssh root#host "mysql database -e 'query to run on table_name; more queries to run;'"
Same can be done with user#host if that user has permission to execute SQL queries let alone launch mysql in general. Using -e is the same as --execute, which will run whatever you put within the trailing quotes (single or double) and quit. The standard output format would be the same as you would see using --batch.
MySql seems to have a special command line syntax which includes the database.
mysql -u user -p -e 'SQL Query' database
This documentation is rather old but I got it to work
http://www.cyberciti.biz/faq/run-sql-query-directly-on-the-command-line/
Final working command with ssh:
ssh user#host "mysql -u user -e 'show tables;' databasename"
This ended up working for me in a bash script:
query='USE [database]; SELECT ...'
mysql='mysql -u [username] -p[password] -e '"'""$query""'"
ssh [username]#[server] -t "$mysql"
If you want to make it more safe then add a prompt for the password instead of storing it somewhere potentially unsafe.
This worked for me after a few tests (basically same answer as #King-Wzrd):
ssh -t kom "mysql -uroot -p -e 'show databases;'"
ssh -t kom "mysql -uroot -p < /home/ling/websites/jin_test/.deploy/tmp.sql"
The "trick" was the quotes around the command.
The -t option allows for prompting password interactively via the remote shell.
The kom here is just a ssh config identifier defined in my ~/.ssh/config file (see more here: https://www.cyberciti.biz/faq/create-ssh-config-file-on-linux-unix/).
Running this from my Host environment against MySQL within my Homestead VM produced a nice result... although I did have to set the root password from within the VM first in order for it to work.
ssh vagrant#192.168.10.10 mysql -h localhost -u root -p -e "'SELECT * FROM user;' mysql";