Summary of the interaction between shell scripts and mysql

Source: Internet
Author: User


The shell script and mysql interaction Method Summarize the shell script to import data files to mysql. Of course, mysqlimport is used. example script: 01 #! /Bin/sh02 # import test_table. SQL into test_database.test_table03 # content in test_table. SQL like "123, abc, 334; N, ddd, 999 "04 2.1605 ### define Login = 192.168.0.6907MYSQL _ USER = root08MYSQL_PW = root09MYSQL_DATABASE = login = test_table11 12 function import_from_file () 13 {14 echo" start importing <$ {MYSQL_TABLE }. SQL> to <$ {MYSQL_IP}/$ {MYSQL_DATABASE}>... "15 echo-n-e" \ t "16 mysqlimport-h $ {MYSQL_IP}-u $ {MYSQL_USER}-p $ {MYSQL_PW} -- fields-terminated-by = ', '-- lines-terminated-by ='; '-L $ {MYSQL_DATABASE }$ {MYSQL_TABLE }. SQL
17 if [0-eq $? ]; Then18 echo "Import completed! "19 www.2cto.com else20 echo" An error occurred while importing. The error code is: $? "21 exit22 fi23 echo24} 25 26 # call function27import_from_file shell script using mysql Command Method 1: 1 #! /Bin/sh2CONNECT_MYSQL = "mysql-h 192.168.0.123-uroot-p123456 test_database" 3SQL = "SELECT * FROM TEST_TABLE" 4 5 echo "$ {SQL}" | $ {CONNECT_MYSQL} method 2: 1 #! /Bin/sh2 www.2cto.com CONNECT_MYSQL = "mysql-h 192.168.0.123-uroot-p123456 test_database" 3SQL = "SELECT * FROM TEST_TABLE" 4 5 echo $ {CONNECT_MYSQL} <MYSQLEOF6 $ {SQL} 7 MYSQLEOF

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.