Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Bash Script To Connect to MySQL

Posted on 2004-04-14
9
Medium Priority
?
4,436 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
Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

 
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 750 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses

721 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