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