Solved

Bash Script To Connect to MySQL

Posted on 2004-04-14
9
4,357 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.

803 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