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