MySQL executes SQL query and uploads to remote server

Source: Internet
Author: User

In recent projects, a shell script is required to perform SQL operations on a database and convert the results to txt, and the filter results are tab-delimited and saved to a remote server for other people to use in Excel to read content in txt.

MySQL saves the results in two scenarios, adding an into outfile statement to the SQL statement and customizing the output format. But this method leaves the file on the server where the database resides, and expects the file to be placed on the machine that executes the script.

Later implementation is not to change the contents of the SQL statement, in the script to save the results to the local/tmp directory, and then use curl to upload to the remote server, the following extract script the main structure of the code, file upload or code conversion and so on.

#! /bin/Bashdb_host='your_db_address'Db_port=3306Db_username='User'Db_password='Password'db_name='Your_db_name'table_name='Your_table_name'output_file='/tmp/output.txt'sql_stmt="Use ${db_name}; \SELECT * from ${table_name}"MySQL-H $db _host-p $db _port-u $db _username--password= $db _password-e"$sql _stmt"-B > $output _file

MySQL executes SQL query and uploads to remote server

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.