Home > Database > Mysql Tutorial > How to Restore MySQL Data from a SQL File Using a Shell Script?

How to Restore MySQL Data from a SQL File Using a Shell Script?

Mary-Kate Olsen
Release: 2024-11-27 22:15:17
Original
730 people have browsed it

How to Restore MySQL Data from a SQL File Using a Shell Script?

Execute MySQL Commands from Shell Script

Introduction:
Executing MySQL commands from a shell script offers automation capabilities for database operations. To achieve this, the MySQL client can be invoked from the script, enabling database connections and command execution.

Question:
How do you execute an SQL command through a shell script to restore data from a SQL file?

Answer:
To execute a MySQL command from a shell script, use the following syntax:

mysql -h "server-name" -u "username" "-pPASSWORD" "database-name" < "filename.sql"
Copy after login

Key Points:

  • -p: Use the -p flag to specify the password. Avoid putting spaces between -p and the password.
  • Database Password: Alternatively, store the user and password in ~/.my.cnf to eliminate the need to specify it on the command line.

Example:

[client]
user = root
password = XXXXXXXX

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

Tips:

  • Use the -x flag when troubleshooting a script to trace its commands.
  • Consult the MySQL documentation for additional connection parameters.

Conclusion:

Following these guidelines ensures seamless execution of MySQL commands from within shell scripts, automating data restoration and other database operations.

The above is the detailed content of How to Restore MySQL Data from a SQL File Using a Shell Script?. 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