I need the script because a DB schema has changed and I have to fill a new table with some values (these have to be calculated using a php class). That was just a short summary, for a small overview).
Here's the first thing I can't figure out. I'm new to scripts so I was watching a few basic/beginner guids. In the shell (windows) I can run:
mysql -B -D www -h xxx -u yyy -pzzz -e "show tables"
this prints me all tables. Fine. If I try something similar in a batch script just to test if a can use variables
#!/bin/bash
V=10 // or V="10" // or V=$10, i saw all of it
mysql -B -D www -h xxx -u yyy -pzzz -e "select * from session_content_score where score_id<$V"
I'm always getting the error The command V cannot be found. I tried every declaration I could find. Whats the problem? How can I make usage of variables, because I need them for fulfilling my task?
The correct way to set a variable in a windows batch script is:
SET foo=bar
echo %foo%
A good windows batch tutorial is here:
Windows batch tutorial
You should question whether you are doing the right thing when you want to populate your database from windows batch scripts :)
Related
I've been searching to understanding the following MySQL command:
framework_mariadb.sql | mysql -u username -p password -h 127.0.0.1 -P 26257 target
My guess was the sql statements within the sql file get executed by mysql for the given target/database. But then I came across the source command in MySQL, ie
\bin\mysql -u root -p testdatabase < C:\Users\Juan\Desktop\databasebackup.sql
So my question is, does the first command and the second command essentially do the same thing? My apologies if this has already been asked, I haven't been able to find details for the first SQL command.
This is more about Linux shell capabilities than it is about MySQL.
The second form runs the mysql client, and uses the < symbol to tell it to take its input from the specified file.
The first form does essentially the same thing, but uses the pipe character | to indicate that the output of the first command should be sent to the input of the second command.
However, for the first form I'd expect the line to start with cat (as in cat framework_mariadb.sql | mysql ...) because the SQL script won't normally run as a shell command. cat is a command that reads one or more files and send s them to its output.
It is possible to set the SQL script up to run like this, but that requires a specific line (#! /bin/cat or similar) to be present at the top of the file, and the file must have the execution bit set. (At least, that's how I'd do it. There might be some other bash magic I'm not aware of. bash is not my forté)
There are many resources on the web that can teach the fundamentals of the Linux shell. You could try Microsoft's Introduction to bash, but there are many others.
I'm trying to run a mysql query on a remote host using a bash script.
${MYSQL} -u ${USER} -p${PASS} -P${PORT} -h ${HOST} -e "select * from information_schema;"
My PASS looks something like "dfsf#DFD". It conatins '#' character.
For some reasons the PASS is not retrieving correctly in the script. Its getting chopped off after '#'.
And for some reason including source /etc/environment in the script, seems to fix the problem.
I want to understand why is this happening. How to make it work without the source statement.
As far as I know - the safest way to specify remote connection details for MySQL is using configuration file: https://dev.mysql.com/doc/refman/8.0/en/option-files.html
So briefly you just put something like this in ~/.my.cnf file:
[client]
host='...'
port='...'
user='...'
password='...'
And then you simply run
mysql -e "select * from information_schema;"
Not sure it suits your situation, but it should solve your issue, if you quote the values.
Quoting at command line is also a solution, but your credentials will be visible to everyone able to see your processes at the system where you run that.
I did tried to search, but nothing comes up that really works for me.
So i would start this thread to see if anyone can help. I hope this is not a stupid question that i overlook something simple.
I have a mac mini, that running with a MySQL server.
There is some day end job, so i put them into a script, trigger by a crontab (Actually I also tried launched as this is mac OS X, but same behavior)
crontab looks like this
15 00 * * * /Users/fgs/Documents/database/process_db.sh > /Users/fgs/Documents/database/output.txt 2>&1
the script looks like this
#!/bin/bash
#some data patching task before everything start
#This sql takes 3 sec
/usr/local/bin/mysql dbname -u root "-ppassword" < /Users/fgs/Documents/database/loadrawdata.sql
#This sql takes 90 sec
/usr/local/bin/mysql dbname -u root "-ppassword" < /Users/fgs/Documents/database/LongLongsql.sql
#This sql takes 1 sec
/usr/local/bin/mysql dbname -u root "-ppassword" < /Users/fgs/Documents/database/anothersql.sql
Behavior:
A. When i execute the shell script directly in terminal, all the 3 sql works
B. When i execute this with crontab, the 90 sec SQL doesn't work (it is an insert into with a very big join, so there is no output printed, i did also tried to > output file, adding 2>&1, also no output), but the SQL before and after it works as expected.
C. To simulate crontab behavior, I tried to use
env - /bin/sh
and then start the shell script manually.
It appears that, the 90 sec longlongsql.sql was running only 5 sec, and skipped to the next line. No error message was displayed
I am wondering if there is any kind of timeout for crontab? (I did searched but found nothing)
I did checked ulimit is unlimited (checked within "env - /bin/sh", and also did tried to put into the script)
I believe it is not related to mysql command, since it works fine by running same scripts (I also did searched this topic, and nothing interesting)
Just wondering if anyone can shed some light on me, a direction or whatever will help.
Thanks everyone in advance.
Don't forget that cron will start an isolated shell where it may not be able to read the file.
I would recommend to put your mysql-stuff inside a script. If you are able to execute the script, cron should also be able to do so.
#!/bin/bash
/usr/local/bin/mysql dbname -u root "-ppassword" < /Users/fgs/Documents/database/LongLongsql.sq
Or:
#!/bin/bash
/usr/local/bin/mysql --user=root --password=xxxxxx -e "/Users/fgs/Documents/database/LongLongsql.sq"
Then call the script from crontab...
We have a simple script to connect to the database (mysql -u root -p -h localhost). I don't want to create 2 scripts (.bat, .sh), how do I just create a single script to address this issue.
You might want to give MSYS a try: http://www.mingw.org/wiki/MSYS
you can use perl scripts. Perl is powerful scripting medium which is platform independent.
use can insert batch command in a variable in perl.
for eg
#!C:\Perl64\bin\perl
my $cmd = 'mysql - u root -p -h localhost';
system $cmd;
This should work fine.
I've been trying for a few hours now to store row data into a BASH var then use that to launch some PHP scripts.
So far, I've only been able to echo out the whole result set. I have since broken that script, so I can't even paste it here. This is what I have, that's not working at all.
#! bin/bash
query=`echo "SELECT id FROM searches WHERE running=1 AND id_user=2" | mysql -u root`
I've never used BASH before, so I'm completely lost.
What I'm asking for is some resources that can show me how I can connect to mysql, then loop through a result set using the data from that row.
Thanks.
You can read mysql --silent output into Bash vars with while read:
>sql="SELECT id FROM searches WHERE running=1 AND id_user=2"
>mysql --silent -u ctrahey test -e "$sql" |while read myid; do
php -f my_processor.php $myid;
done
>
Notes:
sql="... sets a Bash variable of your sql (not actually necessary, just aides readability)
mysql -e the -e option allows you to pass in a query, so you don't need STDIN
mysql --silent --silent suppresses the extra formatting mysql usually does. in my testing, this was actually also unnecessary (the pipe chars in the output did not mess up Bash)
php -f .. this executes a php file, passing the current id as an arg.
Bash is not a good language for interacting with mysql, it only works for very very simple cases. Use php instead (since you mentioned php), it has an api for interacting with mysql databases sanely. And yes, you can run a php script as you would a bash script.
If you want to run a single command with MySQL, use mysql -e or mysql --execute. They are the same command, but the second version is more memorable. If you add this flag to the above, the rows will be stored in the variable.
However, as mentioned by geirha, BASH may not be the best language to get row data. But if you choose to parse it wish BASH, mysql -e is the right command to execute queries.