Execute Multiple Command Using Batch File and install MySQL using batch file - mysql

I'm trying it write a window batch file to perform several tasks in series, However, it always stops after tie first command in the script.
I am use this this batch file code:
start cmd /k cd %CD%mysql\bin && mysqld --install
I want to use this batch file command and install the MySQL but it run only only one command

You have the following command in your batch file:
start cmd /k cd %CD%mysql\bin && mysqld --install
Lets break it down into smaller pieces.
start start a program, command or batch script (opens in a new window.)
cmd /k cd %CD%mysql\bin run `cd %CD%mysql\bin and then return to the cmd prompt.
&& if the above succeeds then run the next command
mysqld --install run mysqld --install if start cmd /k cd %CD%mysql\bin succeeded
The second part will never run as the first part returned to the command prompt.
Try the following batch file instead:
cd %CD%mysql\bin
mysqld --install
Note the variable CD must be assigned a sensible value, otherwise cd %CD%mysql\bin will fail.

Seems you have a lot of layers here: both start and cmd /c (which I think you'd prefer over cmd /k for use in a batch file).
What's wrong with just cd %CD%\mysql\bin && mysqld --install? This worked fine for me when I attempted to run notepad.exe thus: cd /d %WINDIR%\System32 && notepad (note the additional '\' character here, just in case ... an extra backslash won't hurt if env var CD already has one). For that matter, I'll bet %CD%\mysql\bin\mysqld --install would work just fine.
However, just in case you want the extra cruft – or, more likely, need it for some other functionality you're not showing. Using just cmd:
cmd /c "cd %CD%\mysql\bin && mysqld --install"
using just start:
start "" "cd %CD%\mysql\bin && mysqld --install"
I'd put a solution using both start and cmd, but you just don't need it.
BTW, if you can't just call %CD%\mysql\bin\mysqld --install directly, I'd look into using pushd instead of cd, so that you can call popd at the end of your overall script ... it's just good form to put your script user back in the directory in which they started.

Related

My sh script always run after entrypoint DOCKERFILE

I tried to set up a wordpress solution (installing by myself and not using an official image). I have one container with apache, php and mariadb-client (to interrogate mariadb-server from another container)
I have another container with mariadb on it.
I use wp-cli to configure wordpress website but when i build my docker image, I can't execute the command (inside sh file) which is
wp core config --allow-root --dbname=$MYSQL_DATABASE --dbuser=$MYSQL_USER --dbpass=$MYSQL_PASSWORD --dbhost=172.20.0.2 --dbprefix=wp --path=/var/www/html/wordpress
because my mariadb container isn't up.
So I tried to run this script with entrypoint parameters and when I do my docker-compose up, my script is played and I have the message:
apache-php_SDV | Success: Generated 'wp-config.php' file.
apache-php_SDV | Error: The 'wp-config.php' file already exists.
My script is played every second, I tried to use CMD before and it doesn't work, it's like CMD wasn't run
I have the same result if I want to put CMD after ENTRYPOINT, I can run my script only when both containers are up.
I also tried to use command on my docker-compose.yml but not helpful. Does anyone have a solution?
I resolve myself, it could be useful, i kill PID 1 who run my first script and i run another script who start apache and take PID 1 to the container, there is the code :
#!/bin/bash
set -o errexit
case "$1" in
sh|bash)
set -- "$#"
;;
*)
set -- apache2ctl -D FOREGROUND "$#"
;;
esac
exec "$#" ```

What command does XAMPP use to start MySQL?

To start a MySQL (technically mariadb) module from the XAMPP control panel, you just click the "Start" action button. What command is being run behind the scenes for "Start"? I've tried to replicate it from the command line with a variety of commands, but I've found that the "Start" button will succeed where my command-line commands fail.
The net start and net stop commands from a command line will start and stop services including of course MySQL or MariaDB.
net start mariadb
basically
mysqld.exe --defaults-file="C:\ammpp\my.ini"
mysqld is the server command
the my.ini ist the configuration you have to check the folder
And MySQL80 is the name
see manual for more options
As the github says is the command line in the batch file xampp/mysql_start.bat `
mysql\bin\mysqld --defaults-file=mysql\bin\my.ini --standalone --console
the my.ini makes some minor changes to the default values, no trickery at all
I have no idea what trickery the XAMPP package uses, but if you want to start the mariadb service from the cmd, all you need is an elevated command prompt and the following line:
net start MariaDB
Note: my OS is Win10
A modification of nbk's answer worked for me in PowerShell:
C:\xampp\mysql\bin\mysqld.exe --defaults-file="C:\xampp\mysql\bin\my.ini" --console
This shows up as running in the XAMPP Control Panel (v3.2.4) and uses the correct port from the my.ini file (I'm using port 3308).
To stop the MySQL server, you can hit Ctrl+C, but only if the MySQL server is open with the --console param. To stop it from another terminal I've tried various shutdown commands, but none have worked for me so I've had to use the ugly command:
taskkill /FI "IMAGENAME eq mysqld.exe" /T /F
From a .bat batch file I've also found these commands useful for starting and stopping:
:: Start
start "XamppMySQLD1" powershell -NoExit -command "& 'C:\xampp\mysql\bin\mysqld.exe' --defaults-file='C:\xampp\mysql\bin\my.ini' --console" || echo ERROR && exit /b
:: Is it running? On the expected port?
tasklist | findstr /i "mysql"
netstat -ano | findstr 3308
:: Stop
taskkill /FI "WindowTitle eq XamppMySQLD*" /T /F

Terminal doesnt not recognise mysqld and mysql commands

I am runnin OSX 10.9.5 and while trying to reset my MySQL root pasword I typed this:
sudo mysqld_safe --skip-grant-tables
After being asked for the admin password, I got this error :
sudo: mysqld_safe: command not found
I wrote this in
cd /usr/local/mysql
Also, I have a problem with the sudo command, event though I am logged on the admin account my account, It gives me often permission denied, like using this command for basically the same problem ( reseting my root password )
sudo kill cat /usr/local/mysql/data/rodongi.pid
I then got
cat: /usr/local/mysql/data/rodongi.pid: Permission denied
Password:
After entering the password …
usage: kill [-s signal_name] pid ...
kill -l [exit_status]
kill -signal_name pid ...
kill -signal_number pid ...
I have no idea why
1) I dont have the permission even though I used the sudo command( and another time sudo!! )
2) Why msql-bash doesn't not recognise the mysql and mysqld command ( I also tried in terminal-bash;does not work either)
First problem
You're trying to execute the command mysqld_safe, so that command should be on the PATH where the terminal looks for commands. (You can view these locations by running echo $PATH. The different locations are separated with a colon).
Since you're trying to run a file that is in the local directory you should type ./mysqld_safe to tell the shell that you're giving a path to file, otherwise it'll search for it in the PATH. (You can run the file from anywhere by specifying the full path).
Another solution is to make a symbolic link in /usr/local/bin/ that points to /usr/local/mysql/mysqld_safe` (which is the path to the command if I understood you correctly). That way you can run the command from anywhere because it's in the path the shell is looking for.
Second Problem
The cat command surrounded by backticks is executed by the shell before running the sudo command (If the file was readable for everyone the shell will execute something like: sudo kill 12345).
To run the cat as root you should run this command:
sudo bash -c 'kill `cat /usr/local/mysql/data/rodongi.pid`'
That way, you run bash as root, which in turn runs the kill command, and thus reads the rodongi.pid file as root.

add services to init.d on Ubuntu

I'm trying to simplify commands on my ubuntu server.
For now, to start / stop / restart apache2, we have to type those commands :
stop : /etc/apache2/bin/apachectl -k stop
start : /etc/apache2/bin/apachectl -f /usr/local/apache2/conf/httpd.conf
restart : /etc/apache2/bin/apachectl restart
and kinda same for MySql :
stop : mysqladmin -u root shutdown
start : mysqld_safe --user=mysql --log &
What I wish is to be able to use /etc/init.d/apache2 start or /etc/init.d/mysql start to make it simple and not having to always look at the Wiki to find the exact command to use each time...
I tried to find samples of files to put in init.d folder, but found nothing.
I also wanted to know how to do the same with service apache2 start (and same for stop/restart and MySql).
Is there a difference between using /etc/init.d/xx start and service xx start ?
Thanks for you help !
Just put the executable file i.e apachectl in /etc/init.d/ and than try with /etc/init.d/apache{tab} restart/start/stop and all for mysql too it will work.
Actually init.d contains the shell script file that is executable.
Take care the file should be executable like below
chmod +x /etc/init.d/apachectl

Using MySQL in the command line in OS X - command not found?

I'm trying to get MySQL up and running on my Mac OS X 10.9.5.
I've installed the latest version 5.6.21 of MySQL Community Server. I've gone to system preferences and started the mysql server, then launched terminal and typed this:
/usr/local/mysql/bin/mysql --version
which should return the version. But when I type any of the mysql commands I get command not found.
I've also tried:
sudo mysql_secure_installation
mysql -u root --password=password`
I do have web hosting with MySQL etc installed, but I want to be able to get to grips with it in the command line first.
So there are few places where terminal looks for commands. This places are stored in your $PATH variable. Think of it as a global variable where terminal iterates over to look up for any command. This are usually binaries look how /bin folder is usually referenced.
/bin folder has lots of executable files inside it. Turns out this are command. This different folder locations are stored inside one Global variable i.e. $PATH separated by :
Now usually programs upon installation takes care of updating PATH & telling your terminal that hey i can be all commands inside my bin folder.
Turns out MySql doesn't do it upon install so we manually have to do it.
We do it by following command,
export PATH=$PATH:/usr/local/mysql/bin
If you break it down, export is self explanatory. Think of it as an assignment. So export a variable PATH with value old $PATH concat with new bin i.e. /usr/local/mysql/bin
This way after executing it all the commands inside /usr/local/mysql/bin are available to us.
There is a small catch here. Think of one terminal window as one instance of program and maybe something like $PATH is class variable ( maybe ). Note this is pure assumption. So upon close we lose the new assignment. And if we reopen terminal we won't have access to our command again because last when we exported, it was stored in primary memory which is volatile.
Now we need to have our mysql binaries exported every-time we use terminal. So we have to persist concat in our path.
You might be aware that our terminal using something called dotfiles to load configuration on terminal initialisation. I like to think of it's as sets of thing passed to constructer every-time a new instance of terminal is created ( Again an assumption but close to what it might be doing ). So yes by now you get the point what we are going todo.
.bash_profile is one of the primary known dotfile.
So in following command,
echo 'export PATH=$PATH:/usr/local/mysql/bin' >> ~/.bash_profile
What we are doing is saving result of echo i.e. output string to ~/.bash_profile
So now as we noted above every-time we open terminal or instance of terminal our dotfiles are loaded. So .bash_profile is loaded respectively and export that we appended above is run & thus a our global $PATH gets updated and we get all the commands inside /usr/local/mysql/bin.
P.s.
if you are not running first command export directly but just running second in order to persist it? Than for current running instance of terminal you have to,
source ~/.bash_profile
This tells our terminal to reload that particular file.
That means /usr/local/mysql/bin/mysql is not in the PATH variable..
Either execute /usr/local/mysql/bin/mysql to get your mysql shell,
or type this in your terminal:
PATH=$PATH:/usr/local/mysql/bin
to add that to your PATH variable so you can just run mysql without specifying the path
for me the following commands worked:
$ brew install mysql
$ brew services start mysql
You can just modified the .bash_profile by adding the MySQL $PATH as the following:
export PATH=$PATH:/usr/local/mysql/bin.
I did the following:
1- Open Terminal then $ nano .bash_profile or $ vim .bash_profile
2- Add the following PATH code to the .bash_profile
# Set architecture flags
export ARCHFLAGS="-arch x86_64"
# Ensure user-installed binaries take precedence
export PATH=/usr/local/mysql/bin:$PATH
# Load .bashrc if it exists
test -f ~/.bashrc && source ~/.bashrc
3- Save the file.
4- Refresh Terminal using $ source ~/.bash_profile
5- To verify, type in Terminal $ mysql --version
6- It should print the output something like this:
$ mysql Ver 14.14 Distrib 5.7.17, for macos10.12 (x86_64)
The Terminal is now configured to read the MySQL commands from $PATH which is placed in the .bash_profile .
modify your bash profile as follows
<>$vim ~/.bash_profile
export PATH=/usr/local/mysql/bin:$PATH
Once its saved you can type in mysql to bring mysql prompt in your terminal.
You have to create a symlink to your mysql installation if it is not the most recent version of mysql.
$ brew link --force mysql#5.6
see this post by Alex Todd