Execute mysql sql commands and scripts in shell scripts

1. Execute the mysql command in the script

 

1. Implement mysql -uuser -ppasswd -e "show databases;show databases;" through the -e parameter of mysql

 

2.
Echo "show databases;show databases"|mysql -uuser -ppassword through echo

 

3. Specify the sql to be executed through EOF
mysql -uroot -ppassword <<EOF
show databases;
show databases;
EOF

 

A note about EOF:
Execute commands in the main shell, enter other commands (such as mysql), and the following input, want to be the input of other commands (such as mysql), not the input of the main shell, use <<EOF to tell the main shell , the subsequent input is the input of other commands or subshells, until EOF is encountered, and then return to the main shell.

 

2. Execute the sql file of mysql in the script

 

1、mysql -uroot -ppassword -e "source /root/temp.sql" 

 

2、 mysql -uroot -ppassword </root/temp.sql 


 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326926679&siteId=291194637