Using the shell to manipulate MySQL

Source: Internet
Author: User

Under the Linux platform, in order to avoid each operation of the database (MySQL) into the MySQL command mode, you can use the shell command to manipulate the database directly.

first, write directly SQL Statement

If [$#-ne 1]
Then
echo "Please enter the SQL statement to be run"
Exit-1
Fi
Mysql-dmysql-uroot-p123456abcd-e "$"

-D Specify the database name

-U Specify database user name

-P Specify Database password

-e Specifies the SQL statement to run

Second, the script mode of operation

If [$#-ne 1]
Then
echo "Please enter the SQL statement to be run"
Exit-1
Fi
Cat $ | Mysql-dmysql-uroot-p123456abcd

$ specifies the SQL statement script to run

Iii. examples

#!/bash/sh

Hostname= "localhost"

Username= "Root"

password= "Root"

Dbname= "student_db"

Tablename= "Stu"

show_db= "Show Database"

mysql–h${HOSTNAME}–u ${USERNAME}-p${PASSWORD}–e "${show_db}"

tablename_new=${TABLENAME}_ ' date ' +%y_%m_%d "'

Create_table_sql= "CREATE table if Notexists ${tablename_new} (id int (5) NOT null primary key)"

mysql–h${HOSTNAME}–u ${USERNAME}-p${PASSWORD}–e "${Create_table_sql}"

If [$ = 0]; Then

echo "Create yes"

Fi

Using the shell to manipulate MySQL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.