Solved

Bash Script To Connect to MySQL

Posted on 2004-04-14
9
4,374 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
error log using ftp 7 83
How to use question mark (?) in filename with html 25 101
Syslog-ng works. Now what? How to filter and manage? 8 93
Windows 10 linux VM 19 44
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

730 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