LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu
User Name
Password
Ubuntu This forum is for the discussion of Ubuntu Linux.

Notices


Reply
  Search this Thread
Old 03-10-2011, 04:56 PM   #1
mrpurple
Member
 
Registered: May 2010
Posts: 50

Rep: Reputation: 2
Carry out mysql commands directly from the ubuntu command line interface


Is it possible to carry out mysql commands directly from the ubuntu cli rather than open mysql?

eg to create a database I would normally type
Code:
user@localhost:~$ mysql -u root -p
then INSIDE the mysql CLI I would type
Code:
mysql> create database db_name;
mysql> quit;
How would I do this (or any other) mysql task directly from the ubuntu CLI?

I want to be able to cut and paste lines and lines of commands into the ubuntu cli and then at a later date write those commands to a sript.sh
I'd rather not write a seperate sql script then import it in my ubuntu cli but if thats the only way then so be it.
 
Old 03-10-2011, 05:32 PM   #2
hda7
Member
 
Registered: May 2009
Distribution: Debian wheezy
Posts: 252

Rep: Reputation: 31
You can get commands to mysql by piping in input, or by redirecting input from a file. For example:
Code:
echo "drop db.table" | mysql -u root -p
will drop table "db.table" (mysql should still read passwords from the terminal, though). Similarly
Code:
mysql -u root -p <sqlscript.sql
will read commands from "sqlscript.sql".
 
1 members found this post helpful.
Old 03-10-2011, 05:36 PM   #3
hda7
Member
 
Registered: May 2009
Distribution: Debian wheezy
Posts: 252

Rep: Reputation: 31
You can also embed a larger sql script in a shell file by using something like this:
Code:
#!/bin/bash
mysql -u root -p <<EOF
/* sql code goes here... */
EOF
 
Old 03-10-2011, 06:07 PM   #4
mrpurple
Member
 
Registered: May 2010
Posts: 50

Original Poster
Rep: Reputation: 2
Thanks very much. Just what I was after. I never thought of piping it.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Using PHP directly from the command line on Linux and UNIX LXer Syndicated Linux News 0 05-20-2009 11:30 AM
command line commands bcbotha Linux - Newbie 15 12-01-2008 08:05 AM
line command boot directly into rdesktop (remote desktop connection) asuh Linux - Newbie 6 10-13-2008 11:05 PM
Command Line Interface sj061 Linux - Newbie 4 02-22-2007 01:48 AM
Downloading files with lynx directly via command line Dade Linux - Software 5 06-11-2005 01:43 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu

All times are GMT -5. The time now is 02:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration