Find the answer to your Linux question:
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 ...
  1. #1
    Just Joined!
    Join Date
    Aug 2008
    Posts
    10

    Question 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.

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    this link has a great example of bash script and mysql

    http://bash.cyberciti.biz/backup/bac...base-server-2/

  3. #3
    Just Joined!
    Join Date
    Aug 2008
    Posts
    10
    If I do that,How can I execute 'create database test2' and other mysql commands?

  4. #4
    Linux 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"

  5. #5
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    here's another(the mysql refer is located near the bottom)

    BASH Programming - Introduction HOW-TO

    did you try googling bash mysql?

  6. #6
    Linux 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.

  7. #7
    Just 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.

  8. #8
    Just 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...