Linux uses scripts to automatically connect to databases

Source: Internet
Author: User

Script Name: mtest1. SH

#!/bin/bash# test connecting to the Mysql servermysql= 'which-u root-p

The user is prompted for a password when running the script:

This is not good enough for non-interactive scripting. The-P command-line parameter causes MySQL to pause and require the user to enter a password, and of course, you can also put the password on the Chitian line to resolve:

$MYSQL Test-u root-p123456

Note that the-p and password must be connected, or there will be an error.

At this point, the operation can be directly into the database.

But this is not a good idea, anyone who can access the script will see the database user account and password.

To solve this problem, you can use a special configuration file for the MySQL program. The MySQL program uses the my.cnf file to read special startup commands and settings. One of these settings is the default password for the MySQL session initiated by the Lake account.

To set a default password in this file, you can add the following content

[Client]password=123456

Then use the chmod command to restrict my.conf file access chmod my.cnf so that only you can see him

You can then use the script to manipulate the database.

Script Name: mtest3. SH

#/bin/bash# Send a command to the MySQL servermysql= 'which'  select * from Employe Es where salary  > 4000; '

Run the script sh mtest3. SH

Output:

Linux uses scripts to automatically connect to databases

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.