Link to home
Start Free TrialLog in
Avatar of chigs20
chigs20

asked on

Bash Script To Connect to MySQL

I am a newbie to Linux shell scripting and I am hoping somebody can help point me in the write direction.
I want to create a basic Bash Shell Script that will allow me to connect to MySQL.  Once connected I will do various operations (IE.  mysqldump)
Also, if you can add something about cron and how to use it to automate tasks.  
Thank you to anybody that can help.

Specs:
Linux Red Hat 9.0 (I believe Kernal 2.6)
MySQL 4.0.16

Chigs
Avatar of yellamelli
yellamelli

There are two aspects to your question.

1) Do you want to get inside the database and perform operations

or

2) Do you want perform operations on the MySql database without getting into the database.

You can take an sql dump without actually getting into the mysql database.

Cheers !!
Avatar of chigs20

ASKER

For now I want to get inside the database and perform operations.  This way I can perform other operations if necessary and/or copy the script to do separate operations.
Simply put all your sql queries in a file and do the following :

mysql -u username -ppassword database_name < command_file

where username is your username, password, your password (yes, it has to be stuck to the -p), database_name, your DB name
and the command file, the file you put your commands into.

You could also do the following :

mysql -u username -ppassword database_name << EOF
select * from ...
...
...
EOF

And thus put your commands inline.

For mysqldump, you can do the same :

mysqldump -u username -ppassword db_name

Avatar of chigs20

ASKER

I apologize if I didn't explain myself well enough.
I know how to create a text file containing the operations (mysqldump, DML commands, ect).  What I want to create is
1. A Bash Shell Script that connects to MySQL automatically.
2. Use Cron to run the shell script


IE.
I want to backup the database server everyday @ 3am
ASKER CERTIFIED SOLUTION
Avatar of Karl Heinz Kremer
Karl Heinz Kremer
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Make that
0 3 * * * /usr/local/bin/dump_database
I thought you wrote 2am.
chigs20, you either did get what you asked for, or you didn't. In the latter case, you should have asked for clarification or more information. If you did get an answer, you should not have given a "C" grade (please review the grading guidelines at https://www.experts-exchange.com/help.jsp#hi73
I'll request a review of the grade.