Connect MySQL database from Unix


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Connect MySQL database from Unix
# 1  
Old 03-23-2010
Connect MySQL database from Unix

How to connect a MySQL database from unix using unix shell scripting ( people are using perl scrpt to connect the same database).

I want to access a MySQL database through a shell script
# 2  
Old 03-23-2010
Quote:
Originally Posted by apsprabhu
How to connect a MySQL database from unix using unix shell scripting ( people are using perl scrpt to connect the same database).

I want to access a MySQL database through a shell script
Well, put all the commands that you'd normally run from the shell's command prompt into a shell script, and there you have it -

Code:
$ 
$ 
$ cat -n connect_mysql.sh
     1 #!/bin/bash
     2 # connect to test database and query the EMP table
     3 mysql -u test -ptest test <<EOF
     4 select * from emp;
     5 EOF
     6
$ 
$ ./connect_mysql.sh
empno ename sal
1000 SCOTT 7987.25
2000 JAMES 3489.00
3000 DAVID 5520.75
4000 PETER 6789.01
$ 
$

HTH,
tyler_durden
# 3  
Old 03-24-2010
Thanks a lot Durden...

As i'm new to mysql , Please clarify me on the following,


Code:
$ cat -n connect_mysql.sh
     1 #!/bin/bash
     2 # connect to test database and query the EMP table
     3 mysql -u test -ptest test <<EOF
     4 select * from emp;
     5 EOF


mysql -u test -ptest test
---> here what does the "-u" do?
---> here which is the user name?
--->here which is the database name?
---> here why the "test" comes twice?

Last edited by pludi; 03-24-2010 at 09:26 AM.. Reason: code tags, please...
# 4  
Old 03-24-2010
man mysql
mysql -?

-u means the next argument is the user name (not necessarily separated with a whitespace)
-p means the next argument is the password (not necessarily separated with a whitespace, if empty it's read from stdin)
the last argument is the database to connect to.
# 5  
Old 03-25-2010
Thanks

Thanks a lot for your guidance
 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

MySQL Database Missing on UNIX server. Possible to recover?

My dedicated server is VERY old (running Apache/1.3.37 (Unix) PHP/4.4.4 FrontPage/5.0.2.2510). We have a script that runs and manages our customer database. Early in the morning last Friday, it was working fine and processed data as usual. Later in the day, we got this error: Cannot... (2 Replies)
Discussion started by: SurfMe69
2 Replies

2. Shell Programming and Scripting

Connect to database from different unix box

Hi Friends, I have a one unix box "x.y.z" on SunOS , where oracle is not installed. And another machine "a.b.c" on AIX , where oracle is installed. Now i want to make a script on x.y.z , that can connect to database on a.b.c, using sqlplus command. and then generate a report based on the... (5 Replies)
Discussion started by: gauravgarg
5 Replies

3. Shell Programming and Scripting

How to connect with oracle database using unix...

Hi all I am working in datawarehouse project and use DB2 database . shell scripting is written to connect with DB2 database in my application but i dont know how to connect with oracle databse or other databases.Is there any command in unix to connect any of these datbases? (6 Replies)
Discussion started by: vijays3
6 Replies

4. Shell Programming and Scripting

How to COnnect to Oracle database from UNIX Box

I am trying to connect to Oracle Databse from UNIX box using sqlplus command. But i get sqplus not found message. $ sqlplus sqlplus: not found I have searched in forums and all are suggesting to set environment varaibles...can somebody tell me what variables to set and how to set.and the... (12 Replies)
Discussion started by: angelarosh
12 Replies

5. Shell Programming and Scripting

how will i connect postgres database from unix shell script

I have got the solution so just closing this issue. (3 Replies)
Discussion started by: jambesh
3 Replies

6. Shell Programming and Scripting

How to connect to database db2 through Unix Shell Script

I am trying to connect to database db2 through shell script. The process I am trying is > db2 It gives me error Access Denied. While checking for access I have the rights. Is there ant other way round..? Please help. (3 Replies)
Discussion started by: ankitgupta
3 Replies

7. Shell Programming and Scripting

Can i connect from a .sh or .sc to a database?

please, help me-.... i need connect to a database Sybase IQ 12 and I don't know if i can do it using a shell program. if it's posibble, please tell me how... please, help me, because I don't know what to do.... thanks... jonathan (1 Reply)
Discussion started by: DebianJ
1 Replies

8. Programming

¿how can I connect C to a database?

hi, please, help me i need connect my program in C whit a SybaseIQ 12 database i´m programming in solaris 5 (unix) please, help me, because i don`t know what to do... thanks.. jonathan (0 Replies)
Discussion started by: DebianJ
0 Replies
Login or Register to Ask a Question