Results 1 to 8 of 8
hello everybody
I want to connect to mysql and execute some commands like create database and table,.... via bash scripting. I wrote :
#!/bin/bash
clear
cd /etc/init.d
./mysql start
mysql ...
- 09-09-2008 #1Just Joined!
- Join Date
- Aug 2008
- Posts
- 10
bash scripting & mysql
hello everybody
I want to connect to mysql and execute some commands like create database and table,.... via bash scripting. I wrote :
#!/bin/bash
clear
cd /etc/init.d
./mysql start
mysql -h localhost -uroot
mysql create database test2
when I run this, it stops at mysql> , I don't know how pass mysql commands through bash scripting. Please help me.
Best regards.
- 09-09-2008 #2
this link has a great example of bash script and mysql
http://bash.cyberciti.biz/backup/bac...base-server-2/
- 09-09-2008 #3Just Joined!
- Join Date
- Aug 2008
- Posts
- 10
If I do that,How can I execute 'create database test2' and other mysql commands?
- 09-09-2008 #4Linux User
- Join Date
- May 2008
- Location
- NYC, moved from KS & MO
- Posts
- 251
the answer is yes, you can do it like this:
CMD="create database test2; use test2; another_sql_command; another ..."
mysql -u root -pYOURPASSWORD -e "$CMD"
- 09-09-2008 #5
here's another(the mysql refer is located near the bottom)
BASH Programming - Introduction HOW-TO
did you try googling bash mysql?
- 09-10-2008 #6Linux User
- Join Date
- May 2008
- Location
- NYC, moved from KS & MO
- Posts
- 251
Another way to accomplish what you want is, for example:
echo "create database test2; another mysql command here; another maybe ...;" | mysql -u root -pYOURPASSWORD
the key is you have to supply the mysql root password in the script in order to make it run non-interactively.
- 09-13-2008 #7Just Joined!
- Join Date
- Aug 2008
- Posts
- 10
hi,
I solved my problem simply like this:
mysql -h localhost -uroot < adminpro.sql
adminpro.sql script file contains:
CREATE DATABASE T1 \g
USE T1
CREATE TABLE `myuser` (
....
) ....
your solutions also work correctly.
- 02-22-2012 #8Just Joined!
- Join Date
- Feb 2012
- Posts
- 1
Bash scripting you can probably perform this but it might be easier using pythons module SQLObject. This module is a snap to install and the objects are created in an object oriented fashion. I performed a test of the module from the simple python command line interpreter that created the objects across a network and it took about 5 minutes to complete.
The example you can follow is on this page: sqlobject.org
Obviously you will have to use the correct syntax for your DB connection.
sqlobject.org/SQLObject.html#declaring-a-connection
Cheers


Reply With Quote