Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6779405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:22:53+00:00 2026-05-26T16:22:53+00:00

How can I execute an SQL command through a shell script so that I

  • 0

How can I execute an SQL command through a shell script so that I can make it automated?

I want to restore data I have collected in a SQL file using a shell script. I want to connect to a server and restore data. The command works when executed separately via SSH command line.

This is the command I use:

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

This is the shell script code that creates the file ds_fbids.sql and pipes it into mysql.

perl fb_apps_frm_fb.pl
perl fb_new_spider.pl ds_fbids.txt ds_fbids.sql
mysql -h dbservername -u username -ppassword dbname < ds_fbids.sql

What is the correct way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:22:53+00:00Added an answer on May 26, 2026 at 4:22 pm

    You need to use the -p flag to send a password. And it’s tricky because you must have no space between -p and the password.

    $ mysql -h "server-name" -u "root" "-pXXXXXXXX" "database-name" < "filename.sql"
    

    If you use a space after -p it makes the mysql client prompt you interactively for the password, and then it interprets the next command argument as a database-name:

    $ mysql -h "server-name" -u "root" -p "XXXXXXXX" "database-name" < "filename.sql"
    Enter password: <you type it in here>
    ERROR 1049 (42000): Unknown database 'XXXXXXXX'
    

    Actually, I prefer to store the user and password in ~/.my.cnf so I don’t have to put it on the command-line at all:

    [client]
    user = root
    password = XXXXXXXX
    

    Then:

    $ mysql -h "server-name" "database-name" < "filename.sql"
    

    Re your comment:

    I run batch-mode mysql commands like the above on the command line and in shell scripts all the time. It’s hard to diagnose what’s wrong with your shell script, because you haven’t shared the exact script or any error output. I suggest you edit your original question above and provide examples of what goes wrong.

    Also when I’m troubleshooting a shell script I use the -x flag so I can see how it’s executing each command:

    $ bash -x myscript.sh
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to execute this T-SQL command through a c# method so that I can
I'm trying to execute a SQL command, but I just can't find out why
How can execute sql script stored in *.sql file using MySQLdb python driver. I
I have a sql script that creates several tables, a trigger, and a trigger
In a bash script I execute a command on a remote machine through ssh.
I want to run my .sql script file using my ASP.NET website through ADO.NET.
When I execute a sql statement like Select ..., I can only see ...100%
In SQL Server 2008 Management studio, I can hit F5 to execute everything in
Python has a flag -O that you can execute the interpreter with. The option
In cmd.exe, I can execute the command copy c:\hello.txt c:\hello2.txt and it worked fine.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.