ExecWait and MySQL with Parameters Not Working - mysql

If I run this from a command prompt it works:
C:\Program Files\MariaDB 5.5\bin>MySQL.exe -u root -p --password=xyz --port=3322 --protocol=TCP < "C:\temp folder\CREATE_database.sql"
So I know the .sql script is fine.
But in my NSIS script I see a command prompt window pop up but disappear quickly and the database isn't created:
ExecWait '"C:\Program Files\MariaDB 5.5\bin\mysql.exe" -u root -p --password=root --port=3322 --protocol=TCP < "C:\temp folder\CREATE_database.sql"'
Am I not passing in the parameters to MySQL.exe properly?

ExecWait just creates a simple process, it does not support stdin/stdout redirection.
You can use cmd.exe as a helper:
Section
nsExec::ExectoStack 'cmd.exe /c if 1==1 "c:\path\to\myapp.exe" -param1 -param2 < "stdin file.txt"'
pop $0
pop $1
DetailPrint "Exit code=$0"
DetailPrint "Output=|$1|"
SectionEnd
or you can use the ExecDos plug-in.

Related

Bash: file not created with '>' command

I'm writing a script to create backups of a MySQL database running in a docker container. The database is correctly up and running.
My current code is
#!/bin/bash
PATH=/usr/bin:/usr/local/bin:/root/.local/bin:$PATH
docker-compose exec -T db mkdir -p /opt/booking-backup
docker_backup_path="/opt/booking-backup/dump_prod_$(date +%F_%R).sql"
copy_backup_path="/root/backup_scripts/booking_prod/dump_prod_$(date +%F_%R).sql"
docker-compose exec db mysqldump --add-drop-database --add-drop-table --user=root --password="pw" booking > "$docker_backup_path"
docker-compose exec db mysqldump --add-drop-database --add-drop-table --user=root --password="pw" booking > "/opt/booking-backup/dump_prod.sql"
[ -d ./backup ] || mkdir ./backup
docker cp $(docker-compose ps -q db):$docker_backup_path $copy_backup_path
However, when I execute it it throws this error:
Error: No such container:path: f0baa241becd20d2690bb901fb257a4bbec8cac17e6f1ce6d50adb9532bbae03:/opt/booking-backup/dump_prod_2019-05-28_14:23.sql
What makes this weirder is that I have the exact same code (but with booking switched out for abc, and with PSQL instead of MySQL) that works correctly.
It appears that this line
docker-compose exec db mysqldump --add-drop-database --add-drop-table --user=root --password="pw" booking > $docker_backup_path
does not create the output file, but when I use tee I can see the contents of the dump and they are correct.
What's going wrong here?
The shell redirections
docker-compose exec db mysqldump ... > "$docker_backup_path"
docker-compose exec db mysqldump ... > "/opt/booking-backup/dump_prod.sql"
# -----------------------------------^ here
... will be expanded by your local shell, not inside the container. Meaning the files are written to your local filesystem not to the container's filesystem.

How to copy file from C to a network folder?

I want to copy a file in C:\file.txt to a netword drive which unc path is \fic-bleu\MPM-DICIR\SERVICE-TUNNELS\
I see in Google i have to use pushd and popd but it only modify the current prompt. And i can't use the mysqldump command from this new prompt.
I tried :
cd C:\xampp\mysql\bin
pushd \\ficbleu\MPM-DICIR\SERVICE-TUNNELS
mysqldump.exe --opt --user=root --host=localhost service_tunnels > service_tunnels.sql
popd \\ficbleu\MPM-DICIR\SERVICE-TUNNELS
And
cd C:\xampp\mysql\bin
mysqldump.exe --opt --user=root --host=localhost service_tunnels > pushd \\ficbleu\MPM-DICIR\SERVICE-TUNNELS\service_tunnels.sql
Maybe i have first to save in my computer the dumpsql file and then copy bu i can't find a way to do that :(
Finally i established this script :
#echo off
cd C:\xampp\mysql\bin
mysqldump.exe --opt --user=root --host=localhost service_tunnels > service_tunnels.sql
C:\Windows\System32\xcopy "C:\xampp\mysql\bin\service_tunnels.sql" "\\ficbleu\MPM-DICIR\SERVICE-TUNNELS\01. MAIN COURANTE - MAINTENANCE\Saves\service_tunnels.sql"
Pause
But they said that there are too many arguments so i found a solution in google that says to add quotes to drive path and now its says that is invalid drive path...
Correct answer, need to map network drive as a letter (Y:\ for me)
#echo off
cd C:\xampp\mysql\bin
mysqldump.exe --opt --user=root --host=localhost service_tunnels > service_tunnels.sql
C:\Windows\System32\robocopy "C:\xampp\mysql\bin" "Y:\01. MAIN COURANTE - MAINTENANCE\Saves" *.sql
Pause

Set up MySQL using Dockerfile

I'm just getting started with Docker and was able to set up MySQL according to my needs, by running tutum/lamp and doing a bunch of exec. For example:
docker run -d -p 80:80 -p 3306:3306 --name test tutum/lamp
...
docker exec test mysqldump --host somehost --user someuser --password --databases somedatabase > dump.sql
docker exec test mysql -u root < dump.sql
However, I'm having issues converting this to a Dockerfile. Specifically, the following results in ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock':
FROM tutum/lamp
EXPOSE 80 3306
...
RUN mysqldump --host=$DB_IP --user=$DB_USER --password=$DB_PASSWORD --databases somedatabase > dump.sql
RUN mysql -u root < dump.sql
You will need to override run.sh in order to do that, because when you run a container it will install mysql for the first time.
That is why you can not connect to mysql prior to that (in my previous answer I wasn't aware of that).
I've managed to execute mysql command by adding this to Dockerfile
FROM tutum/lamp
ADD . /custom
RUN chmod 755 /custom/run.sh
CMD ["/custom/run.sh"]
Then in the same folder create a file run.sh
#!/bin/bash
VOLUME_HOME="/var/lib/mysql"
sed -ri -e "s/^upload_max_filesize.*/upload_max_filesize = ${PHP_UPLOAD_MAX_FILESIZE}/" \
-e "s/^post_max_size.*/post_max_size = ${PHP_POST_MAX_SIZE}/" /etc/php5/apache2/php.ini
if [[ ! -d $VOLUME_HOME/mysql ]]; then
echo "=> An empty or uninitialized MySQL volume is detected in $VOLUME_HOME"
echo "=> Installing MySQL ..."
mysql_install_db > /dev/null 2>&1
echo "=> Done!"
/create_mysql_admin_user.sh
else
echo "=> Using an existing volume of MySQL"
fi
( sleep 20 ; mysql -u root < /custom/dump.sql ; echo "*** IMPORT ***" ) &
exec supervisord -n
This file is the same as /run.sh with one line added to run sql import after 20 seconds to make sure mysql service is up and running (there must be more elegant way to run a command just after mysql is started, of course).

cannot open connection of database in docker

sql docker:
FROM ubuntu:latest
RUN apt-get update
RUN apt-get upgrade -y
RUN apt-get -y install mysql-client mysql-server curl
RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf
ADD database.sql /var/db/database.sql
ENV user admin
ENV password admin
ENV url file:/var/db/database.sql
ENV right WRITE
ADD ./start-database.sh /usr/local/bin/start-database.sh
RUN chmod +x /usr/local/bin/start-database.sh
EXPOSE 3306
CMD ["/usr/local/bin/start-database.sh"]
--------\------
startdatabase.sh
#!/bin/bash
# This script starts the database server.
echo "Creating user $user for databases loaded from $url"
# Import database if provided via 'docker run --env url="http:/ex.org/db.sql"'
echo "Adding data into MySQL"
/usr/sbin/mysqld &
sleep 5
curl $url -o import.sql
# Fixing some phpmysqladmin export problems
sed -ri.bak 's/-- Database: (.*?)/CREATE DATABASE \1;\nUSE \1;/g' import.sql
# Fixing some mysqldump export problems (when run without --databases switch)
# This is not tested so far
# if grep -q "CREATE DATABASE" import.sql; then :; else sed -ri.bak 's/-- MySQL dump/CREATE DATABASE `database_1`;\nUSE `database_1`;\n-- MySQL dump/g' import.sql; fi
mysql --default-character-set=utf8 < import.sql
rm import.sql
mysqladmin shutdown
echo "finished"
# Now the provided user credentials are added
/usr/sbin/mysqld &
sleep 5
echo "Creating user"
echo "CREATE USER '$user' IDENTIFIED BY '$password'" | mysql --default-character-set=utf8
echo "REVOKE ALL PRIVILEGES ON *.* FROM '$user'#'%'; FLUSH PRIVILEGES" | mysql --default-character-set=utf8
echo "GRANT SELECT ON *.* TO '$user'#'%'; FLUSH PRIVILEGES" | mysql --default-character-set=utf8
#if [ "$right" = "WRITE" ]; then
#echo "adding write access"
echo "GRANT ALL PRIVILEGES ON *.* TO '$user'#'%' WITH GRANT OPTION; FLUSH PRIVILEGES" | mysql --default-character-set=utf8
echo "finished"
#fi
# And we restart the server to go operational
mysqladmin shutdown
echo "Starting MySQL Server"
/usr/sbin/mysqld
When I run this docker file of sql it runs.
But when I link it to application using the command
$ docker run --link mysqldb -d abc // here abc is image name of application
& mysqldb is name of sql db image.
it terminates the server and shows cannot open the connection.
Even though I had provided privileges to it and also provided the ip addr of sql image to application image.
I have seen that my sql server is running properly and it connect to mysql client also.
Please proivide a better solution**

Mysql backup error

I wrote the below line to create a mysql backup , for some reasons i'm getting Errcode 13 .
E:\Xampp\xampp\mysql\bin\\mysqldump -u root --add-drop-database -B project_db -r C:\Documents and Settings\Administrator\My Documents\a.sql
Why does the above line fail to execute ? I'm trying to create a DB backup using the above line ? Pls Help
It's probably because file "C:\Documents" does not exist. You probably want this:
-r "C:\Documents and Settings\Administrator\My Documents\a.sql"
^ ^
Try this
E:\Xampp\xampp\mysql\bin\mysqldump -u root --add-drop-database -B project_db > "C:\Documents and Settings\Administrator\My Documents\a.sql"
" " were missing
E:\Xampp\xampp\mysql\bin\mysqldump -u root --add-drop-database -B project_db -r "C:\Documents and Settings\Administrator\My Documents\a.sql"
above command may work