I want to remotely run the "show master status" command to get the bin log file and position.
mysql --user=<user> --password=<passwd> -e "show master status\G";
ERROR at line 1: Unknown command '\G'.
I want to get the result vertically and then use grep or awk to get the bin log file and position.
Any help is appreciated.
Thanks!
Related
I'm writing a bash script and I want to redirect MySQL errors to a log file.
I had success with the below (ERROR 1045 (28000): Access denied for user... is being appended to the log file)
mysql -u user -pWrongpass -sN -e "query to update db;" 2>&1 | tee -a log
however, I'm not having success with this one. The error is displayed when I run the script but I don't see it in the log file.
result=$(mysql -u user -pWrongpass -sN "query to select from db;") 2>&1 | tee -a log
What's the correct syntax to put the result of a query into a variable while printing any potential error to the log file?
Thanks in advance and let me know if I'm not clear :)
You have to put the entire pipeline inside the command substitution.
result=$(mysql -u user -pWrongpass -sN "query to select from db;" 2>&1 |
tee -a log)
Since the output of mysql is piped to tee, it is the output of tee that you need to capture in result.
Going by the list of flags here, with the line at the top about usage: Usage: mysql [OPTIONS] [database].
I am running Windows 8; my table is "contact"; I want to create a tee file for source C:/myfile.sql with the verbose option on.
I have tried mysql -v contact, -v contact, --verbose contact, --verbose source C:/myfile.sql, and various others.
EDIT: adding screenshot of where I'm trying to run this, in case it helps.
The correct syntax for a verbose interactive session is:
c:\> mysql -u yourUser -p -v yourDatabase
This will launch an interactive session (prompting you for password), and set yourDatabase as the default database for the session.
Once in this interactive session, issue a tee command:
mysql> tee c:/temp/my.out
And now you can source your script:
mysql> source c:/myfile.sql
Of course, you can avoid all this pain by simply putting this in your command prompt:
c:\> mysql -u yourUser -pYourPassword -v yourDatabase < myfile.sql > my.out
This will:
Push the contents of myfile.sql to an "interactive" mysql session... that's essentially a batch process
Redirect all contents of the batch process to my.out
Reference:
MySQL Reference Manual: mysql CLI: mysql options
That should work, be aware of the db name.
mysql -v contact
If you db requires login:
mysql -v -udbuser -p contact
Performing automated database backups for starters and testing commands. I've found for performing an action on each line of a text file via BASH CLI is something to the effect of:
# while read line; do
COMMAND $line
done
I've created a list of the database names file:
# mysql -uroot -e "show databases" > databases
Then tried the following against the file to see if it would work correctly.
# while read line; do
"mysqldump -uroot $line > /dbbackups/$line.sql"
done
Seemingly, this would be working correctly but am met with the following error(s):
[04:58:46] [root#theia database-backup-testing]# cat databases | while read line ; do "mysqldump -uroot $line > $line.sql" ; done
-bash: mysqldump -uroot Database > Database.sql: command not found
-bash: mysqldump -uroot information_schema > information_schema.sql: command not found
-bash: mysqldump -uroot cphulkd > cphulkd.sql: command not found
I am not sure why it is giving command not found, when obviously, the output of the commands seems to be correct. I have also tried using the absolute path of mysqldump (/usr/bin/mysqldump) but it gives the same error(s).
Can anyone fill me in on why this is happening?
EDIT: I found a fix:
The script works if the quotes are removed:
# cat databases |
while read line; do
mysqldump -uroot $line > $line.sql
done
Apparently, the quotes causes it to execute as a string and not a command.
why it is giving command not found
Your quotes are not correct, Try something like this:
while read line ; do mysqldump -uroot "$line" > "$line".sql ; done
Haven't used mysqldump so I cant help with the syntax of the specific command.
Here's a bash script that solves this:
https://github.com/jeevandongre/backup-restore
I am having a problem getting mysql -vvv to output to a log file from a script. Redhat Linux, mysql 5
I can run the following from the Linux command line and I get SQL command output to the screen just like I would expect. Work fine and tells me what I want to know.
mysql -vvv --user=username --pass=password < /path/script.sql
When I add the following to a Linux bash shell script, I do not get and output to the log file. What do I not understand?
mysql -vvv --user=username --pass=password < /path/script.sql >> /tmp/file.log
NOTE: Database connect string is inside my script.sql
MySQL verbose uses the error output, try this:
mysql -vvv --user=username --pass=password < /path/script.sql 2>&1 >> /tmp/file.log
2>&1 redirect the error to standard output
everyone.
I'm a bash script noob, and I'm failing to figure out why I'm getting an unexpected end of file error.
This is my script:
#!/bin/bash
server=8100
while [ $server -le 8121 ]
do
ssh pos$server <<ENDEXP
mysql -u root -p12345 pos_master_prod <<ENDEXP
show slave status \G <<ENDEXP
\q <<ENDEXP
server=$(( $server + 1 ))
done
Any ideas?
Thanks!!
If I understand what it's supposed to do, this should work:
#!/bin/bash
for ((server=8100; server <= 8121; server++)); do
ssh pos$server <<-ENDEXP
mysql -u root -p12345 pos_master_prod
show slave status \G
\q
ENDEXP
done
(Note: be sure the lines to be sent to the remote server are indented with tabs, not spaces; <<- removes leading tabs, but not other forms of indentation.)
Looks like you want to use a here-doc but the syntax is a bit off..