Solved

Bash Script To Connect to MySQL

Posted on 2004-04-14
9
4,347 Views
Last Modified: 2013-12-15
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
0
Comment
Question by:chigs20
9 Comments
 
LVL 1

Expert Comment

by:yellamelli
ID: 10831511
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 !!
0
 
LVL 6

Author Comment

by:chigs20
ID: 10834143
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.
0
 
LVL 9

Expert Comment

by:Alf666
ID: 10837609
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

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 6

Author Comment

by:chigs20
ID: 10837747
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
0
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 250 total points
ID: 10838062
Create a file that contains all the commands you want to run in the order in which you want to run them (e.g. /usr/local/bin/dump_database)
Make this file executable:
chmod 755 /usr/local/bin/dump_database

Run this command (as root):
crontab -u user -e
Replace user with the user name of the user the script is supposed to run as (e.g. mysql).
Then type this line:
0 2 * * * /usr/local/bin/dump_database

This will add a cron job that runs every night at 2:00am the commands in the script /usr/local/bin/dump_database. All output of the script will be emailed to the owner of the cron job.
If you don't want the output, use this command:
0 2 * * * /usr/local/bin/dump_database > /dev/null 2>&1
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10838063
Make that
0 3 * * * /usr/local/bin/dump_database
I thought you wrote 2am.
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10883550
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 http://www.experts-exchange.com/help.jsp#hi73
I'll request a review of the grade.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now