Home > Database > Mysql Tutorial > How to Correctly Execute MySQL Commands from Shell Scripts?

How to Correctly Execute MySQL Commands from Shell Scripts?

Susan Sarandon
Release: 2024-11-27 17:33:11
Original
904 people have browsed it

How to Correctly Execute MySQL Commands from Shell Scripts?

Executing MySQL Commands from Shell Scripts

Automating SQL command execution through shell scripts simplifies data management tasks. One common use case is restoring data from an SQL file, requiring a connection to a remote server.

Issue:

Attempting to execute an SQL command via a shell script using the command:

mysql -h "server-name" -u root "password" "database-name" < "filename.sql"
Copy after login

results in an error.

Resolution:

The error likely arises from an incorrect way of specifying the password. To fix it, use the -p flag without any space between it and the password:

mysql -h "server-name" -u "root" "-pXXXXXXXX" "database-name" < "filename.sql"
Copy after login

Additional Tips:

  • Storing the username and password in ~/.my.cnf eliminates the need to include them in the command line.
  • Using the -x flag in the shell script execution command allows you to monitor how each command is executed, aiding in troubleshooting.

Revised Shell Script Example:

perl fb_apps_frm_fb.pl
perl fb_new_spider.pl ds_fbids.txt ds_fbids.sql
mysql -h dbservername -u username "-pXXXXXXXX" dbname < ds_fbids.sql
Copy after login

The above is the detailed content of How to Correctly Execute MySQL Commands from Shell Scripts?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template